Add panel custom to load any webcomponent (#2747)
This commit is contained in:
parent
9a575eb6d6
commit
8081fe794e
5 changed files with 161 additions and 31 deletions
64
homeassistant/components/panel_custom.py
Normal file
64
homeassistant/components/panel_custom.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
"""Register a custom front end panel."""
|
||||
import logging
|
||||
import os
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.frontend import register_panel
|
||||
|
||||
DOMAIN = 'panel_custom'
|
||||
DEPENDENCIES = ['frontend']
|
||||
|
||||
CONF_COMPONENT_NAME = 'name'
|
||||
CONF_SIDEBAR_TITLE = 'sidebar_title'
|
||||
CONF_SIDEBAR_ICON = 'sidebar_icon'
|
||||
CONF_URL_PATH = 'url_path'
|
||||
CONF_CONFIG = 'config'
|
||||
CONF_WEBCOMPONENT_PATH = 'webcomponent_path'
|
||||
|
||||
DEFAULT_ICON = 'mdi:bookmark'
|
||||
|
||||
PANEL_DIR = 'panels'
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.All(cv.ensure_list, [{
|
||||
vol.Required(CONF_COMPONENT_NAME): cv.slug,
|
||||
vol.Optional(CONF_SIDEBAR_TITLE): cv.string,
|
||||
vol.Optional(CONF_SIDEBAR_ICON, default=DEFAULT_ICON): cv.icon,
|
||||
vol.Optional(CONF_URL_PATH): cv.string,
|
||||
vol.Optional(CONF_CONFIG): cv.match_all,
|
||||
vol.Optional(CONF_WEBCOMPONENT_PATH): cv.isfile,
|
||||
}])
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
"""Initialize custom panel."""
|
||||
success = False
|
||||
|
||||
for panel in config.get(DOMAIN):
|
||||
name = panel.get(CONF_COMPONENT_NAME)
|
||||
panel_path = panel.get(CONF_WEBCOMPONENT_PATH)
|
||||
|
||||
if panel_path is None:
|
||||
panel_path = hass.config.path(PANEL_DIR, '{}.html'.format(name))
|
||||
|
||||
if not os.path.isfile(panel_path):
|
||||
_LOGGER.error('Unable to find webcomponent for %s: %s',
|
||||
name, panel_path)
|
||||
continue
|
||||
|
||||
register_panel(
|
||||
hass, name, panel_path,
|
||||
sidebar_title=panel.get(CONF_SIDEBAR_TITLE),
|
||||
sidebar_icon=panel.get(CONF_SIDEBAR_ICON),
|
||||
url_path=panel.get(CONF_URL_PATH),
|
||||
config=panel.get(CONF_CONFIG),
|
||||
)
|
||||
|
||||
success = True
|
||||
|
||||
return success
|
Loading…
Add table
Add a link
Reference in a new issue