3838 REQUIREMENTS_FILE ,
3939 SERVICE_JUPYTER_KERNEL_START ,
4040 UNSUB_LISTENERS ,
41- WATCHDOG_OBSERVER ,
41+ WATCHDOG_RUN ,
4242 WATCHDOG_TASK ,
4343)
4444from .eval import AstEval
@@ -144,7 +144,7 @@ async def watchdog_start(
144144 hass : HomeAssistant , pyscript_folder : str , reload_scripts_handler : Callable [[None ], None ]
145145) -> None :
146146 """Start watchdog thread to look for changed files in pyscript_folder."""
147- if WATCHDOG_OBSERVER in hass .data [DOMAIN ]:
147+ if WATCHDOG_TASK in hass .data [DOMAIN ]:
148148 return
149149
150150 class WatchDogHandler (FileSystemEventHandler ):
@@ -159,8 +159,9 @@ def __init__(
159159
160160 def process (self , event : FileSystemEvent ) -> None :
161161 """Send watchdog events to main loop task."""
162- _LOGGER .debug ("watchdog process(%s)" , event )
163- hass .loop .call_soon_threadsafe (self .watchdog_q .put_nowait , event )
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 )
164165
165166 def on_modified (self , event : FileSystemEvent ) -> None :
166167 """File modified."""
@@ -222,7 +223,7 @@ def check_event(event, do_reload: bool) -> bool:
222223 observer = watchdog .observers .Observer ()
223224 if observer is not None :
224225 # don't run watchdog when we are testing (Observer() patches to None)
225- hass .data [DOMAIN ][WATCHDOG_OBSERVER ] = observer
226+ hass .data [DOMAIN ][WATCHDOG_RUN ] = False
226227 hass .data [DOMAIN ][WATCHDOG_TASK ] = Function .create_task (task_watchdog (watchdog_q ))
227228
228229 await hass .async_add_executor_job (WatchDogHandler , watchdog_q , observer , pyscript_folder )
@@ -335,16 +336,12 @@ async def hass_started(event: HAEvent) -> None:
335336 await State .get_service_params ()
336337 hass .data [DOMAIN ][UNSUB_LISTENERS ].append (hass .bus .async_listen (EVENT_STATE_CHANGED , state_changed ))
337338 start_global_contexts ()
338- if WATCHDOG_OBSERVER in hass .data [DOMAIN ]:
339- observer = hass .data [DOMAIN ][WATCHDOG_OBSERVER ]
340- observer .start ()
339+ if WATCHDOG_RUN in hass .data [DOMAIN ]:
340+ hass .data [DOMAIN ][WATCHDOG_RUN ] = True
341341
342342 async def hass_stop (event : HAEvent ) -> None :
343- if WATCHDOG_OBSERVER in hass .data [DOMAIN ]:
344- observer = hass .data [DOMAIN ][WATCHDOG_OBSERVER ]
345- observer .stop ()
346- observer .join ()
347- del hass .data [DOMAIN ][WATCHDOG_OBSERVER ]
343+ if WATCHDOG_RUN in hass .data [DOMAIN ]:
344+ hass .data [DOMAIN ][WATCHDOG_RUN ] = False
348345 Function .reaper_cancel (hass .data [DOMAIN ][WATCHDOG_TASK ])
349346 del hass .data [DOMAIN ][WATCHDOG_TASK ]
350347
0 commit comments