@@ -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
0 commit comments