Skip to content

Commit d723584

Browse files
committed
Code refactoring after #723
1 parent 6c4e132 commit d723584

2 files changed

Lines changed: 14 additions & 30 deletions

File tree

custom_components/webrtc/__init__.py

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
from homeassistant.helpers.network import get_url
2121
from homeassistant.helpers.template import Template
2222

23-
try:
24-
from homeassistant.components.http import StaticPathConfig
25-
HA_VERSION_BEFORE_2024_7 = False
26-
except ImportError:
27-
HA_VERSION_BEFORE_2024_7 = True
28-
2923
from . import utils
3024
from .utils import DOMAIN, Server
3125

@@ -66,36 +60,14 @@ async def async_setup(hass: HomeAssistant, config: dict):
6660
# 1. Serve lovelace card
6761
path = Path(__file__).parent / "www"
6862
for name in ("video-rtc.js", "webrtc-camera.js", "digital-ptz.js"):
69-
if HA_VERSION_BEFORE_2024_7:
70-
hass.http.register_static_path("/webrtc/" + name, str(path / name))
71-
else:
72-
await hass.http.async_register_static_paths(
73-
[
74-
StaticPathConfig(
75-
"/webrtc/" + name,
76-
str(path / name),
77-
True,
78-
)
79-
]
80-
)
63+
await utils.register_static_path(hass, "/webrtc/" + name, str(path / name))
8164

8265
# 2. Add card to resources
8366
version = getattr(hass.data["integrations"][DOMAIN], "version", 0)
8467
await utils.init_resource(hass, "/webrtc/webrtc-camera.js", str(version))
8568

8669
# 3. Serve html page
87-
if HA_VERSION_BEFORE_2024_7:
88-
hass.http.register_static_path("/webrtc/embed", str(path / "embed.html"))
89-
else:
90-
await hass.http.async_register_static_paths(
91-
[
92-
StaticPathConfig(
93-
"/webrtc/embed",
94-
"/config/custom_components/webrtc/www/embed.html",
95-
True,
96-
)
97-
]
98-
)
70+
await utils.register_static_path(hass, "/webrtc/embed", str(path / "embed.html"))
9971

10072
# 4. Serve WebSocket API
10173
hass.http.register_view(WebSocketView)

custom_components/webrtc/utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from homeassistant.components.frontend import add_extra_js_url
1616
from homeassistant.components.http.auth import DATA_SIGN_SECRET, SIGN_QUERY_PARAM
1717
from homeassistant.components.lovelace.resources import ResourceStorageCollection
18+
from homeassistant.const import MAJOR_VERSION, MINOR_VERSION
1819
from homeassistant.core import HomeAssistant
1920
from homeassistant.helpers.aiohttp_client import async_get_clientsession
2021
from homeassistant.helpers.entity_component import DATA_INSTANCES
@@ -102,6 +103,17 @@ async def validate_binary(hass: HomeAssistant) -> Optional[str]:
102103
return filename
103104

104105

106+
async def register_static_path(hass: HomeAssistant, url_path: str, path: str):
107+
if (MAJOR_VERSION, MINOR_VERSION) >= (2024, 7):
108+
from homeassistant.components.http import StaticPathConfig
109+
110+
await hass.http.async_register_static_paths(
111+
[StaticPathConfig(url_path, path, True)]
112+
)
113+
else:
114+
hass.http.register_static_path(url_path, path)
115+
116+
105117
async def init_resource(hass: HomeAssistant, url: str, ver: str) -> bool:
106118
"""Add extra JS module for lovelace mode YAML and new lovelace resource
107119
for mode GUI. It's better to add extra JS for all modes, because it has

0 commit comments

Comments
 (0)