custom_components/bahmcloud_store/views.py aktualisiert
This commit is contained in:
@@ -3,19 +3,17 @@ from __future__ import annotations
|
|||||||
import logging
|
import logging
|
||||||
from dataclasses import asdict
|
from dataclasses import asdict
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any, TYPE_CHECKING
|
||||||
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
|
||||||
from homeassistant.components.http import HomeAssistantView
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DOMAIN = "bahmcloud_store"
|
|
||||||
|
|
||||||
|
|
||||||
class StaticAssetsView(HomeAssistantView):
|
class StaticAssetsView(HomeAssistantView):
|
||||||
url = "/api/bahmcloud_store_static/{path:.*}"
|
url = "/api/bahmcloud_store_static/{path:.*}"
|
||||||
@@ -35,7 +33,6 @@ class StaticAssetsView(HomeAssistantView):
|
|||||||
if not target.exists():
|
if not target.exists():
|
||||||
return web.Response(status=404)
|
return web.Response(status=404)
|
||||||
|
|
||||||
# Basic content types
|
|
||||||
ct = "text/plain"
|
ct = "text/plain"
|
||||||
if target.suffix == ".js":
|
if target.suffix == ".js":
|
||||||
ct = "application/javascript"
|
ct = "application/javascript"
|
||||||
@@ -56,7 +53,8 @@ class BCSApiView(HomeAssistantView):
|
|||||||
name = "api:bcs"
|
name = "api:bcs"
|
||||||
requires_auth = True
|
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
|
self.core = core
|
||||||
|
|
||||||
async def get(self, request: web.Request) -> web.Response:
|
async def get(self, request: web.Request) -> web.Response:
|
||||||
@@ -88,7 +86,7 @@ class BCSCustomRepoView(HomeAssistantView):
|
|||||||
name = "api:bcs_custom_repo"
|
name = "api:bcs_custom_repo"
|
||||||
requires_auth = True
|
requires_auth = True
|
||||||
|
|
||||||
def __init__(self, core: BCSCore) -> None:
|
def __init__(self, core: Any) -> None:
|
||||||
self.core = core
|
self.core = core
|
||||||
|
|
||||||
async def delete(self, request: web.Request) -> web.Response:
|
async def delete(self, request: web.Request) -> web.Response:
|
||||||
@@ -104,7 +102,7 @@ class BCSReadmeView(HomeAssistantView):
|
|||||||
name = "api:bcs_readme"
|
name = "api:bcs_readme"
|
||||||
requires_auth = True
|
requires_auth = True
|
||||||
|
|
||||||
def __init__(self, core: BCSCore) -> None:
|
def __init__(self, core: Any) -> None:
|
||||||
self.core = core
|
self.core = core
|
||||||
|
|
||||||
async def get(self, request: web.Request) -> web.Response:
|
async def get(self, request: web.Request) -> web.Response:
|
||||||
@@ -118,18 +116,17 @@ class BCSReadmeView(HomeAssistantView):
|
|||||||
|
|
||||||
html = None
|
html = None
|
||||||
|
|
||||||
# Render markdown via HA util (best effort, version-dependent)
|
# Render markdown via HA util (best effort)
|
||||||
try:
|
try:
|
||||||
# Most HA versions ship a markdown renderer utility
|
|
||||||
from homeassistant.util.markdown import async_render_markdown # type: ignore
|
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
|
html = rendered
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.debug("Markdown render failed (util.markdown): %s", e)
|
_LOGGER.debug("Markdown render failed: %s", e)
|
||||||
html = None
|
html = None
|
||||||
|
|
||||||
# Sanitize HTML if sanitizer exists
|
# Sanitize HTML if available
|
||||||
if html:
|
if html:
|
||||||
try:
|
try:
|
||||||
from homeassistant.util.sanitize_html import async_sanitize_html # type: ignore
|
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)
|
html = await async_sanitize_html(self.core.hass, html)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.debug("HTML sanitize not available/failed: %s", 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})
|
return web.json_response({"ok": True, "readme": md, "html": html})
|
||||||
|
|||||||
Reference in New Issue
Block a user