1
mirror of https://github.com/home-assistant/core synced 2024-10-07 10:13:38 +02:00

Provide user-defined actions to app (#38572)

* Start moving stuff to iOS

* Load config on to hass.data

* Remove un used logging

* Switch to Rest API

* Add schema

* Return whole config in new view and leave old 100 % the same

* Update doc strings

* MartinHjelmare feedback

* Move register view to async_setup_entry
This commit is contained in:
Tom Brien 2020-09-01 13:24:23 +01:00 committed by GitHub
parent aa476b392c
commit 151c0d9761
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 20 deletions

View File

@ -12,9 +12,20 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, discovery
from homeassistant.util.json import load_json, save_json
_LOGGER = logging.getLogger(__name__)
from .const import (
CONF_ACTION_BACKGROUND_COLOR,
CONF_ACTION_ICON,
CONF_ACTION_ICON_COLOR,
CONF_ACTION_ICON_ICON,
CONF_ACTION_LABEL,
CONF_ACTION_LABEL_COLOR,
CONF_ACTION_LABEL_TEXT,
CONF_ACTION_NAME,
CONF_ACTIONS,
DOMAIN,
)
DOMAIN = "ios"
_LOGGER = logging.getLogger(__name__)
CONF_PUSH = "push"
CONF_PUSH_CATEGORIES = "categories"
@ -32,6 +43,8 @@ CONF_PUSH_ACTIONS_CONTEXT = "context"
CONF_PUSH_ACTIONS_TEXT_INPUT_BUTTON_TITLE = "textInputButtonTitle"
CONF_PUSH_ACTIONS_TEXT_INPUT_PLACEHOLDER = "textInputPlaceholder"
CONF_USER = "user"
ATTR_FOREGROUND = "foreground"
ATTR_BACKGROUND = "background"
@ -87,7 +100,7 @@ BATTERY_STATES = [
ATTR_DEVICES = "devices"
ACTION_SCHEMA = vol.Schema(
PUSH_ACTION_SCHEMA = vol.Schema(
{
vol.Required(CONF_PUSH_ACTIONS_IDENTIFIER): vol.Upper,
vol.Required(CONF_PUSH_ACTIONS_TITLE): cv.string,
@ -107,25 +120,40 @@ ACTION_SCHEMA = vol.Schema(
extra=vol.ALLOW_EXTRA,
)
ACTION_SCHEMA_LIST = vol.All(cv.ensure_list, [ACTION_SCHEMA])
PUSH_ACTION_LIST_SCHEMA = vol.All(cv.ensure_list, [PUSH_ACTION_SCHEMA])
PUSH_CATEGORY_SCHEMA = vol.Schema(
{
vol.Required(CONF_PUSH_CATEGORIES_NAME): cv.string,
vol.Required(CONF_PUSH_CATEGORIES_IDENTIFIER): vol.Lower,
vol.Required(CONF_PUSH_CATEGORIES_ACTIONS): PUSH_ACTION_LIST_SCHEMA,
}
)
PUSH_CATEGORY_LIST_SCHEMA = vol.All(cv.ensure_list, [PUSH_CATEGORY_SCHEMA])
ACTION_SCHEMA = vol.Schema(
{
vol.Required(CONF_ACTION_NAME): cv.string,
vol.Optional(CONF_ACTION_BACKGROUND_COLOR): cv.string,
vol.Optional(CONF_ACTION_LABEL): {
vol.Optional(CONF_ACTION_LABEL_TEXT): cv.string,
vol.Optional(CONF_ACTION_LABEL_COLOR): cv.string,
},
vol.Optional(CONF_ACTION_ICON): {
vol.Optional(CONF_ACTION_ICON_ICON): cv.string,
vol.Optional(CONF_ACTION_ICON_COLOR): cv.string,
},
},
)
ACTION_LIST_SCHEMA = vol.All(cv.ensure_list, [ACTION_SCHEMA])
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: {
CONF_PUSH: {
CONF_PUSH_CATEGORIES: vol.All(
cv.ensure_list,
[
{
vol.Required(CONF_PUSH_CATEGORIES_NAME): cv.string,
vol.Required(CONF_PUSH_CATEGORIES_IDENTIFIER): vol.Lower,
vol.Required(
CONF_PUSH_CATEGORIES_ACTIONS
): ACTION_SCHEMA_LIST,
}
],
)
}
CONF_PUSH: {CONF_PUSH_CATEGORIES: PUSH_CATEGORY_LIST_SCHEMA},
CONF_ACTIONS: ACTION_LIST_SCHEMA,
}
},
extra=vol.ALLOW_EXTRA,
@ -226,7 +254,10 @@ async def async_setup(hass, config):
if ios_config == {}:
ios_config[ATTR_DEVICES] = {}
ios_config[CONF_PUSH] = (conf or {}).get(CONF_PUSH, {})
ios_config[CONF_USER] = conf or {}
if CONF_PUSH not in ios_config[CONF_USER]:
ios_config[CONF_USER][CONF_PUSH] = {}
hass.data[DOMAIN] = ios_config
@ -250,7 +281,8 @@ async def async_setup_entry(hass, entry):
)
hass.http.register_view(iOSIdentifyDeviceView(hass.config.path(CONFIGURATION_FILE)))
hass.http.register_view(iOSPushConfigView(hass.data[DOMAIN][CONF_PUSH]))
hass.http.register_view(iOSPushConfigView(hass.data[DOMAIN][CONF_USER][CONF_PUSH]))
hass.http.register_view(iOSConfigView(hass.data[DOMAIN][CONF_USER]))
return True
@ -272,6 +304,22 @@ class iOSPushConfigView(HomeAssistantView):
return self.json(self.push_config)
class iOSConfigView(HomeAssistantView):
"""A view that provides the whole user-defined configuration."""
url = "/api/ios/config"
name = "api:ios:config"
def __init__(self, config):
"""Init the view."""
self.config = config
@callback
def get(self, request):
"""Handle the GET request for the user-defined configuration."""
return self.json(self.config)
class iOSIdentifyDeviceView(HomeAssistantView):
"""A view that accepts device identification requests."""

View File

@ -1,3 +1,13 @@
"""Const for iOS."""
DOMAIN = "ios"
CONF_ACTION_NAME = "name"
CONF_ACTION_BACKGROUND_COLOR = "background_color"
CONF_ACTION_LABEL = "label"
CONF_ACTION_LABEL_COLOR = "color"
CONF_ACTION_LABEL_TEXT = "text"
CONF_ACTION_ICON = "icon"
CONF_ACTION_ICON_COLOR = "color"
CONF_ACTION_ICON_ICON = "icon"
CONF_ACTIONS = "actions"