Add websockets commands for resource management

This commit is contained in:
Bram Kragten
2020-02-25 16:56:04 +01:00
parent 644bd73290
commit 7d140b2bcc
3 changed files with 51 additions and 7 deletions

View File

@@ -7,7 +7,7 @@ import voluptuous as vol
from homeassistant.components import frontend
from homeassistant.const import CONF_FILENAME, CONF_ICON
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import collection, config_validation as cv
from homeassistant.util import sanitize_filename, slugify
from . import dashboard, resources, websocket
@@ -17,7 +17,9 @@ from .const import (
LOVELACE_CONFIG_FILE,
MODE_STORAGE,
MODE_YAML,
RESOURCE_CREATE_FIELDS,
RESOURCE_SCHEMA,
RESOURCE_UPDATE_FIELDS,
)
_LOGGER = logging.getLogger(__name__)
@@ -111,6 +113,14 @@ async def async_setup(hass, config):
resource_collection = resources.ResourceStorageCollection(hass, default_config)
collection.StorageCollectionWebsocket(
resource_collection,
"lovelace/resources",
"lovelace_resource",
RESOURCE_CREATE_FIELDS,
RESOURCE_UPDATE_FIELDS,
).async_setup(hass, create_list=False)
hass.components.websocket_api.async_register_command(
websocket.websocket_lovelace_config
)

View File

@@ -14,13 +14,27 @@ MODE_STORAGE = "storage"
LOVELACE_CONFIG_FILE = "ui-lovelace.yaml"
CONF_RESOURCES = "resources"
CONF_URL_PATH = "url_path"
CONF_TYPE_WS = "res_type"
RESOURCE_TYPES = ["js", "css", "module", "html"]
RESOURCE_FIELDS = {
CONF_TYPE: vol.In(["js", "css", "module", "html"]),
CONF_TYPE: vol.In(RESOURCE_TYPES),
CONF_URL: cv.string,
}
RESOURCE_SCHEMA = vol.Schema(RESOURCE_FIELDS)
RESOURCE_CREATE_FIELDS = {
vol.Required(CONF_TYPE_WS): vol.In(RESOURCE_TYPES),
vol.Required(CONF_URL): cv.string,
}
RESOURCE_UPDATE_FIELDS = {
vol.Optional(CONF_TYPE_WS): vol.In(RESOURCE_TYPES),
vol.Optional(CONF_URL): cv.string,
}
class ConfigNotFound(HomeAssistantError):
"""When no config available."""

View File

@@ -5,11 +5,19 @@ import uuid
import voluptuous as vol
from homeassistant.const import CONF_TYPE
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import collection, storage
from .const import CONF_RESOURCES, DOMAIN, RESOURCE_SCHEMA
from .const import (
CONF_RESOURCES,
CONF_TYPE_WS,
DOMAIN,
RESOURCE_CREATE_FIELDS,
RESOURCE_SCHEMA,
RESOURCE_UPDATE_FIELDS,
)
from .dashboard import LovelaceConfig
RESOURCE_STORAGE_KEY = f"{DOMAIN}_resources"
@@ -36,6 +44,8 @@ class ResourceStorageCollection(collection.StorageCollection):
"""Collection to store resources."""
loaded = False
CREATE_SCHEMA = vol.Schema(RESOURCE_CREATE_FIELDS)
UPDATE_SCHEMA = vol.Schema(RESOURCE_UPDATE_FIELDS)
def __init__(self, hass: HomeAssistant, ll_config: LovelaceConfig):
"""Initialize the storage collection."""
@@ -84,13 +94,23 @@ class ResourceStorageCollection(collection.StorageCollection):
async def _process_create_data(self, data: dict) -> dict:
"""Validate the config is valid."""
raise NotImplementedError
data = self.CREATE_SCHEMA(data)
data[CONF_TYPE] = data.pop(CONF_TYPE_WS)
return data
@callback
def _get_suggested_id(self, info: dict) -> str:
"""Suggest an ID based on the config."""
raise NotImplementedError
"""Return unique ID."""
return uuid.uuid4().hex
async def _update_data(self, data: dict, update_data: dict) -> dict:
"""Return a new updated data object."""
raise NotImplementedError
if not self.loaded:
await self.async_load()
self.loaded = True
update_data = self.UPDATE_SCHEMA(update_data)
if CONF_TYPE_WS in update_data:
update_data[CONF_TYPE] = update_data.pop(CONF_TYPE_WS)
return {**data, **update_data}