From d723584f5e72f6d512c3a8697bec28c8c08f6424 Mon Sep 17 00:00:00 2001 From: Alex X Date: Sun, 8 Sep 2024 13:08:55 +0300 Subject: [PATCH] Code refactoring after #723 --- custom_components/webrtc/__init__.py | 32 ++-------------------------- custom_components/webrtc/utils.py | 12 +++++++++++ 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/custom_components/webrtc/__init__.py b/custom_components/webrtc/__init__.py index 39de61e..51aa2ec 100644 --- a/custom_components/webrtc/__init__.py +++ b/custom_components/webrtc/__init__.py @@ -20,12 +20,6 @@ from homeassistant.helpers.network import get_url from homeassistant.helpers.template import Template -try: - from homeassistant.components.http import StaticPathConfig - HA_VERSION_BEFORE_2024_7 = False -except ImportError: - HA_VERSION_BEFORE_2024_7 = True - from . import utils from .utils import DOMAIN, Server @@ -66,36 +60,14 @@ async def async_setup(hass: HomeAssistant, config: dict): # 1. Serve lovelace card path = Path(__file__).parent / "www" for name in ("video-rtc.js", "webrtc-camera.js", "digital-ptz.js"): - if HA_VERSION_BEFORE_2024_7: - hass.http.register_static_path("/webrtc/" + name, str(path / name)) - else: - await hass.http.async_register_static_paths( - [ - StaticPathConfig( - "/webrtc/" + name, - str(path / name), - True, - ) - ] - ) + await utils.register_static_path(hass, "/webrtc/" + name, str(path / name)) # 2. Add card to resources version = getattr(hass.data["integrations"][DOMAIN], "version", 0) await utils.init_resource(hass, "/webrtc/webrtc-camera.js", str(version)) # 3. Serve html page - if HA_VERSION_BEFORE_2024_7: - hass.http.register_static_path("/webrtc/embed", str(path / "embed.html")) - else: - await hass.http.async_register_static_paths( - [ - StaticPathConfig( - "/webrtc/embed", - "/config/custom_components/webrtc/www/embed.html", - True, - ) - ] - ) + await utils.register_static_path(hass, "/webrtc/embed", str(path / "embed.html")) # 4. Serve WebSocket API hass.http.register_view(WebSocketView) diff --git a/custom_components/webrtc/utils.py b/custom_components/webrtc/utils.py index 5e32982..8f7c8d0 100644 --- a/custom_components/webrtc/utils.py +++ b/custom_components/webrtc/utils.py @@ -15,6 +15,7 @@ from homeassistant.components.frontend import add_extra_js_url from homeassistant.components.http.auth import DATA_SIGN_SECRET, SIGN_QUERY_PARAM from homeassistant.components.lovelace.resources import ResourceStorageCollection +from homeassistant.const import MAJOR_VERSION, MINOR_VERSION from homeassistant.core import HomeAssistant from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.entity_component import DATA_INSTANCES @@ -102,6 +103,17 @@ async def validate_binary(hass: HomeAssistant) -> Optional[str]: return filename +async def register_static_path(hass: HomeAssistant, url_path: str, path: str): + if (MAJOR_VERSION, MINOR_VERSION) >= (2024, 7): + from homeassistant.components.http import StaticPathConfig + + await hass.http.async_register_static_paths( + [StaticPathConfig(url_path, path, True)] + ) + else: + hass.http.register_static_path(url_path, path) + + async def init_resource(hass: HomeAssistant, url: str, ver: str) -> bool: """Add extra JS module for lovelace mode YAML and new lovelace resource for mode GUI. It's better to add extra JS for all modes, because it has