38
38
REQUIREMENTS_FILE ,
39
39
SERVICE_JUPYTER_KERNEL_START ,
40
40
UNSUB_LISTENERS ,
41
- WATCHDOG_OBSERVER ,
41
+ WATCHDOG_RUN ,
42
42
WATCHDOG_TASK ,
43
43
)
44
44
from .eval import AstEval
@@ -144,7 +144,7 @@ async def watchdog_start(
144
144
hass : HomeAssistant , pyscript_folder : str , reload_scripts_handler : Callable [[None ], None ]
145
145
) -> None :
146
146
"""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 ]:
148
148
return
149
149
150
150
class WatchDogHandler (FileSystemEventHandler ):
@@ -159,8 +159,9 @@ def __init__(
159
159
160
160
def process (self , event : FileSystemEvent ) -> None :
161
161
"""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 )
164
165
165
166
def on_modified (self , event : FileSystemEvent ) -> None :
166
167
"""File modified."""
@@ -222,7 +223,7 @@ def check_event(event, do_reload: bool) -> bool:
222
223
observer = watchdog .observers .Observer ()
223
224
if observer is not None :
224
225
# 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
226
227
hass .data [DOMAIN ][WATCHDOG_TASK ] = Function .create_task (task_watchdog (watchdog_q ))
227
228
228
229
await hass .async_add_executor_job (WatchDogHandler , watchdog_q , observer , pyscript_folder )
@@ -335,16 +336,12 @@ async def hass_started(event: HAEvent) -> None:
335
336
await State .get_service_params ()
336
337
hass .data [DOMAIN ][UNSUB_LISTENERS ].append (hass .bus .async_listen (EVENT_STATE_CHANGED , state_changed ))
337
338
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
341
341
342
342
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
348
345
Function .reaper_cancel (hass .data [DOMAIN ][WATCHDOG_TASK ])
349
346
del hass .data [DOMAIN ][WATCHDOG_TASK ]
350
347
0 commit comments