custom_components/bahmcloud_store/views.py aktualisiert

This commit is contained in:
2026-01-15 10:17:01 +00:00
parent 12f4aec1f7
commit 46508f1c34

View File

@@ -3,19 +3,17 @@ from __future__ import annotations
import logging
from dataclasses import asdict
from pathlib import Path
from typing import Any
from typing import Any, TYPE_CHECKING
from aiohttp import web
from homeassistant.components.http import HomeAssistantView
from homeassistant.core import HomeAssistant
from .core import BCSCore
if TYPE_CHECKING:
from .core import BCSCore # only for type hints, avoids circular import at runtime
_LOGGER = logging.getLogger(__name__)
DOMAIN = "bahmcloud_store"
class StaticAssetsView(HomeAssistantView):
url = "/api/bahmcloud_store_static/{path:.*}"
@@ -35,7 +33,6 @@ class StaticAssetsView(HomeAssistantView):
if not target.exists():
return web.Response(status=404)
# Basic content types
ct = "text/plain"
if target.suffix == ".js":
ct = "application/javascript"
@@ -56,7 +53,8 @@ class BCSApiView(HomeAssistantView):
name = "api:bcs"
requires_auth = True
def __init__(self, core: BCSCore) -> None:
def __init__(self, core: Any) -> None:
# Any to avoid runtime import cycle (core imports views)
self.core = core
async def get(self, request: web.Request) -> web.Response:
@@ -88,7 +86,7 @@ class BCSCustomRepoView(HomeAssistantView):
name = "api:bcs_custom_repo"
requires_auth = True
def __init__(self, core: BCSCore) -> None:
def __init__(self, core: Any) -> None:
self.core = core
async def delete(self, request: web.Request) -> web.Response:
@@ -104,7 +102,7 @@ class BCSReadmeView(HomeAssistantView):
name = "api:bcs_readme"
requires_auth = True
def __init__(self, core: BCSCore) -> None:
def __init__(self, core: Any) -> None:
self.core = core
async def get(self, request: web.Request) -> web.Response:
@@ -118,18 +116,17 @@ class BCSReadmeView(HomeAssistantView):
html = None
# Render markdown via HA util (best effort, version-dependent)
# Render markdown via HA util (best effort)
try:
# Most HA versions ship a markdown renderer utility
from homeassistant.util.markdown import async_render_markdown # type: ignore
rendered = await async_render_markdown(self.core.hass, md) # returns HTML string
rendered = await async_render_markdown(self.core.hass, md)
html = rendered
except Exception as e:
_LOGGER.debug("Markdown render failed (util.markdown): %s", e)
_LOGGER.debug("Markdown render failed: %s", e)
html = None
# Sanitize HTML if sanitizer exists
# Sanitize HTML if available
if html:
try:
from homeassistant.util.sanitize_html import async_sanitize_html # type: ignore
@@ -137,8 +134,5 @@ class BCSReadmeView(HomeAssistantView):
html = await async_sanitize_html(self.core.hass, html)
except Exception as e:
_LOGGER.debug("HTML sanitize not available/failed: %s", e)
# If sanitizer is missing, still return rendered HTML (HA renderer is typically safe-ish),
# but keep it best-effort.
pass
return web.json_response({"ok": True, "readme": md, "html": html})