Skip to content

Commit aaf0ab5

Browse files
committed
fixed automatic reload; see custom-components#622
1 parent a8d7ff0 commit aaf0ab5

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

custom_components/pyscript/__init__.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
REQUIREMENTS_FILE,
3939
SERVICE_JUPYTER_KERNEL_START,
4040
UNSUB_LISTENERS,
41-
WATCHDOG_RUN,
4241
WATCHDOG_TASK,
4342
)
4443
from .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

232248
async 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

custom_components/pyscript/const.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
REQUIREMENTS_FILE = "requirements.txt"
4141
REQUIREMENTS_PATHS = ("", "apps/*", "modules/*", "scripts/**")
4242

43-
WATCHDOG_RUN = "watch_dog_run"
4443
WATCHDOG_TASK = "watch_dog_task"
4544

4645
ALLOWED_IMPORTS = {

0 commit comments

Comments
 (0)