custom_components/bahmcloud_store/__init__.py aktualisiert
This commit is contained in:
@@ -3,45 +3,66 @@ from __future__ import annotations
|
||||
import logging
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.components import frontend
|
||||
|
||||
from .core import BCSCore
|
||||
from .views import (
|
||||
StaticAssetsView,
|
||||
BCSApiView,
|
||||
BCSCustomRepoView,
|
||||
BCSReadmeView,
|
||||
)
|
||||
from .core import DOMAIN, BahmcloudStore, StoreConfig
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = "bahmcloud_store"
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: dict) -> bool:
|
||||
# Your BCSCore requires config in __init__
|
||||
core = BCSCore(hass, config)
|
||||
# Read configuration from configuration.yaml:
|
||||
# bahmcloud_store:
|
||||
# store_url: "https://....../store.yaml"
|
||||
domain_cfg = config.get(DOMAIN) or {}
|
||||
|
||||
# Call optional setup methods if they exist (keep compatibility with your working core)
|
||||
try:
|
||||
if hasattr(core, "setup"):
|
||||
res = core.setup()
|
||||
if hasattr(res, "__await__"):
|
||||
await res
|
||||
elif hasattr(core, "start"):
|
||||
res = core.start()
|
||||
if hasattr(res, "__await__"):
|
||||
await res
|
||||
except Exception as e:
|
||||
_LOGGER.exception("BCSCore setup/start failed: %s", e)
|
||||
store_url = None
|
||||
if isinstance(domain_cfg, dict):
|
||||
store_url = domain_cfg.get("store_url") or domain_cfg.get("store") or domain_cfg.get("url")
|
||||
|
||||
if not store_url or not isinstance(store_url, str):
|
||||
_LOGGER.error(
|
||||
"Missing configuration for %s. Please set %s: {store_url: 'https://.../store.yaml'}",
|
||||
DOMAIN,
|
||||
DOMAIN,
|
||||
)
|
||||
return False
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN]["core"] = core
|
||||
store = BahmcloudStore(hass, StoreConfig(store_url=store_url))
|
||||
|
||||
# Register HTTP views
|
||||
hass.http.register_view(StaticAssetsView())
|
||||
hass.http.register_view(BCSApiView(core))
|
||||
hass.http.register_view(BCSCustomRepoView(core))
|
||||
hass.http.register_view(BCSReadmeView(core))
|
||||
# Register API + static panel assets (these are defined in core.py)
|
||||
await store.register_http_views()
|
||||
|
||||
# Store reference
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN]["store"] = store
|
||||
|
||||
# Register sidebar panel (compat layer for different HA versions)
|
||||
panel_js = "/api/bahmcloud_store_static/panel.js"
|
||||
try:
|
||||
# Some HA versions use module_url
|
||||
frontend.async_register_panel(
|
||||
hass,
|
||||
"custom",
|
||||
DOMAIN,
|
||||
sidebar_title="Bahmcloud Store",
|
||||
sidebar_icon="mdi:store",
|
||||
module_url=panel_js,
|
||||
config={},
|
||||
)
|
||||
except TypeError:
|
||||
# Fallback: some HA versions use js_url instead
|
||||
try:
|
||||
frontend.async_register_panel(
|
||||
hass,
|
||||
"custom",
|
||||
DOMAIN,
|
||||
sidebar_title="Bahmcloud Store",
|
||||
sidebar_icon="mdi:store",
|
||||
js_url=panel_js,
|
||||
config={},
|
||||
)
|
||||
except Exception as e:
|
||||
_LOGGER.warning("Panel registration failed (store will still work via API): %s", e)
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user