Skip to content

Commit 26d5994

Browse files
authored
Merge pull request #342 from plugwise/improve_node_type_cache
Improve node_type caching
2 parents 849820c + 0ffd05c commit 26d5994

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Ongoing
4+
5+
- PR [342](https://github.com/plugwise/python-plugwise-usb/pull/342): Improve node_type chaching.
6+
37
## 0.46.0 - 2025-09-12
48

59
- PR [338](https://github.com/plugwise/python-plugwise-usb/pull/338): Append report interval to Sense node configuration

plugwise_usb/network/cache.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ def nodetypes(self) -> dict[str, NodeType]:
2626

2727
async def save_cache(self) -> None:
2828
"""Save the node information to file."""
29-
cache_data_to_save: dict[str, str] = {}
30-
for mac, node_type in self._nodetypes.items():
31-
node_value = str(node_type)
32-
cache_data_to_save[mac] = node_value
33-
_LOGGER.debug("Save NodeTypes %s", str(len(cache_data_to_save)))
34-
await self.write_cache(cache_data_to_save)
29+
cache_data_to_save: dict[str, str] = {
30+
mac: node_type.name for mac, node_type in self._nodetypes.items()
31+
}
32+
_LOGGER.debug("Save NodeTypes for %s Nodes", len(cache_data_to_save))
33+
await self.write_cache(cache_data_to_save, rewrite=True) # Make sure the cache-contents is actual
3534

3635
async def clear_cache(self) -> None:
3736
"""Clear current cache."""
@@ -44,13 +43,18 @@ async def restore_cache(self) -> None:
4443
self._nodetypes = {}
4544
for mac, node_value in data.items():
4645
node_type: NodeType | None = None
47-
if len(node_value) >= 10:
46+
# Backward-compatible parsing: support full enums, enum names, or numeric values
47+
val = node_value.strip()
48+
key = (val.split(".", 1)[1] if val.startswith("NodeType.") else val).upper()
49+
node_type = NodeType.__members__.get(key) # e.g., "CIRCLE"
50+
if node_type is None:
4851
try:
49-
node_type = NodeType[node_value[9:]]
50-
except KeyError:
52+
node_type = NodeType(int(val))
53+
except ValueError:
5154
node_type = None
55+
5256
if node_type is None:
53-
_LOGGER.warning("Invalid NodeType in cache: %s", node_value)
57+
_LOGGER.warning("Invalid NodeType in cache for mac %s: %s", mac, node_value)
5458
continue
5559
self._nodetypes[mac] = node_type
5660
_LOGGER.debug(
@@ -86,5 +90,9 @@ async def prune_cache(self, registry: list[str]) -> None:
8690
continue
8791
if (node_type := self.get_nodetype(mac)) is not None:
8892
new_nodetypes[mac] = node_type
93+
94+
if new_nodetypes == self._nodetypes:
95+
_LOGGER.debug("prune_cache: no changes; skipping save")
96+
return
8997
self._nodetypes = new_nodetypes
9098
await self.save_cache()

tests/test_usb.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,9 +1712,9 @@ async def makedirs(cache_dir: str, exist_ok: bool) -> None:
17121712

17131713
# test with valid data
17141714
mock_read_data = [
1715-
"0123456789ABCDEF;NodeType.CIRCLE_PLUS",
1715+
"0123456789ABCDEF;CIRCLE_PLUS",
17161716
"FEDCBA9876543210;NodeType.CIRCLE",
1717-
"1298347650AFBECD;NodeType.SCAN",
1717+
"1298347650AFBECD;6",
17181718
]
17191719
file_chunks_iter = iter(mock_read_data)
17201720
mock_file_stream = MagicMock(readlines=lambda *args, **kwargs: file_chunks_iter)
@@ -1733,10 +1733,10 @@ async def makedirs(cache_dir: str, exist_ok: bool) -> None:
17331733
)
17341734
mock_file_stream.writelines.assert_called_with(
17351735
[
1736-
"0123456789ABCDEF;NodeType.CIRCLE_PLUS\n",
1737-
"FEDCBA9876543210;NodeType.CIRCLE\n",
1738-
"1298347650AFBECD;NodeType.SCAN\n",
1739-
"1234ABCD4321FEDC;NodeType.STEALTH\n",
1736+
"0123456789ABCDEF;CIRCLE_PLUS\n",
1737+
"FEDCBA9876543210;CIRCLE\n",
1738+
"1298347650AFBECD;SCAN\n",
1739+
"1234ABCD4321FEDC;STEALTH\n",
17401740
]
17411741
)
17421742
assert pw_nw_cache.nodetypes == {

0 commit comments

Comments
 (0)