38
38
REQUIREMENTS_FILE ,
39
39
SERVICE_JUPYTER_KERNEL_START ,
40
40
UNSUB_LISTENERS ,
41
- WATCHDOG_RUN ,
42
41
WATCHDOG_TASK ,
43
42
)
44
43
from .eval import AstEval
@@ -156,12 +155,29 @@ def __init__(
156
155
self .watchdog_q = watchdog_q
157
156
self ._observer = observer
158
157
self ._observer .schedule (self , path , recursive = True )
158
+ if not hass .is_running :
159
+ hass .bus .listen_once (EVENT_HOMEASSISTANT_STARTED , self .startup )
160
+ else :
161
+ self .startup (None )
162
+
163
+ hass .bus .listen_once (EVENT_HOMEASSISTANT_STOP , self .shutdown )
164
+ _LOGGER .debug ("watchdog init path=%s" , path )
165
+
166
+ def startup (self , event : Event | None ) -> None :
167
+ """Start the observer."""
168
+ _LOGGER .debug ("watchdog startup" )
169
+ self ._observer .start ()
170
+
171
+ def shutdown (self , event : Event | None ) -> None :
172
+ """Stop the observer."""
173
+ self ._observer .stop ()
174
+ self ._observer .join ()
175
+ _LOGGER .debug ("watchdog shutdown" )
159
176
160
177
def process (self , event : FileSystemEvent ) -> None :
161
178
"""Send watchdog events to main loop task."""
162
- if hass .data [DOMAIN ].get (WATCHDOG_RUN , False ):
163
- _LOGGER .debug ("watchdog process(%s)" , event )
164
- hass .loop .call_soon_threadsafe (self .watchdog_q .put_nowait , event )
179
+ _LOGGER .debug ("watchdog process(%s)" , event )
180
+ hass .loop .call_soon_threadsafe (self .watchdog_q .put_nowait , event )
165
181
166
182
def on_modified (self , event : FileSystemEvent ) -> None :
167
183
"""File modified."""
@@ -197,7 +213,7 @@ def check_event(event, do_reload: bool) -> bool:
197
213
try :
198
214
#
199
215
# since some file/dir changes create multiple events, we consume all
200
- # events in a small window; first # wait indefinitely for next event
216
+ # events in a small window; first wait indefinitely for next event
201
217
#
202
218
do_reload = check_event (await watchdog_q .get (), False )
203
219
#
@@ -223,10 +239,10 @@ def check_event(event, do_reload: bool) -> bool:
223
239
observer = watchdog .observers .Observer ()
224
240
if observer is not None :
225
241
# don't run watchdog when we are testing (Observer() patches to None)
226
- hass .data [DOMAIN ][WATCHDOG_RUN ] = False
227
242
hass .data [DOMAIN ][WATCHDOG_TASK ] = Function .create_task (task_watchdog (watchdog_q ))
228
243
229
244
await hass .async_add_executor_job (WatchDogHandler , watchdog_q , observer , pyscript_folder )
245
+ _LOGGER .debug ("watchdog started job and task folder=%s" , pyscript_folder )
230
246
231
247
232
248
async def async_setup_entry (hass : HomeAssistant , config_entry : ConfigEntry ) -> bool :
@@ -336,12 +352,9 @@ async def hass_started(event: HAEvent) -> None:
336
352
await State .get_service_params ()
337
353
hass .data [DOMAIN ][UNSUB_LISTENERS ].append (hass .bus .async_listen (EVENT_STATE_CHANGED , state_changed ))
338
354
start_global_contexts ()
339
- if WATCHDOG_RUN in hass .data [DOMAIN ]:
340
- hass .data [DOMAIN ][WATCHDOG_RUN ] = True
341
355
342
356
async def hass_stop (event : HAEvent ) -> None :
343
- if WATCHDOG_RUN in hass .data [DOMAIN ]:
344
- hass .data [DOMAIN ][WATCHDOG_RUN ] = False
357
+ if WATCHDOG_TASK in hass .data [DOMAIN ]:
345
358
Function .reaper_cancel (hass .data [DOMAIN ][WATCHDOG_TASK ])
346
359
del hass .data [DOMAIN ][WATCHDOG_TASK ]
347
360
0 commit comments