feat: add battery activity info logs for Shelly emulation#241
feat: add battery activity info logs for Shelly emulation#241
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughBattery 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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
shelly/shelly.py (1)
112-127: Consider logging outside the lock for consistency.Unlike
_track_battery_seenwhich 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.
Summary
Why
This makes connection diagnostics easier without requiring debug logs.
Validation
pipenv run pytest -q shelly/shelly_udp_test.pySummary by CodeRabbit