Skip to content

Commit

Permalink
Code refactoring after #723
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Sep 8, 2024
1 parent 6c4e132 commit d723584
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
32 changes: 2 additions & 30 deletions custom_components/webrtc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions custom_components/webrtc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d723584

Please sign in to comment.