Clean up frontend services and events (#31654)

* Clean up frontend services and events

* Fix bug in core instead

* Add test that core works correctly with callback marked async funcs
This commit is contained in:
Paulus Schoutsen 2020-02-09 19:47:16 -08:00 committed by GitHub
parent 28eeed1db3
commit 12de3f1e47
4 changed files with 59 additions and 40 deletions

View file

@ -16,6 +16,7 @@ from homeassistant.components.http.view import HomeAssistantView
from homeassistant.config import async_hass_config_yaml
from homeassistant.const import CONF_NAME, EVENT_THEMES_UPDATED
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
@ -103,19 +104,6 @@ CONFIG_SCHEMA = vol.Schema(
SERVICE_SET_THEME = "set_theme"
SERVICE_RELOAD_THEMES = "reload_themes"
SERVICE_SET_THEME_SCHEMA = vol.Schema({vol.Required(CONF_NAME): cv.string})
WS_TYPE_GET_PANELS = "get_panels"
SCHEMA_GET_PANELS = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
{vol.Required("type"): WS_TYPE_GET_PANELS}
)
WS_TYPE_GET_THEMES = "frontend/get_themes"
SCHEMA_GET_THEMES = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
{vol.Required("type"): WS_TYPE_GET_THEMES}
)
WS_TYPE_GET_TRANSLATIONS = "frontend/get_translations"
SCHEMA_GET_TRANSLATIONS = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
{vol.Required("type"): WS_TYPE_GET_TRANSLATIONS, vol.Required("language"): str}
)
class Panel:
@ -251,15 +239,9 @@ def _frontend_root(dev_repo_path):
async def async_setup(hass, config):
"""Set up the serving of the frontend."""
await async_setup_frontend_storage(hass)
hass.components.websocket_api.async_register_command(
WS_TYPE_GET_PANELS, websocket_get_panels, SCHEMA_GET_PANELS
)
hass.components.websocket_api.async_register_command(
WS_TYPE_GET_THEMES, websocket_get_themes, SCHEMA_GET_THEMES
)
hass.components.websocket_api.async_register_command(
WS_TYPE_GET_TRANSLATIONS, websocket_get_translations, SCHEMA_GET_TRANSLATIONS
)
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.http.register_view(ManifestJSONView)
conf = config.get(DOMAIN, {})
@ -331,11 +313,7 @@ async def async_setup(hass, config):
def _async_setup_themes(hass, themes):
"""Set up themes data and services."""
hass.data[DATA_DEFAULT_THEME] = DEFAULT_THEME
if themes is None:
hass.data[DATA_THEMES] = {}
return
hass.data[DATA_THEMES] = themes
hass.data[DATA_THEMES] = themes or {}
@callback
def update_theme_and_fire_event():
@ -348,9 +326,7 @@ def _async_setup_themes(hass, themes):
"app-header-background-color",
themes[name].get(PRIMARY_COLOR, DEFAULT_THEME_COLOR),
)
hass.bus.async_fire(
EVENT_THEMES_UPDATED, {"themes": themes, "default_theme": name}
)
hass.bus.async_fire(EVENT_THEMES_UPDATED)
@callback
def set_theme(call):
@ -373,10 +349,17 @@ def _async_setup_themes(hass, themes):
hass.data[DATA_DEFAULT_THEME] = DEFAULT_THEME
update_theme_and_fire_event()
hass.services.async_register(
DOMAIN, SERVICE_SET_THEME, set_theme, schema=SERVICE_SET_THEME_SCHEMA
service.async_register_admin_service(
hass,
DOMAIN,
SERVICE_SET_THEME,
set_theme,
vol.Schema({vol.Required(CONF_NAME): cv.string}),
)
service.async_register_admin_service(
hass, DOMAIN, SERVICE_RELOAD_THEMES, reload_themes
)
hass.services.async_register(DOMAIN, SERVICE_RELOAD_THEMES, reload_themes)
class IndexView(web_urldispatcher.AbstractResource):
@ -498,6 +481,7 @@ class ManifestJSONView(HomeAssistantView):
@callback
@websocket_api.websocket_command({"type": "get_panels"})
def websocket_get_panels(hass, connection, msg):
"""Handle get panels command.
@ -514,6 +498,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.
@ -530,6 +515,9 @@ def websocket_get_themes(hass, connection, msg):
)
@websocket_api.websocket_command(
{"type": "frontend/get_translations", vol.Required("language"): str}
)
@websocket_api.async_response
async def websocket_get_translations(hass, connection, msg):
"""Handle get translations command.