Add frontend version WS command (#34701)

pull/34850/head
Paulus Schoutsen 2020-04-25 17:30:15 -07:00
parent 17c31ed640
commit 86ae02e5c5
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
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})

View File

@ -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}