3838 REQUIREMENTS_FILE ,
3939 SERVICE_JUPYTER_KERNEL_START ,
4040 UNSUB_LISTENERS ,
41- WATCHDOG_RUN ,
4241 WATCHDOG_TASK ,
4342)
4443from .eval import AstEval
@@ -156,12 +155,29 @@ def __init__(
156155 self .watchdog_q = watchdog_q
157156 self ._observer = observer
158157 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" )
159176
160177 def process (self , event : FileSystemEvent ) -> None :
161178 """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 )
165181
166182 def on_modified (self , event : FileSystemEvent ) -> None :
167183 """File modified."""
@@ -197,7 +213,7 @@ def check_event(event, do_reload: bool) -> bool:
197213 try :
198214 #
199215 # 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
201217 #
202218 do_reload = check_event (await watchdog_q .get (), False )
203219 #
@@ -223,10 +239,10 @@ def check_event(event, do_reload: bool) -> bool:
223239 observer = watchdog .observers .Observer ()
224240 if observer is not None :
225241 # don't run watchdog when we are testing (Observer() patches to None)
226- hass .data [DOMAIN ][WATCHDOG_RUN ] = False
227242 hass .data [DOMAIN ][WATCHDOG_TASK ] = Function .create_task (task_watchdog (watchdog_q ))
228243
229244 await hass .async_add_executor_job (WatchDogHandler , watchdog_q , observer , pyscript_folder )
245+ _LOGGER .debug ("watchdog started job and task folder=%s" , pyscript_folder )
230246
231247
232248async def async_setup_entry (hass : HomeAssistant , config_entry : ConfigEntry ) -> bool :
@@ -336,12 +352,9 @@ async def hass_started(event: HAEvent) -> None:
336352 await State .get_service_params ()
337353 hass .data [DOMAIN ][UNSUB_LISTENERS ].append (hass .bus .async_listen (EVENT_STATE_CHANGED , state_changed ))
338354 start_global_contexts ()
339- if WATCHDOG_RUN in hass .data [DOMAIN ]:
340- hass .data [DOMAIN ][WATCHDOG_RUN ] = True
341355
342356 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 ]:
345358 Function .reaper_cancel (hass .data [DOMAIN ][WATCHDOG_TASK ])
346359 del hass .data [DOMAIN ][WATCHDOG_TASK ]
347360
0 commit comments