Skip to content

Commit

Permalink
Keep backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
hmmbob authored Jul 11, 2024
1 parent b1633f9 commit 4e2ac70
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
51 changes: 31 additions & 20 deletions custom_components/webrtc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "WebRTC Camera",
"homeassistant": "2024.7.0",
"homeassistant": "2023.2.0",
"render_readme": true
}

0 comments on commit 4e2ac70

Please sign in to comment.