Skip to content

Commit f436e29

Browse files
authored
Add frontend version WS command (#34701)
1 parent aa60d36 commit f436e29

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

homeassistant/components/frontend/__init__.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from homeassistant.helpers import service
2020
import homeassistant.helpers.config_validation as cv
2121
from homeassistant.helpers.translation import async_get_translations
22-
from homeassistant.loader import bind_hass
22+
from homeassistant.loader import async_get_integration, bind_hass
2323

2424
from .storage import async_setup_frontend_storage
2525

@@ -248,6 +248,7 @@ async def async_setup(hass, config):
248248
hass.components.websocket_api.async_register_command(websocket_get_panels)
249249
hass.components.websocket_api.async_register_command(websocket_get_themes)
250250
hass.components.websocket_api.async_register_command(websocket_get_translations)
251+
hass.components.websocket_api.async_register_command(websocket_get_version)
251252
hass.http.register_view(ManifestJSONView)
252253

253254
conf = config.get(DOMAIN, {})
@@ -486,10 +487,7 @@ def get(self, request): # pylint: disable=no-self-use
486487
@callback
487488
@websocket_api.websocket_command({"type": "get_panels"})
488489
def websocket_get_panels(hass, connection, msg):
489-
"""Handle get panels command.
490-
491-
Async friendly.
492-
"""
490+
"""Handle get panels command."""
493491
user_is_admin = connection.user.is_admin
494492
panels = {
495493
panel_key: panel.to_response()
@@ -503,10 +501,7 @@ def websocket_get_panels(hass, connection, msg):
503501
@callback
504502
@websocket_api.websocket_command({"type": "frontend/get_themes"})
505503
def websocket_get_themes(hass, connection, msg):
506-
"""Handle get themes command.
507-
508-
Async friendly.
509-
"""
504+
"""Handle get themes command."""
510505
if hass.config.safe_mode:
511506
connection.send_message(
512507
websocket_api.result_message(
@@ -546,10 +541,7 @@ def websocket_get_themes(hass, connection, msg):
546541
)
547542
@websocket_api.async_response
548543
async def websocket_get_translations(hass, connection, msg):
549-
"""Handle get translations command.
550-
551-
Async friendly.
552-
"""
544+
"""Handle get translations command."""
553545
resources = await async_get_translations(
554546
hass,
555547
msg["language"],
@@ -560,3 +552,21 @@ async def websocket_get_translations(hass, connection, msg):
560552
connection.send_message(
561553
websocket_api.result_message(msg["id"], {"resources": resources})
562554
)
555+
556+
557+
@websocket_api.websocket_command({"type": "frontend/get_version"})
558+
@websocket_api.async_response
559+
async def websocket_get_version(hass, connection, msg):
560+
"""Handle get version command."""
561+
integration = await async_get_integration(hass, "frontend")
562+
563+
frontend = None
564+
565+
for req in integration.requirements:
566+
if req.startswith("home-assistant-frontend=="):
567+
frontend = req.split("==", 1)[1]
568+
569+
if frontend is None:
570+
connection.send_error(msg["id"], "unknown_version", "Version not found")
571+
else:
572+
connection.send_result(msg["id"], {"version": frontend})

tests/components/frontend/test_init.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)
1515
from homeassistant.components.websocket_api.const import TYPE_RESULT
1616
from homeassistant.const import HTTP_NOT_FOUND
17+
from homeassistant.loader import async_get_integration
1718
from homeassistant.setup import async_setup_component
1819

1920
from tests.common import async_capture_events
@@ -336,3 +337,24 @@ async def test_auth_authorize(mock_http_client):
336337
resp = await mock_http_client.get(authorizejs.groups(0)[0])
337338
assert resp.status == 200
338339
assert "public" in resp.headers.get("cache-control")
340+
341+
342+
async def test_get_version(hass, hass_ws_client):
343+
"""Test get_version command."""
344+
frontend = await async_get_integration(hass, "frontend")
345+
cur_version = next(
346+
req.split("==", 1)[1]
347+
for req in frontend.requirements
348+
if req.startswith("home-assistant-frontend==")
349+
)
350+
351+
await async_setup_component(hass, "frontend", {})
352+
client = await hass_ws_client(hass)
353+
354+
await client.send_json({"id": 5, "type": "frontend/get_version"})
355+
msg = await client.receive_json()
356+
357+
assert msg["id"] == 5
358+
assert msg["type"] == TYPE_RESULT
359+
assert msg["success"]
360+
assert msg["result"] == {"version": cur_version}

0 commit comments

Comments
 (0)