Skip to content

feat: add battery activity info logs for Shelly emulation#241

Open
tomquist wants to merge 2 commits intodevelopfrom
feat/log-battery-first-seen-and-inactive
Open

feat: add battery activity info logs for Shelly emulation#241
tomquist wants to merge 2 commits intodevelopfrom
feat/log-battery-first-seen-and-inactive

Conversation

@tomquist
Copy link
Owner

@tomquist tomquist commented Mar 5, 2026

Summary

  • log an INFO message when a battery IP is seen for the first time on a Shelly UDP emulator port
  • log an INFO message when a previously seen battery has been inactive for >=120s
  • log an INFO message when an inactive battery reconnects

Why

This makes connection diagnostics easier without requiring debug logs.

Validation

  • pipenv run pytest -q shelly/shelly_udp_test.py

Summary by CodeRabbit

  • New Features
    • Battery monitoring system added to track device connectivity and detect inactive batteries.
    • Automatic detection and logging of battery devices that exceed the inactivity timeout threshold.
    • System logs reconnection events when previously inactive batteries come back online.
    • Enhanced visibility into battery device status and availability.

@coderabbitai
Copy link

coderabbitai bot commented Mar 5, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5f5dbcc6-f679-4696-8ae9-3c719fbeb17c

📥 Commits

Reviewing files that changed from the base of the PR and between a8caaf8 and 726954e.

📒 Files selected for processing (1)
  • shelly/shelly.py

Walkthrough

Battery inactivity tracking was added to the Shelly module: a new timeout constant, state fields for per-battery last-seen and inactive sets, methods to record sightings and mark/log inactive batteries, and integration of tracking into request handling and the UDP server loop.

Changes

Cohort / File(s) Summary
Battery Tracking
shelly/shelly.py
Added BATTERY_INACTIVE_TIMEOUT_SECONDS; initialized _battery_state_lock, _battery_last_seen, _inactive_batteries; added _track_battery_seen() and _log_inactive_batteries(); call tracking from _handle_request() and on UDP socket timeout / each UDP loop iteration.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding battery activity info logs to the Shelly UDP emulation component. It directly relates to the changeset's primary objective of improving connection diagnostics through battery activity logging.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/log-battery-first-seen-and-inactive

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
shelly/shelly.py (1)

112-127: Consider logging outside the lock for consistency.

Unlike _track_battery_seen which logs outside the lock, this method performs I/O while holding _battery_state_lock. While unlikely to cause issues in practice (logging is typically fast), moving the log calls outside the lock would be more consistent and slightly reduce contention.

♻️ Proposed refactor to log outside the lock
 def _log_inactive_batteries(self):
     now = time.time()
+    newly_inactive = []

     with self._battery_state_lock:
         for battery_ip, last_seen in self._battery_last_seen.items():
             if (
                 now - last_seen >= BATTERY_INACTIVE_TIMEOUT_SECONDS
                 and battery_ip not in self._inactive_batteries
             ):
                 self._inactive_batteries.add(battery_ip)
-                logger.info(
-                    "Battery inactive on Shelly UDP port %s for >= %ss: %s",
-                    self._udp_port,
-                    BATTERY_INACTIVE_TIMEOUT_SECONDS,
-                    battery_ip,
-                )
+                newly_inactive.append(battery_ip)
+
+    for battery_ip in newly_inactive:
+        logger.info(
+            "Battery inactive on Shelly UDP port %s for >= %ss: %s",
+            self._udp_port,
+            BATTERY_INACTIVE_TIMEOUT_SECONDS,
+            battery_ip,
+        )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@shelly/shelly.py` around lines 112 - 127, The _log_inactive_batteries method
is doing logger.info calls while holding _battery_state_lock which is
inconsistent with _track_battery_seen and can increase contention; inside the
lock, compute which battery_ip values should be marked inactive and add them to
_inactive_batteries, but collect those battery_ip entries (and any data needed
for the message such as _udp_port and BATTERY_INACTIVE_TIMEOUT_SECONDS) into a
local list, release the lock, then iterate that list and call logger.info for
each inactive battery; update references to _battery_last_seen,
_inactive_batteries, _battery_state_lock, logger.info, _udp_port and
BATTERY_INACTIVE_TIMEOUT_SECONDS accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@shelly/shelly.py`:
- Around line 112-127: The _log_inactive_batteries method is doing logger.info
calls while holding _battery_state_lock which is inconsistent with
_track_battery_seen and can increase contention; inside the lock, compute which
battery_ip values should be marked inactive and add them to _inactive_batteries,
but collect those battery_ip entries (and any data needed for the message such
as _udp_port and BATTERY_INACTIVE_TIMEOUT_SECONDS) into a local list, release
the lock, then iterate that list and call logger.info for each inactive battery;
update references to _battery_last_seen, _inactive_batteries,
_battery_state_lock, logger.info, _udp_port and BATTERY_INACTIVE_TIMEOUT_SECONDS
accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1935c342-4f9c-4a0a-87db-71a8c2de625a

📥 Commits

Reviewing files that changed from the base of the PR and between 1acd1d1 and a8caaf8.

📒 Files selected for processing (1)
  • shelly/shelly.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant