From fc24c6cbd718dee36b0912741096a065b8566a7e Mon Sep 17 00:00:00 2001 From: Hmmbob <33529490+hmmbob@users.noreply.github.com> Date: Tue, 2 Jul 2024 10:16:11 +0200 Subject: [PATCH 1/5] await static_path_config --- custom_components/webrtc/__init__.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/custom_components/webrtc/__init__.py b/custom_components/webrtc/__init__.py index a366e50..a3d4521 100644 --- a/custom_components/webrtc/__init__.py +++ b/custom_components/webrtc/__init__.py @@ -60,14 +60,30 @@ 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"): - hass.http.register_static_path("/webrtc/" + name, str(path / name)) + await hass.http.async_register_static_paths( + [ + StaticPathConfig( + "/webrtc/" + name, + str(path / name), + True, + ) + ] + ) # 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 - hass.http.register_static_path("/webrtc/embed", str(path / "embed.html")) + await hass.http.async_register_static_paths( + [ + StaticPathConfig( + "/webrtc/embed", + "/config/custom_components/webrtc/www/embed.html", + True, + ) + ] + ) # 4. Serve WebSocket API hass.http.register_view(WebSocketView) From 123d669b3800b3e3494f5db0b922c4fa7f604afd Mon Sep 17 00:00:00 2001 From: Hmmbob <33529490+hmmbob@users.noreply.github.com> Date: Tue, 2 Jul 2024 10:18:10 +0200 Subject: [PATCH 2/5] StaticPath needs HA 2024.7 --- hacs.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hacs.json b/hacs.json index 3916fdc..27ec472 100644 --- a/hacs.json +++ b/hacs.json @@ -1,5 +1,5 @@ { "name": "WebRTC Camera", - "homeassistant": "2023.2.0", + "homeassistant": "2024.7.0b0", "render_readme": true } \ No newline at end of file From b1633f93dde59558009f578cb817ff2e5d31581f Mon Sep 17 00:00:00 2001 From: Hmmbob <33529490+hmmbob@users.noreply.github.com> Date: Thu, 11 Jul 2024 08:41:50 +0200 Subject: [PATCH 3/5] Fix StaticPathConfig import --- custom_components/webrtc/__init__.py | 2 +- hacs.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/webrtc/__init__.py b/custom_components/webrtc/__init__.py index a3d4521..cb8b714 100644 --- a/custom_components/webrtc/__init__.py +++ b/custom_components/webrtc/__init__.py @@ -11,7 +11,7 @@ from homeassistant.components.binary_sensor import HomeAssistant # fix tests from homeassistant.components.camera import async_get_stream_source, async_get_image from homeassistant.components.hassio.ingress import _websocket_forward -from homeassistant.components.http import HomeAssistantView +from homeassistant.components.http import HomeAssistantView, StaticPathConfig from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ENTITY_ID, CONF_URL, EVENT_HOMEASSISTANT_STOP from homeassistant.core import ServiceCall diff --git a/hacs.json b/hacs.json index 27ec472..0eeb100 100644 --- a/hacs.json +++ b/hacs.json @@ -1,5 +1,5 @@ { "name": "WebRTC Camera", - "homeassistant": "2024.7.0b0", + "homeassistant": "2024.7.0", "render_readme": true } \ No newline at end of file From 4e2ac7017aa4e7b66d2d7d6873e20506effa4cdf Mon Sep 17 00:00:00 2001 From: Hmmbob <33529490+hmmbob@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:27:01 +0200 Subject: [PATCH 4/5] Keep backward compatibility --- custom_components/webrtc/__init__.py | 51 +++++++++++++++++----------- hacs.json | 2 +- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/custom_components/webrtc/__init__.py b/custom_components/webrtc/__init__.py index cb8b714..35ce8c5 100644 --- a/custom_components/webrtc/__init__.py +++ b/custom_components/webrtc/__init__.py @@ -11,7 +11,7 @@ from homeassistant.components.binary_sensor import HomeAssistant # fix tests from homeassistant.components.camera import async_get_stream_source, async_get_image from homeassistant.components.hassio.ingress import _websocket_forward -from homeassistant.components.http import HomeAssistantView, StaticPathConfig +from homeassistant.components.http import HomeAssistantView from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ENTITY_ID, CONF_URL, EVENT_HOMEASSISTANT_STOP from homeassistant.core import ServiceCall @@ -20,6 +20,12 @@ 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 @@ -55,36 +61,41 @@ HLS_COOKIE = "webrtc-hls-session" HLS_SESSION = str(uuid.uuid4()) - 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, + ) + ] + ) + + # 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/" + name, - str(path / name), + "/webrtc/embed", + "/config/custom_components/webrtc/www/embed.html", True, ) ] ) - # 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 - await hass.http.async_register_static_paths( - [ - StaticPathConfig( - "/webrtc/embed", - "/config/custom_components/webrtc/www/embed.html", - True, - ) - ] - ) - # 4. Serve WebSocket API hass.http.register_view(WebSocketView) @@ -331,4 +342,4 @@ async def get(self, request: web.Request, filename: str): raise HTTPNotFound() body = await r.read() - return web.Response(body=body, content_type=r.content_type) + return web.Response(body=body, content_type=r.content_type) \ No newline at end of file diff --git a/hacs.json b/hacs.json index 0eeb100..3916fdc 100644 --- a/hacs.json +++ b/hacs.json @@ -1,5 +1,5 @@ { "name": "WebRTC Camera", - "homeassistant": "2024.7.0", + "homeassistant": "2023.2.0", "render_readme": true } \ No newline at end of file From a0783df2e5426118599edc50bfd0466b1b0f0716 Mon Sep 17 00:00:00 2001 From: Hmmbob <33529490+hmmbob@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:30:35 +0200 Subject: [PATCH 5/5] Styling --- custom_components/webrtc/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/webrtc/__init__.py b/custom_components/webrtc/__init__.py index 35ce8c5..39de61e 100644 --- a/custom_components/webrtc/__init__.py +++ b/custom_components/webrtc/__init__.py @@ -61,6 +61,7 @@ HLS_COOKIE = "webrtc-hls-session" HLS_SESSION = str(uuid.uuid4()) + async def async_setup(hass: HomeAssistant, config: dict): # 1. Serve lovelace card path = Path(__file__).parent / "www" @@ -342,4 +343,4 @@ async def get(self, request: web.Request, filename: str): raise HTTPNotFound() body = await r.read() - return web.Response(body=body, content_type=r.content_type) \ No newline at end of file + return web.Response(body=body, content_type=r.content_type)