custom_components/bahmcloud_store/__init__.py aktualisiert
This commit is contained in:
@@ -4,27 +4,57 @@ import logging
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.const import Platform
|
|
||||||
from homeassistant.helpers.discovery import async_load_platform
|
|
||||||
from homeassistant.helpers.event import async_track_time_interval
|
|
||||||
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
|
||||||
|
|
||||||
from .core import BCSCore, BCSConfig, BCSError
|
from .core import BCSCore, BCSConfig, BCSError
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DOMAIN = "bahmcloud_store"
|
DOMAIN = "bahmcloud_store"
|
||||||
...
|
|
||||||
|
|
||||||
|
DEFAULT_STORE_URL = "https://git.bahmcloud.de/bahmcloud/ha_store/raw/branch/main/store.yaml"
|
||||||
|
CONF_STORE_URL = "store_url"
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup(hass: HomeAssistant, config: dict) -> bool:
|
||||||
|
"""Set up the Bahmcloud Store integration."""
|
||||||
|
cfg = config.get(DOMAIN, {}) or {}
|
||||||
|
store_url = cfg.get(CONF_STORE_URL, DEFAULT_STORE_URL)
|
||||||
|
|
||||||
|
core = BCSCore(hass, BCSConfig(store_url=store_url))
|
||||||
|
hass.data[DOMAIN] = core
|
||||||
|
|
||||||
|
await core.register_http_views()
|
||||||
|
|
||||||
|
# Keep the exact module_url pattern that worked reliably (cache-busted via v=42)
|
||||||
|
await async_register_panel(
|
||||||
|
hass,
|
||||||
|
frontend_url_path="bahmcloud-store",
|
||||||
|
webcomponent_name="bahmcloud-store-panel",
|
||||||
|
module_url="/api/bahmcloud_store_static/panel.js?v=42",
|
||||||
|
sidebar_title="Bahmcloud Store",
|
||||||
|
sidebar_icon="mdi:store",
|
||||||
|
require_admin=True,
|
||||||
|
config={},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Initial load at startup
|
||||||
|
try:
|
||||||
|
await core.full_refresh(source="startup")
|
||||||
|
except BCSError as e:
|
||||||
|
_LOGGER.error("Initial refresh failed: %s", e)
|
||||||
|
|
||||||
async def periodic(_now) -> None:
|
async def periodic(_now) -> None:
|
||||||
try:
|
try:
|
||||||
await core.full_refresh(source="timer")
|
await core.full_refresh(source="timer")
|
||||||
except BCSError as e:
|
except BCSError as e:
|
||||||
_LOGGER.warning("Periodic refresh failed: %s", e)
|
_LOGGER.warning("Periodic refresh failed: %s", e)
|
||||||
|
except Exception as e:
|
||||||
|
_LOGGER.exception("Unexpected error during periodic refresh: %s", e)
|
||||||
|
|
||||||
interval = timedelta(seconds=int(core.refresh_seconds or 300))
|
# Start with the configured refresh interval (default: 300 seconds)
|
||||||
async_track_time_interval(hass, periodic, interval)
|
interval_seconds = int(getattr(core, "refresh_seconds", 300) or 300)
|
||||||
|
async_track_time_interval(hass, periodic, timedelta(seconds=interval_seconds))
|
||||||
|
|
||||||
await async_load_platform(hass, Platform.UPDATE, DOMAIN, {}, config)
|
|
||||||
return True
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user