Add frontend version WS command (#34701)

This commit is contained in:
Paulus Schoutsen 2020-04-25 17:30:15 -07:00 committed by GitHub
parent aa60d362fd
commit f436e29a0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 13 deletions

View File

@ -19,7 +19,7 @@ from homeassistant.core import callback
from homeassistant.helpers import service from homeassistant.helpers import service
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.translation import async_get_translations 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 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_panels)
hass.components.websocket_api.async_register_command(websocket_get_themes) 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_translations)
hass.components.websocket_api.async_register_command(websocket_get_version)
hass.http.register_view(ManifestJSONView) hass.http.register_view(ManifestJSONView)
conf = config.get(DOMAIN, {}) conf = config.get(DOMAIN, {})
@ -486,10 +487,7 @@ class ManifestJSONView(HomeAssistantView):
@callback @callback
@websocket_api.websocket_command({"type": "get_panels"}) @websocket_api.websocket_command({"type": "get_panels"})
def websocket_get_panels(hass, connection, msg): def websocket_get_panels(hass, connection, msg):
"""Handle get panels command. """Handle get panels command."""
Async friendly.
"""
user_is_admin = connection.user.is_admin user_is_admin = connection.user.is_admin
panels = { panels = {
panel_key: panel.to_response() panel_key: panel.to_response()
@ -503,10 +501,7 @@ def websocket_get_panels(hass, connection, msg):
@callback @callback
@websocket_api.websocket_command({"type": "frontend/get_themes"}) @websocket_api.websocket_command({"type": "frontend/get_themes"})
def websocket_get_themes(hass, connection, msg): def websocket_get_themes(hass, connection, msg):
"""Handle get themes command. """Handle get themes command."""
Async friendly.
"""
if hass.config.safe_mode: if hass.config.safe_mode:
connection.send_message( connection.send_message(
websocket_api.result_message( websocket_api.result_message(
@ -546,10 +541,7 @@ def websocket_get_themes(hass, connection, msg):
) )
@websocket_api.async_response @websocket_api.async_response
async def websocket_get_translations(hass, connection, msg): async def websocket_get_translations(hass, connection, msg):
"""Handle get translations command. """Handle get translations command."""
Async friendly.
"""
resources = await async_get_translations( resources = await async_get_translations(
hass, hass,
msg["language"], msg["language"],
@ -560,3 +552,21 @@ async def websocket_get_translations(hass, connection, msg):
connection.send_message( connection.send_message(
websocket_api.result_message(msg["id"], {"resources": resources}) 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})

View File

@ -14,6 +14,7 @@ from homeassistant.components.frontend import (
) )
from homeassistant.components.websocket_api.const import TYPE_RESULT from homeassistant.components.websocket_api.const import TYPE_RESULT
from homeassistant.const import HTTP_NOT_FOUND from homeassistant.const import HTTP_NOT_FOUND
from homeassistant.loader import async_get_integration
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import async_capture_events 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]) resp = await mock_http_client.get(authorizejs.groups(0)[0])
assert resp.status == 200 assert resp.status == 200
assert "public" in resp.headers.get("cache-control") 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}