This commit is contained in:
2026-01-18 18:51:17 +00:00
parent 7a3a28d87f
commit 05897d4370

View File

@@ -3,10 +3,10 @@ from __future__ import annotations
import logging import logging
from datetime import timedelta from datetime import timedelta
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED
from homeassistant.components.panel_custom import async_register_panel from homeassistant.components.panel_custom import async_register_panel
from homeassistant.helpers.event import async_track_time_interval, async_call_later 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 homeassistant.helpers.discovery import async_load_platform
from .core import BCSCore, BCSConfig, BCSError from .core import BCSCore, BCSConfig, BCSError
@@ -35,6 +35,7 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool:
from .views import ( from .views import (
StaticAssetsView, StaticAssetsView,
BCSApiView, BCSApiView,
BCSSettingsView,
BCSReadmeView, BCSReadmeView,
BCSVersionsView, BCSVersionsView,
BCSRepoDetailView, BCSRepoDetailView,
@@ -49,6 +50,7 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool:
hass.http.register_view(StaticAssetsView()) hass.http.register_view(StaticAssetsView())
hass.http.register_view(BCSApiView(core)) hass.http.register_view(BCSApiView(core))
hass.http.register_view(BCSSettingsView(core))
hass.http.register_view(BCSReadmeView(core)) hass.http.register_view(BCSReadmeView(core))
hass.http.register_view(BCSVersionsView(core)) hass.http.register_view(BCSVersionsView(core))
hass.http.register_view(BCSRepoDetailView(core)) hass.http.register_view(BCSRepoDetailView(core))
@@ -72,23 +74,17 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool:
config={}, config={},
) )
# IMPORTANT: async def _do_startup_refresh(_now=None) -> None:
# 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:
try: try:
await core.full_refresh(source="startup") await core.full_refresh(source="startup")
except BCSError as e: except BCSError as e:
_LOGGER.error("Initial refresh failed: %s", e) _LOGGER.error("Initial refresh failed: %s", e)
except Exception:
_LOGGER.exception("Unexpected error during initial refresh")
@callback # Do not block Home Assistant startup. Schedule the initial refresh after HA started.
def _schedule_startup_refresh(_event=None) -> None: def _on_ha_started(_event) -> None:
# Give HA a short head-start (UI, recorder, etc.) before we start fetching lots of data. async_call_later(hass, 30, _do_startup_refresh)
async_call_later(hass, 30, lambda _now: hass.async_create_task(_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: async def periodic(_now) -> None:
try: try: