Skip to content

Conversation

@bouwew
Copy link
Contributor

@bouwew bouwew commented Sep 16, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Standardized a device type label from “Circle+” to “Circle” in cached data and outputs for consistent naming.
    • Cache no longer persists low-priority device entries, reducing clutter and improving reliability.
  • Tests

    • Updated test data and expectations to reflect the standardized device type and the new cache filtering behavior.
  • Chores

    • Refined internal cache handling without changing public APIs.

@coderabbitai
Copy link

coderabbitai bot commented Sep 16, 2025

Warning

Rate limit exceeded

@bouwew has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 6 minutes and 34 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between ab945ec and f86812c.

📒 Files selected for processing (2)
  • plugwise_usb/network/cache.py (2 hunks)
  • tests/test_usb.py (1 hunks)

Walkthrough

The cache save logic now filters node types to include only entries with node_type.value > 1 before persisting. Tests were updated to treat node 0123456789ABCDEF as NodeType.CIRCLE instead of CIRCLE_PLUS across restore and write expectations. A changelog entry for PR 344 was added. No public API changes.

Changes

Cohort / File(s) Change summary
Cache filtering in save_cache
plugwise_usb/network/cache.py
Modified save_cache to persist only nodetypes where node_type.value > 1; logging and write_cache call with rewrite=True remain unchanged.
Tests updated for NodeType adjustments
tests/test_usb.py
Updated fixtures and expectations: replaced NodeType.CIRCLE_PLUS with NodeType.CIRCLE for node 0123456789ABCDEF across restore/write tests and adjusted expected serialized cache lines and parsed mappings.
Changelog
CHANGELOG.md
Added entry under Ongoing for PR 344: "Don't store plus-device in nodetypes cache".

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Caller
  participant Cache as Cache.save_cache
  participant Store as write_cache

  Caller->>Cache: save_cache()
  Note over Cache: Build mapping from self._nodetypes
  Cache->>Cache: Filter mapping to entries where node_type.value > 1
  Note right of Cache: omit entries with value ≤ 1
  Cache->>Store: write_cache(filtered_map, rewrite=True)
  Store-->>Caller: Completed
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • dirixmjm

Poem

A nibble of bytes, a cache I trim,
I hop past nodes whose values are slim.
Circles not plus, the tests realign,
Carrots of logic, crisp and fine.
With filtered fields, I thump “all set!” 🥕🐇

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly and accurately summarizes the main change: excluding the "plus" device from the nodetypes cache. The diff, test updates, and changelog entry all reflect this intent, so the title is both relevant and clear to reviewers.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.

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.

@codecov
Copy link

codecov bot commented Sep 16, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.67%. Comparing base (a3ed4d9) to head (f86812c).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #344      +/-   ##
==========================================
- Coverage   81.76%   81.67%   -0.09%     
==========================================
  Files          36       36              
  Lines        8142     8142              
==========================================
- Hits         6657     6650       -7     
- Misses       1485     1492       +7     

☔ 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: 0

🧹 Nitpick comments (3)
plugwise_usb/network/cache.py (2)

30-31: Filter explicitly for CIRCLE_PLUS, not by ordinal.

node_type.value > 1 also drops STICK and relies on enum ordinals. Be explicit to match the PR intent.

Apply:

-        cache_data_to_save: dict[str, str] = {
-            mac: node_type.name for mac, node_type in self._nodetypes.items() if node_type.value > 1
-        }
+        cache_data_to_save: dict[str, str] = {
+            mac: node_type.name
+            for mac, node_type in self._nodetypes.items()
+            if node_type is not NodeType.CIRCLE_PLUS
+        }

58-63: Optional: ignore CIRCLE_PLUS on restore (backward‑compat).

Prevents lingering Plus entries in memory from legacy cache files and aligns behavior across read/write.

Apply:

             if node_type is None:
                 _LOGGER.warning(
                     "Invalid NodeType in cache for mac %s: %s", mac, node_value
                 )
                 continue
+            if node_type is NodeType.CIRCLE_PLUS:
+                _LOGGER.debug("Skipping CIRCLE_PLUS entry for %s (not cached by design)", mac)
+                continue
             self._nodetypes[mac] = node_type
tests/test_usb.py (1)

1718-1726: Optional: add regression for legacy CIRCLE_PLUS lines.

If you adopt “skip on restore,” add a line with NodeType.CIRCLE_PLUS to mock_read_data and assert it’s ignored.

Example tweak:

-        mock_read_data = [
-            "0123456789ABCDEF;NodeType.CIRCLE",
-            "FEDCBA9876543210;CIRCLE",
-            "1298347650AFBECD;6",
-        ]
+        mock_read_data = [
+            "0123456789ABCDEF;NodeType.CIRCLE_PLUS",  # legacy cache entry
+            "FEDCBA9876543210;CIRCLE",
+            "1298347650AFBECD;6",
+        ]
...
-            assert pw_nw_cache.nodetypes == {
-                "0123456789ABCDEF": pw_api.NodeType.CIRCLE,
-                "FEDCBA9876543210": pw_api.NodeType.CIRCLE,
-                "1298347650AFBECD": pw_api.NodeType.SCAN,
-            }
+            assert pw_nw_cache.nodetypes == {
+                "FEDCBA9876543210": pw_api.NodeType.CIRCLE,
+                "1298347650AFBECD": pw_api.NodeType.SCAN,
+            }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a3ed4d9 and 0ce3abe.

📒 Files selected for processing (2)
  • plugwise_usb/network/cache.py (1 hunks)
  • tests/test_usb.py (2 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: bouwew
PR: plugwise/python-plugwise-usb#329
File: plugwise_usb/nodes/circle.py:0-0
Timestamp: 2025-08-30T10:46:42.649Z
Learning: In plugwise_usb/nodes/circle.py, the save_cache() method should only be called when both cache_updated AND save_cache parameters are True. If no cache updates occurred (cache_updated = False), there's no meaningful data to persist to disk, even if the caller requested saving (save_cache = True). This prevents unnecessary disk I/O operations.
📚 Learning: 2025-08-30T10:46:42.649Z
Learnt from: bouwew
PR: plugwise/python-plugwise-usb#329
File: plugwise_usb/nodes/circle.py:0-0
Timestamp: 2025-08-30T10:46:42.649Z
Learning: In plugwise_usb/nodes/circle.py, the save_cache() method should only be called when both cache_updated AND save_cache parameters are True. If no cache updates occurred (cache_updated = False), there's no meaningful data to persist to disk, even if the caller requested saving (save_cache = True). This prevents unnecessary disk I/O operations.

Applied to files:

  • plugwise_usb/network/cache.py
🧬 Code graph analysis (2)
tests/test_usb.py (3)
plugwise_usb/network/cache.py (2)
  • restore_cache (42-68)
  • nodetypes (23-25)
plugwise_usb/nodes/helpers/cache.py (1)
  • restore_cache (53-59)
plugwise_usb/api.py (1)
  • NodeType (62-73)
plugwise_usb/network/cache.py (1)
plugwise_usb/messages/responses.py (1)
  • node_type (634-636)
🔇 Additional comments (3)
tests/test_usb.py (3)

1707-1716: LGTM: test data now treats Plus as Circle in cache inputs.

Matches the “don’t store Plus in nodetypes cache” policy and the new save filter behavior.


1720-1732: LGTM: restore accepts enum names and numeric values consistently.

Expectations for CIRCLE and SCAN map correctly after restore.

Also applies to: 1729-1732


1739-1746: LGTM: expected write excludes Plus and preserves order.

Write assertions reflect filtered cache (CIRCLE only) and stable insertion ordering.

@bouwew bouwew marked this pull request as ready for review September 16, 2025 14:00
@bouwew bouwew requested a review from a team as a code owner September 16, 2025 14:00
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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
plugwise_usb/network/cache.py (1)

60-66: Also skip excluded types when restoring legacy cache files (prevents stale Plus entries in memory).

If an older cache on disk contains CIRCLE_PLUS, it will currently be restored into _nodetypes until the next save. Drop it during restore to align runtime state with the new persistence rule.

Apply this diff:

@@
-            if node_type is None:
+            if node_type is None:
                 _LOGGER.warning(
                     "Invalid NodeType in cache for mac %s: %s", mac, node_value
                 )
                 continue
+            if node_type in _EXCLUDED_CACHE_TYPES:
+                _LOGGER.debug(
+                    "Skipping excluded NodeType %s for mac %s during restore",
+                    str(node_type),
+                    mac,
+                )
+                continue
🧹 Nitpick comments (2)
plugwise_usb/network/cache.py (2)

30-32: Replace magic threshold with explicit enum exclusions (future‑proof, self‑documenting).

Relying on node_type.value > 1 is brittle if enum values change/reorder. Filter by named members instead.

Apply this diff:

@@
-_LOGGER = logging.getLogger(__name__)
-_NETWORK_CACHE_FILE_NAME = "nodetype.cache"
+_LOGGER = logging.getLogger(__name__)
+_NETWORK_CACHE_FILE_NAME = "nodetype.cache"
+
+# NodeTypes we never persist to disk (e.g., the Plus device and, optionally, UNKNOWN)
+# Extend this set as needed rather than using numeric thresholds.
+_EXCLUDED_CACHE_TYPES: set[NodeType] = {
+    NodeType.CIRCLE_PLUS,
+    # Add below if desired:
+    # NodeType.UNKNOWN,
+}
@@
-        cache_data_to_save: dict[str, str] = {
-            mac: node_type.name
-            for mac, node_type in self._nodetypes.items()
-            if node_type.value > 1
-        }
+        cache_data_to_save: dict[str, str] = {
+            mac: node_type.name
+            for mac, node_type in self._nodetypes.items()
+            if node_type not in _EXCLUDED_CACHE_TYPES
+        }

72-85: Don't retain excluded NodeType values in the in‑memory cache (exclude CIRCLE_PLUS)

NodeType.CIRCLE_PLUS is defined in plugwise_usb/api.py and referenced by tests and plugwise_usb/nodes/init.py; in plugwise_usb/network/cache.py.update_nodetypes add an early return when node_type is an excluded value — use _EXCLUDED_CACHE_TYPES if present or define it as {NodeType.CIRCLE_PLUS} and log a debug message before returning.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0ce3abe and 4692c6a.

📒 Files selected for processing (1)
  • plugwise_usb/network/cache.py (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: bouwew
PR: plugwise/python-plugwise-usb#329
File: plugwise_usb/nodes/circle.py:0-0
Timestamp: 2025-08-30T10:46:42.649Z
Learning: In plugwise_usb/nodes/circle.py, the save_cache() method should only be called when both cache_updated AND save_cache parameters are True. If no cache updates occurred (cache_updated = False), there's no meaningful data to persist to disk, even if the caller requested saving (save_cache = True). This prevents unnecessary disk I/O operations.
📚 Learning: 2025-08-30T10:46:42.649Z
Learnt from: bouwew
PR: plugwise/python-plugwise-usb#329
File: plugwise_usb/nodes/circle.py:0-0
Timestamp: 2025-08-30T10:46:42.649Z
Learning: In plugwise_usb/nodes/circle.py, the save_cache() method should only be called when both cache_updated AND save_cache parameters are True. If no cache updates occurred (cache_updated = False), there's no meaningful data to persist to disk, even if the caller requested saving (save_cache = True). This prevents unnecessary disk I/O operations.

Applied to files:

  • plugwise_usb/network/cache.py
🧬 Code graph analysis (1)
plugwise_usb/network/cache.py (2)
plugwise_usb/nodes/node.py (1)
  • mac (235-237)
plugwise_usb/messages/responses.py (1)
  • node_type (634-636)

This reverts commit 0ce3abe.
@sonarqubecloud
Copy link

Copy link
Contributor

@dirixmjm dirixmjm left a comment

Choose a reason for hiding this comment

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

As discussed, makes sense!

@bouwew bouwew merged commit 12a1770 into main Sep 17, 2025
16 of 17 checks passed
@bouwew bouwew deleted the limit-nodetypes-in-cache branch September 17, 2025 09:14
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.

3 participants