Skip to content

Commit 05780f4

Browse files
committed
Update node_type cache:
- Save NodeType name to file - Read all previously used string-formats from cache-file - Fix CIRCLE_PLUS not appearing in cache-file - Make sure to rewrite the full contents at every write-to-file
1 parent 849820c commit 05780f4

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

plugwise_usb/network/cache.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ async def save_cache(self) -> None:
2828
"""Save the node information to file."""
2929
cache_data_to_save: dict[str, str] = {}
3030
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)
31+
cache_data_to_save[mac] = node_type.name
32+
_LOGGER.debug("Save NodeTypes for %s Nodes", str(len(cache_data_to_save)))
33+
await self.write_cache(
34+
cache_data_to_save, True
35+
) # rewrite set to True is required
3536

3637
async def clear_cache(self) -> None:
3738
"""Clear current cache."""
@@ -44,11 +45,16 @@ async def restore_cache(self) -> None:
4445
self._nodetypes = {}
4546
for mac, node_value in data.items():
4647
node_type: NodeType | None = None
47-
if len(node_value) >= 10:
48+
# Backward-compatible parsing: support full enums, enum names, or numeric values
49+
val = node_value.strip()
50+
key = val.split(".", 1)[1] if val.startswith("NodeType.") else val
51+
node_type = NodeType.__members__.get(key) # e.g., "CIRCLE"
52+
if node_type is None and val.isdigit(): # e.g., "2"
4853
try:
49-
node_type = NodeType[node_value[9:]]
50-
except KeyError:
54+
node_type = NodeType(int(val))
55+
except ValueError:
5156
node_type = None
57+
5258
if node_type is None:
5359
_LOGGER.warning("Invalid NodeType in cache: %s", node_value)
5460
continue

0 commit comments

Comments
 (0)