Skip to content

Commit 8ba0ea1

Browse files
committed
separate load_scan_from_cache from basenode load_from_cache as it already provides defaults values on cache-misses, update default accordingly
1 parent 5cc68f9 commit 8ba0ea1

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

plugwise_usb/nodes/node.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,12 @@ def _get_cache(self, setting: str) -> str | None:
662662
return None
663663
return self._node_cache.get_state(setting)
664664

665+
def _get_cache_as_bool(self, setting: str) -> bool | None:
666+
"""Retrieve value of specified setting from cache memory and return it as bool."""
667+
if (value := self._get_cache(setting)) is not None:
668+
return bool(value)
669+
return None
670+
665671
def _get_cache_as_datetime(self, setting: str) -> datetime | None:
666672
"""Retrieve value of specified setting from cache memory and return it as datetime object."""
667673
if (timestamp_str := self._get_cache(setting)) is not None:

plugwise_usb/nodes/scan.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ async def load(self) -> None:
107107

108108
_LOGGER.debug("Loading Scan node %s", self._node_info.mac)
109109
await super().load()
110+
if (
111+
await self._load_scan_from_cache()
112+
and not self._scan_config_task_scheduled
113+
):
114+
await self.schedule_task_when_awake(self._configure_scan_task())
115+
self._scan_config_task_scheduled = True
110116

111117
self._setup_protocol(SCAN_FIRMWARE_SUPPORT, SCAN_FEATURES)
112118
await self.initialize()
@@ -134,31 +140,15 @@ async def unload(self) -> None:
134140
# region Caching
135141
async def _load_defaults(self) -> None:
136142
"""Load default configuration settings."""
137-
await super()._load_defaults()
138-
self._motion_state = MotionState(
139-
state=DEFAULT_MOTION_STATE,
140-
timestamp=None,
141-
)
142-
self._motion_config = MotionConfig(
143-
reset_timer=DEFAULT_RESET_TIMER,
144-
daylight_mode=DEFAULT_DAYLIGHT_MODE,
145-
sensitivity_level=DEFAULT_SENSITIVITY,
146-
dirty=True,
147-
)
148-
await self._scan_configure_update()
149-
await self.schedule_task_when_awake(self._configure_scan_task())
150-
self._scan_config_task_scheduled = True
151143
if self._node_info.model is None:
152144
self._node_info.model = "Scan"
153145
if self._node_info.name is None:
154146
self._node_info.name = f"Scan {self._node_info.mac[-5:]}"
155147
if self._node_info.firmware is None:
156148
self._node_info.firmware = DEFAULT_FIRMWARE
157149

158-
async def _load_from_cache(self) -> bool:
150+
async def _load_scan_from_cache(self) -> bool:
159151
"""Load states from previous cached information. Returns True if successful."""
160-
if not await super()._load_from_cache():
161-
return False
162152
self._motion_state = MotionState(
163153
state=self._motion_from_cache(),
164154
timestamp=self._motion_timestamp_from_cache(),
@@ -173,29 +163,31 @@ async def _load_from_cache(self) -> bool:
173163
if (sensitivity_level := self._sensitivity_level_from_cache()) is None:
174164
dirty = True
175165
sensitivity_level = DEFAULT_SENSITIVITY
176-
dirty &= self._motion_config_dirty_from_cache()
166+
dirty |= self._motion_config_dirty_from_cache()
177167

178168
self._motion_config = MotionConfig(
179169
daylight_mode=daylight_mode,
180170
reset_timer=reset_timer,
181171
sensitivity_level=sensitivity_level,
182172
dirty=dirty,
183173
)
184-
return True
174+
return dirty
185175

186176
def _daylight_mode_from_cache(self) -> bool | None:
187177
"""Load awake duration from cache."""
188-
if (daylight_mode := self._get_cache(CACHE_CONFIG_DAYLIGHT_MODE)) is not None:
189-
if daylight_mode == "True":
190-
return True
191-
return False
178+
if (
179+
daylight_mode := self._get_cache_as_bool(CACHE_CONFIG_DAYLIGHT_MODE)
180+
) is not None:
181+
return daylight_mode
192182
return None
193183

194184
def _motion_from_cache(self) -> bool:
195185
"""Load motion state from cache."""
196-
if (cached_motion_state := self._get_cache(CACHE_MOTION_STATE)) is not None:
186+
if (
187+
cached_motion_state := self._get_cache_as_bool(CACHE_MOTION_STATE)
188+
) is not None:
197189
if (
198-
cached_motion_state == "True"
190+
cached_motion_state
199191
and (motion_timestamp := self._motion_timestamp_from_cache())
200192
is not None
201193
and int((datetime.now(tz=UTC) - motion_timestamp).total_seconds())
@@ -219,7 +211,7 @@ def _sensitivity_level_from_cache(self) -> MotionSensitivity | None:
219211

220212
def _motion_config_dirty_from_cache(self) -> bool:
221213
"""Load dirty from cache."""
222-
if (dirty := self._get_cache(CACHE_CONFIG_DIRTY)) is not None:
214+
if (dirty := self._get_cache_as_bool(CACHE_CONFIG_DIRTY)) is not None:
223215
return dirty
224216
return True
225217

tests/test_usb.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,8 @@ def fake_cache(dummy: object, setting: str) -> str | None: # noqa: PLR0911 PLR0
21352135
return "MEDIUM"
21362136
if setting == pw_scan.CACHE_CONFIG_DAYLIGHT_MODE:
21372137
return "True"
2138+
if setting == pw_scan.CACHE_CONFIG_DIRTY:
2139+
return "False"
21382140
return None
21392141

21402142
monkeypatch.setattr(pw_node.PlugwiseBaseNode, "_get_cache", fake_cache)
@@ -2178,7 +2180,6 @@ async def load_callback(event: pw_api.NodeEvent, mac: str) -> None: # type: ign
21782180
with pytest.raises(ValueError):
21792181
assert await test_scan.set_motion_reset_timer(256)
21802182
assert not await test_scan.set_motion_reset_timer(10)
2181-
assert test_scan.scan_config_task_scheduled
21822183
assert await test_scan.set_motion_reset_timer(15)
21832184
assert test_scan.scan_config_task_scheduled
21842185
assert test_scan.reset_timer == 15

0 commit comments

Comments
 (0)