From f436e29a0df7769b18337b5003188a945857cc5b Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 25 Apr 2020 17:30:15 -0700 Subject: [PATCH] Add frontend version WS command (#34701) --- homeassistant/components/frontend/__init__.py | 36 ++++++++++++------- tests/components/frontend/test_init.py | 22 ++++++++++++ 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/frontend/__init__.py b/homeassistant/components/frontend/__init__.py index f6a16205755..e5b93399c43 100644 --- a/homeassistant/components/frontend/__init__.py +++ b/homeassistant/components/frontend/__init__.py @@ -19,7 +19,7 @@ from homeassistant.core import callback from homeassistant.helpers import service import homeassistant.helpers.config_validation as cv from homeassistant.helpers.translation import async_get_translations -from homeassistant.loader import bind_hass +from homeassistant.loader import async_get_integration, bind_hass from .storage import async_setup_frontend_storage @@ -248,6 +248,7 @@ async def async_setup(hass, config): hass.components.websocket_api.async_register_command(websocket_get_panels) hass.components.websocket_api.async_register_command(websocket_get_themes) hass.components.websocket_api.async_register_command(websocket_get_translations) + hass.components.websocket_api.async_register_command(websocket_get_version) hass.http.register_view(ManifestJSONView) conf = config.get(DOMAIN, {}) @@ -486,10 +487,7 @@ class ManifestJSONView(HomeAssistantView): @callback @websocket_api.websocket_command({"type": "get_panels"}) def websocket_get_panels(hass, connection, msg): - """Handle get panels command. - - Async friendly. - """ + """Handle get panels command.""" user_is_admin = connection.user.is_admin panels = { panel_key: panel.to_response() @@ -503,10 +501,7 @@ def websocket_get_panels(hass, connection, msg): @callback @websocket_api.websocket_command({"type": "frontend/get_themes"}) def websocket_get_themes(hass, connection, msg): - """Handle get themes command. - - Async friendly. - """ + """Handle get themes command.""" if hass.config.safe_mode: connection.send_message( websocket_api.result_message( @@ -546,10 +541,7 @@ def websocket_get_themes(hass, connection, msg): ) @websocket_api.async_response async def websocket_get_translations(hass, connection, msg): - """Handle get translations command. - - Async friendly. - """ + """Handle get translations command.""" resources = await async_get_translations( hass, msg["language"], @@ -560,3 +552,21 @@ async def websocket_get_translations(hass, connection, msg): connection.send_message( websocket_api.result_message(msg["id"], {"resources": resources}) ) + + +@websocket_api.websocket_command({"type": "frontend/get_version"}) +@websocket_api.async_response +async def websocket_get_version(hass, connection, msg): + """Handle get version command.""" + integration = await async_get_integration(hass, "frontend") + + frontend = None + + for req in integration.requirements: + if req.startswith("home-assistant-frontend=="): + frontend = req.split("==", 1)[1] + + if frontend is None: + connection.send_error(msg["id"], "unknown_version", "Version not found") + else: + connection.send_result(msg["id"], {"version": frontend}) diff --git a/tests/components/frontend/test_init.py b/tests/components/frontend/test_init.py index 7297812249c..ef254871830 100644 --- a/tests/components/frontend/test_init.py +++ b/tests/components/frontend/test_init.py @@ -14,6 +14,7 @@ from homeassistant.components.frontend import ( ) from homeassistant.components.websocket_api.const import TYPE_RESULT from homeassistant.const import HTTP_NOT_FOUND +from homeassistant.loader import async_get_integration from homeassistant.setup import async_setup_component from tests.common import async_capture_events @@ -336,3 +337,24 @@ async def test_auth_authorize(mock_http_client): resp = await mock_http_client.get(authorizejs.groups(0)[0]) assert resp.status == 200 assert "public" in resp.headers.get("cache-control") + + +async def test_get_version(hass, hass_ws_client): + """Test get_version command.""" + frontend = await async_get_integration(hass, "frontend") + cur_version = next( + req.split("==", 1)[1] + for req in frontend.requirements + if req.startswith("home-assistant-frontend==") + ) + + await async_setup_component(hass, "frontend", {}) + client = await hass_ws_client(hass) + + await client.send_json({"id": 5, "type": "frontend/get_version"}) + msg = await client.receive_json() + + assert msg["id"] == 5 + assert msg["type"] == TYPE_RESULT + assert msg["success"] + assert msg["result"] == {"version": cur_version}