diff --git a/custom_components/bahmcloud_store/__init__.py b/custom_components/bahmcloud_store/__init__.py index e0faf4a..3712b59 100644 --- a/custom_components/bahmcloud_store/__init__.py +++ b/custom_components/bahmcloud_store/__init__.py @@ -3,10 +3,10 @@ from __future__ import annotations import logging from datetime import timedelta -from homeassistant.core import HomeAssistant, callback -from homeassistant.const import EVENT_HOMEASSISTANT_STARTED +from homeassistant.core import HomeAssistant from homeassistant.components.panel_custom import async_register_panel from homeassistant.helpers.event import async_track_time_interval, async_call_later +from homeassistant.const import EVENT_HOMEASSISTANT_STARTED from homeassistant.helpers.discovery import async_load_platform from .core import BCSCore, BCSConfig, BCSError @@ -35,6 +35,7 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool: from .views import ( StaticAssetsView, BCSApiView, + BCSSettingsView, BCSReadmeView, BCSVersionsView, BCSRepoDetailView, @@ -49,6 +50,7 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool: hass.http.register_view(StaticAssetsView()) hass.http.register_view(BCSApiView(core)) + hass.http.register_view(BCSSettingsView(core)) hass.http.register_view(BCSReadmeView(core)) hass.http.register_view(BCSVersionsView(core)) hass.http.register_view(BCSRepoDetailView(core)) @@ -72,23 +74,17 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool: config={}, ) - # IMPORTANT: - # Do NOT block Home Assistant startup with network-heavy refreshes. - # We wait until HA has fully started, then kick off the initial refresh. - async def _startup_refresh() -> None: + async def _do_startup_refresh(_now=None) -> None: try: await core.full_refresh(source="startup") except BCSError as e: _LOGGER.error("Initial refresh failed: %s", e) - except Exception: - _LOGGER.exception("Unexpected error during initial refresh") - @callback - def _schedule_startup_refresh(_event=None) -> None: - # Give HA a short head-start (UI, recorder, etc.) before we start fetching lots of data. - async_call_later(hass, 30, lambda _now: hass.async_create_task(_startup_refresh())) + # Do not block Home Assistant startup. Schedule the initial refresh after HA started. + def _on_ha_started(_event) -> None: + async_call_later(hass, 30, _do_startup_refresh) - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, _schedule_startup_refresh) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, _on_ha_started) async def periodic(_now) -> None: try: @@ -101,4 +97,4 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool: interval_seconds = int(getattr(core, "refresh_seconds", 300) or 300) async_track_time_interval(hass, periodic, timedelta(seconds=interval_seconds)) - return True + return True \ No newline at end of file