Skip to content

Conversation

@bouwew
Copy link
Contributor

@bouwew bouwew commented May 30, 2025

Summary by CodeRabbit

  • New Features
    • Added the ability to configure energy measurement intervals for devices asynchronously.
  • Enhancements
    • Improved logic for determining energy log direction, enhancing production and consumption tracking accuracy.
    • Updated acknowledgment handling for device interval configuration requests.
  • Bug Fixes
    • Adjusted pulse log values in tests to ensure correct handling of production and consumption data.
  • Chores
    • Updated version to v0.41.0.

@coderabbitai
Copy link

coderabbitai bot commented May 30, 2025

Walkthrough

The changes add a new asynchronous method set_energy_intervals to configure energy logging intervals for nodes in the Plugwise USB integration, updating the main Stick class and its network layer. The message request and response classes are extended to support this feature. Pulse log direction logic is refined with new state flags and helper methods. Tests and the project version are updated accordingly.

Changes

File(s) Change Summary
plugwise_usb/init.py Added async method set_energy_intervals to Stick class to configure node energy logging intervals.
plugwise_usb/network/init.py Added import and async method set_energy_intervals to StickNetwork for sending interval requests and validating responses.
plugwise_usb/messages/requests.py Updated docstring and added async send method to CircleMeasureIntervalRequest for sending and validating requests.
plugwise_usb/messages/responses.py Added POWER_LOG_INTERVAL_ACCEPTED enum member and clarified comments for ack-type enum members in NodeResponseType.
plugwise_usb/nodes/helpers/pulses.py Added _first_next_log_processed and _first_prev_log_processed flags to PulseCollection; refined _update_log_direction logic with new helpers and enhanced debug logging.
tests/test_usb.py Modified pulse log values and assertions in test_pulse_collection_production to match updated production/consumption logic.
pyproject.toml Updated project version from v0.40.1b1 to v0.41.0.
CHANGELOG.md Added version 0.41.0 entry documenting energy logging intervals feature; moved previous 0.40.1 entry below.
scripts/tests_and_coverage.sh Changed test script to always run pytest with debug logging, removing coverage and fallback rerun logic.

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant Stick
  participant StickNetwork
  participant CircleMeasureIntervalRequest
  participant Device

  User->>Stick: set_energy_intervals(mac, cons_interval, prod_interval)
  Stick->>StickNetwork: set_energy_intervals(mac, cons_interval, prod_interval)
  StickNetwork->>CircleMeasureIntervalRequest: create and send request
  CircleMeasureIntervalRequest->>Device: send request
  Device-->>CircleMeasureIntervalRequest: NodeResponse (POWER_LOG_INTERVAL_ACCEPTED)
  CircleMeasureIntervalRequest-->>StickNetwork: return response
  StickNetwork-->>Stick: return success
  Stick-->>User: return True
Loading

Poem

A bunny hops with code anew,
Setting intervals—one, then two!
Pulses tracked with careful care,
Directions sorted, logs laid bare.
Version bumps, responses clear,
The network listens—bunny cheer!
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Pylint (3.3.7)
plugwise_usb/__init__.py
plugwise_usb/messages/requests.py
plugwise_usb/messages/responses.py
  • 3 others
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link

codecov bot commented May 30, 2025

Codecov Report

Attention: Patch coverage is 62.50000% with 30 lines in your changes missing coverage. Please review.

Project coverage is 80.97%. Comparing base (497ee31) to head (a969a31).
Report is 41 commits behind head on main.

Files with missing lines Patch % Lines
plugwise_usb/network/__init__.py 14.28% 12 Missing ⚠️
plugwise_usb/nodes/helpers/pulses.py 84.44% 7 Missing ⚠️
plugwise_usb/messages/requests.py 14.28% 6 Missing ⚠️
plugwise_usb/__init__.py 16.66% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #247      +/-   ##
==========================================
- Coverage   81.21%   80.97%   -0.24%     
==========================================
  Files          36       36              
  Lines        7369     7418      +49     
==========================================
+ Hits         5985     6007      +22     
- Misses       1384     1411      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Actionable comments posted: 3

🧹 Nitpick comments (1)
plugwise_usb/messages/requests.py (1)

1267-1267: Fix trailing whitespace in docstring.

The docstring clarification about the expected response message is helpful, but there's trailing whitespace that should be removed.

Apply this diff to fix the whitespace issue:

-    Response message: NodeResponse with ack-type POWER_LOG_INTERVAL_ACCEPTED 
+    Response message: NodeResponse with ack-type POWER_LOG_INTERVAL_ACCEPTED
🧰 Tools
🪛 Ruff (0.11.9)

1267-1267: Trailing whitespace

Remove trailing whitespace

(W291)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e3e84a4 and f2d0c27.

📒 Files selected for processing (7)
  • plugwise_usb/__init__.py (1 hunks)
  • plugwise_usb/messages/requests.py (2 hunks)
  • plugwise_usb/messages/responses.py (1 hunks)
  • plugwise_usb/network/__init__.py (2 hunks)
  • plugwise_usb/nodes/helpers/pulses.py (3 hunks)
  • pyproject.toml (1 hunks)
  • tests/test_usb.py (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (4)
plugwise_usb/nodes/helpers/pulses.py (1)
plugwise_usb/nodes/helpers/counter.py (3)
  • timestamp (170-178)
  • direction (209-211)
  • is_consumption (229-231)
tests/test_usb.py (2)
plugwise_usb/nodes/helpers/pulses.py (7)
  • add_log (416-451)
  • log_addresses_missing (795-797)
  • production_logging (138-140)
  • log_interval_consumption (143-145)
  • log_interval_production (148-150)
  • update_pulse_counter (272-314)
  • collected_pulses (167-215)
plugwise_usb/nodes/helpers/counter.py (2)
  • log_addresses_missing (119-121)
  • is_consumption (229-231)
plugwise_usb/network/__init__.py (5)
plugwise_usb/messages/requests.py (18)
  • CirclePlusAllowJoiningRequest (454-485)
  • CircleMeasureIntervalRequest (1262-1293)
  • send (365-374)
  • send (387-396)
  • send (438-440)
  • send (476-485)
  • send (512-521)
  • send (542-553)
  • send (566-575)
  • send (588-597)
  • send (621-630)
  • send (667-676)
  • send (719-728)
  • send (775-784)
  • send (808-817)
  • send (848-857)
  • send (880-889)
  • response (125-129)
plugwise_usb/__init__.py (2)
  • set_measure_interval (213-221)
  • name (141-143)
plugwise_usb/connection/__init__.py (1)
  • send (231-242)
plugwise_usb/exceptions.py (2)
  • NodeError (24-25)
  • MessageError (20-21)
plugwise_usb/messages/responses.py (1)
  • NodeResponseType (56-72)
plugwise_usb/messages/requests.py (3)
plugwise_usb/connection/__init__.py (1)
  • send (231-242)
plugwise_usb/messages/responses.py (1)
  • NodeResponse (243-270)
plugwise_usb/exceptions.py (1)
  • MessageError (20-21)
🪛 Ruff (0.11.9)
plugwise_usb/__init__.py

220-220: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

plugwise_usb/nodes/helpers/pulses.py

540-540: Trailing whitespace

Remove trailing whitespace

(W291)

plugwise_usb/network/__init__.py

548-548: Blank line contains whitespace

Remove whitespace from blank line

(W293)

plugwise_usb/messages/requests.py

1267-1267: Trailing whitespace

Remove trailing whitespace

(W291)

🔇 Additional comments (15)
plugwise_usb/nodes/helpers/pulses.py (3)

94-95: LGTM!

The new boolean flags for tracking previous and next log existence are properly initialized.


444-450: Good improvement to debug logging!

Including the direction in the log message will help with debugging pulse log issues.


570-575: Clever heuristic for production detection!

The logic that sets _log_production to True when 2 out of 3 consecutive slots have the same timestamp is a smart way to detect production logging patterns.

pyproject.toml (1)

7-7: LGTM!

The alpha version bump to v0.41.0a4 is appropriate for a pre-release feature.

plugwise_usb/network/__init__.py (1)

17-21: LGTM: Import addition is appropriate.

The addition of CircleMeasureIntervalRequest to the imports is necessary for the new functionality and follows the existing import pattern.

plugwise_usb/messages/requests.py (1)

1284-1294: LGTM: Send method implementation follows established patterns.

The send method is well-implemented and consistent with other request classes in the file. It properly validates the response type and handles both success and error cases appropriately.

tests/test_usb.py (6)

1221-1221: LGTM: Production pulse value correctly updated to negative.

The change from positive to negative production pulse value (-2000) aligns with the refined pulse direction logic where negative pulses indicate production when production logging is enabled.


1229-1229: LGTM: Consumption pulse value correctly set to zero.

The consumption pulse value is appropriately set to 0, which is consistent with the test scenario for production/consumption energy log validation.


1238-1238: LGTM: Production pulse value correctly updated to negative.

The change to negative production pulse value (-2200) is consistent with the updated pulse direction logic in the PulseCollection class.


1248-1248: LGTM: Consumption pulse value correctly set to zero.

The consumption pulse value is correctly set to 0, maintaining consistency with the production/consumption test scenario.


1255-1255: LGTM: Pulse counter values correctly updated.

The pulse counter update with consumption=0 and production=-500 (negative) is consistent with the refined pulse direction logic where negative values represent production.


1258-1273: LGTM: Test assertions correctly updated to match new pulse logic.

The assertions have been properly updated to reflect the new behavior:

  • Consumption assertions now expect 0 values (lines 1258, 1264, 1267)
  • Production assertions expect positive values (500, 2000, 2500) which are correctly converted from negative input values (-500, -2000, -2200) by the collected_pulses method that returns absolute values

This aligns with the relevant code snippet showing that collected_pulses always returns positive values for energy statistics: return (abs(pulses + log_pulses), timestamp).

plugwise_usb/messages/responses.py (3)

59-59: Clarified comment for CIRCLE_PLUS enum member
The updated comment makes it explicit that this value is an acknowledgment for CirclePlusAllowJoiningRequest with state false, improving readability.


61-61: Clarified comment for JOIN_ACCEPTED enum member
The refined comment now clearly denotes this as an acknowledgment for CirclePlusAllowJoiningRequest with state true, enhancing clarity.


62-62: Added POWER_LOG_INTERVAL_ACCEPTED to NodeResponseType
Introducing the POWER_LOG_INTERVAL_ACCEPTED member aligns with the new measurement interval request/response flow and ensures proper acknowledgment handling.

@bouwew bouwew changed the title Enable use of ... Implement setting of energy logging intervals May 30, 2025
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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
plugwise_usb/network/__init__.py (1)

544-570: Method implementation is solid but fix whitespace issues.

The set_energy_intervals method implementation follows good patterns with proper input validation, logging, and error handling. However, there are whitespace formatting issues that need to be addressed.

Apply this diff to fix the whitespace issues:

     """Set the logging intervals for both energy consumption and production.
-        
+        
     Default: consumption = 60, production = 0.
     For logging energy in both directions set both to 60.
     """
     # Validate input parameters
     if consumption <= 0:
         raise ValueError("Consumption interval must be positive")
     if production < 0:
         raise ValueError("Production interval must be non-negative")
     if production > 0 and production % consumption != 0:
         raise ValueError("Production interval must be a multiple of consumption interval")

-    _LOGGER.debug("set_energy_intervals | cons=%s, prod=%s", consumption, production)        
+    _LOGGER.debug("set_energy_intervals | cons=%s, prod=%s", consumption, production)
     request = CircleMeasureIntervalRequest(
🧰 Tools
🪛 Ruff (0.11.9)

548-548: Blank line contains whitespace

Remove whitespace from blank line

(W293)


560-560: Trailing whitespace

Remove trailing whitespace

(W291)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9d185a5 and 4a6e7f2.

📒 Files selected for processing (3)
  • plugwise_usb/__init__.py (1 hunks)
  • plugwise_usb/network/__init__.py (2 hunks)
  • plugwise_usb/nodes/helpers/pulses.py (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • plugwise_usb/init.py
  • plugwise_usb/nodes/helpers/pulses.py
🧰 Additional context used
🪛 Ruff (0.11.9)
plugwise_usb/network/__init__.py

548-548: Blank line contains whitespace

Remove whitespace from blank line

(W293)


560-560: Trailing whitespace

Remove trailing whitespace

(W291)

🔇 Additional comments (1)
plugwise_usb/network/__init__.py (1)

17-21: Import addition looks good.

The addition of CircleMeasureIntervalRequest to the imports is appropriate and necessary for the new set_energy_intervals method.

@bouwew bouwew marked this pull request as ready for review May 30, 2025 08:57
@bouwew bouwew requested a review from a team as a code owner May 30, 2025 08:57
@bouwew bouwew requested review from ArnoutD, brefra and dirixmjm May 30, 2025 08:58
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.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9a0e301 and 9003710.

📒 Files selected for processing (1)
  • plugwise_usb/nodes/helpers/pulses.py (3 hunks)
🧰 Additional context used
🪛 Ruff (0.11.9)
plugwise_usb/nodes/helpers/pulses.py

547-547: SyntaxError: Expected an indented block after function definition


548-548: SyntaxError: Unexpected indentation

🔇 Additional comments (3)
plugwise_usb/nodes/helpers/pulses.py (3)

94-95: LGTM! State tracking variables properly initialized.

The new boolean flags _next_log_exists and _prev_log_exists are appropriately initialized to False and will help track the existence of adjacent logs for improved direction detection logic.


444-449: Good enhancement to debug logging.

Adding the direction parameter to the debug log message provides valuable context for troubleshooting pulse direction detection issues.


519-543: LGTM! Well-structured helper method for previous log checking.

The _check_prev_production method properly handles the logic for checking previous logs and updating consumption/production flags based on timestamp comparisons. The state tracking with _prev_log_exists improves efficiency by ensuring checks are performed only once.

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.

Actionable comments posted: 2

🧹 Nitpick comments (1)
plugwise_usb/nodes/helpers/pulses.py (1)

380-381: Fix trailing whitespace and approve the explicit return.

The explicit return False statement is good practice. However, there's trailing whitespace on line 380 that should be removed.

Apply this diff to fix the whitespace:

-                    
+
🧰 Tools
🪛 Ruff (0.11.9)

380-380: Blank line contains whitespace

Remove whitespace from blank line

(W293)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9003710 and eb7ef20.

📒 Files selected for processing (1)
  • plugwise_usb/nodes/helpers/pulses.py (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
plugwise_usb/nodes/helpers/pulses.py (1)
plugwise_usb/nodes/helpers/counter.py (2)
  • direction (209-211)
  • is_consumption (229-231)
🪛 Ruff (0.11.9)
plugwise_usb/nodes/helpers/pulses.py

380-380: Blank line contains whitespace

Remove whitespace from blank line

(W293)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Check commit
  • GitHub Check: Check commit
🔇 Additional comments (3)
plugwise_usb/nodes/helpers/pulses.py (3)

94-95: LGTM! Good state tracking implementation.

The new boolean flags _next_log_processed and _prev_log_processed provide proper state tracking for the refactored direction detection logic, ensuring each log is processed only once per invocation.


445-450: LGTM! Improved debug logging.

Adding the direction parameter to the debug logging provides valuable information for troubleshooting pulse direction detection issues.


511-518: Good refactoring approach with improved code organization.

The extraction of complex direction detection logic into helper methods improves code readability and maintainability. The use of processed flags and timestamp comparisons follows a logical approach.

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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
plugwise_usb/nodes/helpers/pulses.py (1)

380-380: Fix trailing whitespace on blank line.

The static analysis tool correctly identifies trailing whitespace on this blank line.

-                    
+
🧰 Tools
🪛 Ruff (0.11.9)

380-380: Blank line contains whitespace

Remove whitespace from blank line

(W293)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eb7ef20 and e272fe0.

📒 Files selected for processing (1)
  • plugwise_usb/nodes/helpers/pulses.py (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
plugwise_usb/nodes/helpers/pulses.py (1)
plugwise_usb/nodes/helpers/counter.py (3)
  • timestamp (170-178)
  • direction (209-211)
  • is_consumption (229-231)
🪛 Ruff (0.11.9)
plugwise_usb/nodes/helpers/pulses.py

380-380: Blank line contains whitespace

Remove whitespace from blank line

(W293)

🔇 Additional comments (5)
plugwise_usb/nodes/helpers/pulses.py (5)

91-92: LGTM! Good state management improvement.

The new boolean flags _first_next_log_processed and _first_prev_log_processed provide clear state tracking for the refactored log direction detection logic. This ensures each log is processed only once per invocation, improving the robustness of the algorithm.


445-450: LGTM! Enhanced debugging information.

Adding the direction parameter to the debug logging provides valuable context for troubleshooting energy flow direction issues. This will help with debugging consumption vs production pulse logging.


511-518: LGTM! Well-structured refactoring.

The refactored logic properly delegates to helper methods and uses the returned timestamps for accurate direction detection. The condition correctly identifies production logging when 2 out of 3 consecutive slots have the same timestamp, and handles None values appropriately when logs don't exist.


520-545: LGTM! Robust helper method implementation.

The _check_prev_production method correctly handles all edge cases:

  • Returns None when logs don't exist (avoiding undefined variable issues)
  • Uses state tracking to ensure logs are processed only once
  • Properly manages consumption/production direction based on timestamp equality
  • Resets state flags when logs are no longer available

The logic for energy direction detection is sound and well-implemented.


547-572: LGTM! Consistent and well-implemented helper method.

The _check_next_production method maintains excellent consistency with its counterpart _check_prev_production. It properly:

  • Handles missing logs by returning None
  • Uses state tracking with _first_next_log_processed flag
  • Implements correct energy direction logic for next log timestamps
  • Maintains code symmetry for better maintainability

The implementation addresses all the issues identified in previous reviews and provides a robust solution for production pulse detection.

@sonarqubecloud
Copy link

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.

Actionable comments posted: 0

♻️ Duplicate comments (1)
plugwise_usb/network/__init__.py (1)

544-570: Excellent implementation with comprehensive input validation.

The method addresses the previous review feedback by implementing proper input validation:

  • Ensures consumption interval is positive
  • Ensures production interval is non-negative
  • Validates production is a multiple of consumption when non-zero
  • Proper error handling and response validation

The implementation follows established patterns and integrates well with the existing codebase.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 553-558: plugwise_usb/network/init.py#L553-L558
Added lines #L553 - L558 were not covered by tests


[warning] 560-561: plugwise_usb/network/init.py#L560-L561
Added lines #L560 - L561 were not covered by tests


[warning] 564-565: plugwise_usb/network/init.py#L564-L565
Added lines #L564 - L565 were not covered by tests


[warning] 567-568: plugwise_usb/network/init.py#L567-L568
Added lines #L567 - L568 were not covered by tests

🧹 Nitpick comments (1)
plugwise_usb/messages/requests.py (1)

1267-1267: Fix trailing whitespace.

Remove the trailing whitespace on line 1267 as flagged by static analysis.

-    Response message: NodeResponse with ack-type POWER_LOG_INTERVAL_ACCEPTED 
+    Response message: NodeResponse with ack-type POWER_LOG_INTERVAL_ACCEPTED
🧰 Tools
🪛 Ruff (0.11.9)

1267-1267: Trailing whitespace

Remove trailing whitespace

(W291)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 36aef6f and a969a31.

📒 Files selected for processing (9)
  • CHANGELOG.md (1 hunks)
  • plugwise_usb/__init__.py (1 hunks)
  • plugwise_usb/messages/requests.py (2 hunks)
  • plugwise_usb/messages/responses.py (1 hunks)
  • plugwise_usb/network/__init__.py (2 hunks)
  • plugwise_usb/nodes/helpers/pulses.py (4 hunks)
  • pyproject.toml (1 hunks)
  • scripts/tests_and_coverage.sh (1 hunks)
  • tests/test_usb.py (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • CHANGELOG.md
  • pyproject.toml
  • tests/test_usb.py
  • plugwise_usb/messages/responses.py
🧰 Additional context used
🧬 Code Graph Analysis (4)
plugwise_usb/__init__.py (2)
plugwise_usb/network/__init__.py (1)
  • set_energy_intervals (544-570)
plugwise_usb/exceptions.py (2)
  • MessageError (20-21)
  • NodeError (24-25)
plugwise_usb/messages/requests.py (4)
tests/test_usb.py (1)
  • send (252-258)
plugwise_usb/connection/__init__.py (1)
  • send (231-242)
plugwise_usb/messages/responses.py (1)
  • NodeResponse (243-270)
plugwise_usb/exceptions.py (1)
  • MessageError (20-21)
plugwise_usb/network/__init__.py (5)
plugwise_usb/messages/requests.py (18)
  • CirclePlusAllowJoiningRequest (454-485)
  • CircleMeasureIntervalRequest (1262-1293)
  • send (365-374)
  • send (387-396)
  • send (438-440)
  • send (476-485)
  • send (512-521)
  • send (542-553)
  • send (566-575)
  • send (588-597)
  • send (621-630)
  • send (667-676)
  • send (719-728)
  • send (775-784)
  • send (808-817)
  • send (848-857)
  • send (880-889)
  • response (125-129)
plugwise_usb/__init__.py (2)
  • set_energy_intervals (213-221)
  • name (141-143)
plugwise_usb/connection/__init__.py (1)
  • send (231-242)
plugwise_usb/exceptions.py (2)
  • NodeError (24-25)
  • MessageError (20-21)
plugwise_usb/messages/responses.py (3)
  • response_type (236-240)
  • response_type (266-270)
  • NodeResponseType (56-72)
plugwise_usb/nodes/helpers/pulses.py (1)
plugwise_usb/nodes/helpers/counter.py (3)
  • timestamp (170-178)
  • direction (209-211)
  • is_consumption (229-231)
🪛 GitHub Check: codecov/patch
plugwise_usb/__init__.py

[warning] 217-221: plugwise_usb/init.py#L217-L221
Added lines #L217 - L221 were not covered by tests

plugwise_usb/messages/requests.py

[warning] 1286-1291: plugwise_usb/messages/requests.py#L1286-L1291
Added lines #L1286 - L1291 were not covered by tests

plugwise_usb/network/__init__.py

[warning] 553-558: plugwise_usb/network/init.py#L553-L558
Added lines #L553 - L558 were not covered by tests


[warning] 560-561: plugwise_usb/network/init.py#L560-L561
Added lines #L560 - L561 were not covered by tests


[warning] 564-565: plugwise_usb/network/init.py#L564-L565
Added lines #L564 - L565 were not covered by tests


[warning] 567-568: plugwise_usb/network/init.py#L567-L568
Added lines #L567 - L568 were not covered by tests

plugwise_usb/nodes/helpers/pulses.py

[warning] 537-540: plugwise_usb/nodes/helpers/pulses.py#L537-L540
Added lines #L537 - L540 were not covered by tests


[warning] 542-542: plugwise_usb/nodes/helpers/pulses.py#L542
Added line #L542 was not covered by tests


[warning] 567-568: plugwise_usb/nodes/helpers/pulses.py#L567-L568
Added lines #L567 - L568 were not covered by tests

🪛 Ruff (0.11.9)
plugwise_usb/messages/requests.py

1267-1267: Trailing whitespace

Remove trailing whitespace

(W291)

🔇 Additional comments (10)
scripts/tests_and_coverage.sh (1)

26-27: Clarify the intent behind removing coverage collection.

The change removes coverage collection from the test execution. Is this intentional for debugging purposes, or should coverage collection be retained?

Consider whether this change aligns with your testing and CI/CD requirements.

plugwise_usb/__init__.py (1)

213-221: LGTM! Clean implementation of the energy intervals configuration.

The method properly delegates to the network layer with appropriate exception handling and chaining. The implementation follows the established patterns in the codebase.

Note: Static analysis indicates this code lacks test coverage, but the functionality appears correct.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 217-221: plugwise_usb/init.py#L217-L221
Added lines #L217 - L221 were not covered by tests

plugwise_usb/messages/requests.py (1)

1284-1293: LGTM! The send method follows established patterns.

The implementation correctly validates the response type and handles error cases appropriately, consistent with other request classes in the module.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 1286-1291: plugwise_usb/messages/requests.py#L1286-L1291
Added lines #L1286 - L1291 were not covered by tests

plugwise_usb/network/__init__.py (1)

17-21: LGTM! Import statement correctly updated.

The addition of CircleMeasureIntervalRequest to the imports is necessary for the new functionality.

plugwise_usb/nodes/helpers/pulses.py (6)

91-92: LGTM! Good state tracking enhancement.

The addition of _first_next_log_processed and _first_prev_log_processed flags improves the robustness of the direction detection logic by ensuring adjacent logs are processed only once per invocation.


382-383: LGTM! Explicit return improves code clarity.

Adding the explicit return False statement makes the function's behavior more predictable and follows good defensive programming practices.


446-446: LGTM! Enhanced debug logging.

Adding the direction parameter to the debug logging provides valuable context for troubleshooting pulse direction detection issues.


512-520: LGTM! Well-structured refactoring.

The delegation to helper methods _check_prev_production and _check_next_production improves code organization and maintainability. The logic correctly implements the business rule where production is detected when 2 out of 3 consecutive slots have the same timestamp.


549-575:

❓ Verification inconclusive

LGTM with test coverage improvement needed.

The _check_next_production method mirrors the structure of _check_prev_production and is correctly implemented with proper state tracking and error handling for the next slot processing.

Static analysis indicates test coverage gaps for lines 567-568. Please ensure comprehensive unit tests cover all code paths in this method, particularly the conditional branches for different _log_production states.


🏁 Script executed:

#!/bin/bash
# Check if there are existing tests for the next production logic
rg -A 10 -B 5 "_check_next_production|next.*production" tests/

Length of output: 64


Ensure test coverage for _check_next_production

I wasn’t able to find any existing tests that exercise this new helper, and static analysis shows lines 567–568 remain untested. Please add unit tests covering all branches in this method, including:

  • Next log exists vs. missing
  • _first_next_log_processed transitions
  • All combinations of timestamp equality and _log_production being True, False, or None

File to cover:
• plugwise_usb/nodes/helpers/pulses.py (lines 549–575)

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 567-568: plugwise_usb/nodes/helpers/pulses.py#L567-L568
Added lines #L567 - L568 were not covered by tests


521-547:

❓ Verification inconclusive

LGTM with test coverage improvement needed.

The _check_prev_production method is well-implemented with proper state tracking and error handling. The logic correctly processes the previous slot only once per invocation and handles missing logs appropriately.

However, static analysis indicates test coverage gaps for lines 537-540 and 542. Please add unit tests to cover the edge cases in this method, particularly:

  • When _log_production is True but not None
  • When _log_production is None

🏁 Script executed:

#!/bin/bash
# Check current test coverage for this method
rg -A 10 -B 5 "_check_prev_production" tests/

Length of output: 45


Add unit tests for _check_prev_production edge cases

I wasn’t able to find any existing tests that directly exercise this helper. Please ensure that all code paths in _check_prev_production are covered, in particular:

  • When there is no previous log (_first_prev_log_processed reset path)
  • When _log_production is initially None and set to False
  • When _log_production is True and the previous log’s is_consumption flag is toggled
  • When a duplicate timestamp is detected on the second log (both logs’ is_consumption flags switched)

Once you’ve added those tests, verify via your coverage report that lines 537–540 and 542 in plugwise_usb/nodes/helpers/pulses.py are exercised.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 537-540: plugwise_usb/nodes/helpers/pulses.py#L537-L540
Added lines #L537 - L540 were not covered by tests


[warning] 542-542: plugwise_usb/nodes/helpers/pulses.py#L542
Added line #L542 was not covered by tests

@bouwew bouwew merged commit 46b8a0c into main May 31, 2025
27 of 29 checks passed
@bouwew bouwew deleted the enable_prod_2 branch May 31, 2025 13:36
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.

2 participants