2016-03-09 10:15:04 +00:00
|
|
|
"""Handle the frontend for Home Assistant."""
|
2016-10-24 06:48:01 +00:00
|
|
|
import asyncio
|
2016-07-29 07:49:58 +00:00
|
|
|
import hashlib
|
2016-10-24 06:48:01 +00:00
|
|
|
import json
|
2016-07-17 05:32:25 +00:00
|
|
|
import logging
|
2015-01-30 07:56:04 +00:00
|
|
|
import os
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
from aiohttp import web
|
2017-07-13 01:08:13 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-10-24 06:48:01 +00:00
|
|
|
|
2017-07-13 01:08:13 +00:00
|
|
|
from homeassistant.config import find_config_file, load_yaml_config_file
|
|
|
|
from homeassistant.const import CONF_NAME, EVENT_THEMES_UPDATED
|
2016-10-24 06:48:01 +00:00
|
|
|
from homeassistant.core import callback
|
2017-07-22 04:38:53 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
2016-05-14 07:58:36 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView
|
2016-11-25 21:04:06 +00:00
|
|
|
from homeassistant.components.http.auth import is_trusted_ip
|
2015-01-30 07:56:04 +00:00
|
|
|
|
|
|
|
DOMAIN = 'frontend'
|
2016-11-27 20:13:01 +00:00
|
|
|
DEPENDENCIES = ['api', 'websocket_api']
|
2017-10-30 06:29:49 +00:00
|
|
|
REQUIREMENTS = ['home-assistant-frontend==20171030.0']
|
2017-04-30 05:04:49 +00:00
|
|
|
|
2016-07-17 05:32:25 +00:00
|
|
|
URL_PANEL_COMPONENT = '/frontend/panels/{}.html'
|
|
|
|
URL_PANEL_COMPONENT_FP = '/frontend/panels/{}-{}.html'
|
2017-04-30 05:04:49 +00:00
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
POLYMER_PATH = os.path.join(os.path.dirname(__file__),
|
|
|
|
'home-assistant-polymer/')
|
|
|
|
FINAL_PATH = os.path.join(POLYMER_PATH, 'final')
|
2017-04-30 05:04:49 +00:00
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
CONF_THEMES = 'themes'
|
|
|
|
CONF_EXTRA_HTML_URL = 'extra_html_url'
|
|
|
|
CONF_FRONTEND_REPO = 'development_repo'
|
2017-07-13 01:08:13 +00:00
|
|
|
DEFAULT_THEME_COLOR = '#03A9F4'
|
2016-08-14 08:10:07 +00:00
|
|
|
MANIFEST_JSON = {
|
2017-04-30 05:04:49 +00:00
|
|
|
'background_color': '#FFFFFF',
|
|
|
|
'description': 'Open-source home automation platform running on Python 3.',
|
|
|
|
'dir': 'ltr',
|
|
|
|
'display': 'standalone',
|
|
|
|
'icons': [],
|
|
|
|
'lang': 'en-US',
|
|
|
|
'name': 'Home Assistant',
|
2017-07-01 04:57:38 +00:00
|
|
|
'short_name': 'Assistant',
|
2017-04-30 05:04:49 +00:00
|
|
|
'start_url': '/',
|
2017-07-13 01:08:13 +00:00
|
|
|
'theme_color': DEFAULT_THEME_COLOR
|
2016-08-14 08:10:07 +00:00
|
|
|
}
|
2016-07-20 06:36:46 +00:00
|
|
|
|
2016-11-25 05:37:56 +00:00
|
|
|
for size in (192, 384, 512, 1024):
|
|
|
|
MANIFEST_JSON['icons'].append({
|
2017-04-30 05:04:49 +00:00
|
|
|
'src': '/static/icons/favicon-{}x{}.png'.format(size, size),
|
|
|
|
'sizes': '{}x{}'.format(size, size),
|
|
|
|
'type': 'image/png'
|
2016-11-25 05:37:56 +00:00
|
|
|
})
|
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
DATA_FINALIZE_PANEL = 'frontend_finalize_panel'
|
2016-11-25 05:37:56 +00:00
|
|
|
DATA_PANELS = 'frontend_panels'
|
2017-08-27 16:07:58 +00:00
|
|
|
DATA_EXTRA_HTML_URL = 'frontend_extra_html_url'
|
2017-07-13 01:08:13 +00:00
|
|
|
DATA_THEMES = 'frontend_themes'
|
|
|
|
DATA_DEFAULT_THEME = 'frontend_default_theme'
|
|
|
|
DEFAULT_THEME = 'default'
|
|
|
|
|
|
|
|
PRIMARY_COLOR = 'primary-color'
|
2016-11-25 05:37:56 +00:00
|
|
|
|
2016-07-20 06:36:46 +00:00
|
|
|
# To keep track we don't register a component twice (gives a warning)
|
2017-10-25 02:36:27 +00:00
|
|
|
# _REGISTERED_COMPONENTS = set()
|
2016-07-17 05:32:25 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-07-13 01:08:13 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
2017-10-25 02:36:27 +00:00
|
|
|
vol.Optional(CONF_FRONTEND_REPO): cv.isdir,
|
|
|
|
vol.Optional(CONF_THEMES): vol.Schema({
|
2017-07-13 01:08:13 +00:00
|
|
|
cv.string: {cv.string: cv.string}
|
|
|
|
}),
|
2017-10-25 02:36:27 +00:00
|
|
|
vol.Optional(CONF_EXTRA_HTML_URL):
|
2017-08-27 16:07:58 +00:00
|
|
|
vol.All(cv.ensure_list, [cv.string]),
|
2017-07-13 01:08:13 +00:00
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
SERVICE_SET_THEME = 'set_theme'
|
|
|
|
SERVICE_RELOAD_THEMES = 'reload_themes'
|
|
|
|
SERVICE_SET_THEME_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
})
|
|
|
|
|
2016-07-17 05:32:25 +00:00
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
class AbstractPanel:
|
|
|
|
"""Abstract class for panels."""
|
|
|
|
|
|
|
|
# Name of the webcomponent
|
|
|
|
component_name = None
|
|
|
|
|
|
|
|
# Icon to show in the sidebar (optional)
|
|
|
|
sidebar_icon = None
|
|
|
|
|
|
|
|
# Title to show in the sidebar (optional)
|
|
|
|
sidebar_title = None
|
|
|
|
|
|
|
|
# Url to the webcomponent
|
|
|
|
webcomponent_url = None
|
|
|
|
|
|
|
|
# Url to show the panel in the frontend
|
|
|
|
frontend_url_path = None
|
|
|
|
|
|
|
|
# Config to pass to the webcomponent
|
|
|
|
config = None
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_register(self, hass):
|
|
|
|
"""Register panel with HASS."""
|
|
|
|
panels = hass.data.get(DATA_PANELS)
|
|
|
|
if panels is None:
|
|
|
|
panels = hass.data[DATA_PANELS] = {}
|
|
|
|
|
|
|
|
if self.frontend_url_path in panels:
|
|
|
|
_LOGGER.warning("Overwriting component %s", self.frontend_url_path)
|
|
|
|
|
|
|
|
if DATA_FINALIZE_PANEL in hass.data:
|
|
|
|
yield from hass.data[DATA_FINALIZE_PANEL](self)
|
|
|
|
|
|
|
|
panels[self.frontend_url_path] = self
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_register_index_routes(self, router, index_view):
|
|
|
|
"""Register routes for panel to be served by index view."""
|
|
|
|
router.add_route(
|
|
|
|
'get', '/{}'.format(self.frontend_url_path), index_view.get)
|
|
|
|
router.add_route(
|
|
|
|
'get', '/{}/{{extra:.+}}'.format(self.frontend_url_path),
|
|
|
|
index_view.get)
|
|
|
|
|
|
|
|
def as_dict(self):
|
|
|
|
"""Panel as dictionary."""
|
|
|
|
return {
|
|
|
|
'component_name': self.component_name,
|
|
|
|
'icon': self.sidebar_icon,
|
|
|
|
'title': self.sidebar_title,
|
|
|
|
'url': self.webcomponent_url,
|
|
|
|
'url_path': self.frontend_url_path,
|
|
|
|
'config': self.config,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class BuiltInPanel(AbstractPanel):
|
|
|
|
"""Panel that is part of hass_frontend."""
|
|
|
|
|
|
|
|
def __init__(self, component_name, sidebar_title, sidebar_icon,
|
|
|
|
frontend_url_path, config):
|
|
|
|
"""Initialize a built-in panel."""
|
|
|
|
self.component_name = component_name
|
|
|
|
self.sidebar_title = sidebar_title
|
|
|
|
self.sidebar_icon = sidebar_icon
|
|
|
|
self.frontend_url_path = frontend_url_path or component_name
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_finalize(self, hass, frontend_repository_path):
|
|
|
|
"""Finalize this panel for usage.
|
|
|
|
|
|
|
|
If frontend_repository_path is set, will be prepended to path of
|
|
|
|
built-in components.
|
|
|
|
"""
|
|
|
|
panel_path = 'panels/ha-panel-{}.html'.format(self.component_name)
|
|
|
|
|
|
|
|
if frontend_repository_path is None:
|
|
|
|
import hass_frontend
|
|
|
|
|
|
|
|
self.webcomponent_url = \
|
|
|
|
'/static/panels/ha-panel-{}-{}.html'.format(
|
|
|
|
self.component_name,
|
|
|
|
hass_frontend.FINGERPRINTS[panel_path])
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Dev mode
|
|
|
|
self.webcomponent_url = \
|
|
|
|
'/home-assistant-polymer/panels/{}/ha-panel-{}.html'.format(
|
|
|
|
self.component_name, self.component_name)
|
|
|
|
|
|
|
|
|
|
|
|
class ExternalPanel(AbstractPanel):
|
|
|
|
"""Panel that is added by a custom component."""
|
|
|
|
|
|
|
|
REGISTERED_COMPONENTS = set()
|
|
|
|
|
|
|
|
def __init__(self, component_name, path, md5, sidebar_title, sidebar_icon,
|
|
|
|
frontend_url_path, config):
|
|
|
|
"""Initialize an external panel."""
|
|
|
|
self.component_name = component_name
|
|
|
|
self.path = path
|
|
|
|
self.md5 = md5
|
|
|
|
self.sidebar_title = sidebar_title
|
|
|
|
self.sidebar_icon = sidebar_icon
|
|
|
|
self.frontend_url_path = frontend_url_path or component_name
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_finalize(self, hass, frontend_repository_path):
|
|
|
|
"""Finalize this panel for usage.
|
|
|
|
|
|
|
|
frontend_repository_path is set, will be prepended to path of built-in
|
|
|
|
components.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if self.md5 is None:
|
|
|
|
yield from hass.async_add_job(_fingerprint, self.path)
|
|
|
|
except OSError:
|
|
|
|
_LOGGER.error('Cannot find or access %s at %s',
|
|
|
|
self.component_name, self.path)
|
|
|
|
hass.data[DATA_PANELS].pop(self.frontend_url_path)
|
|
|
|
|
|
|
|
self.webcomponent_url = \
|
|
|
|
URL_PANEL_COMPONENT_FP.format(self.component_name, self.md5)
|
|
|
|
|
|
|
|
if self.component_name not in self.REGISTERED_COMPONENTS:
|
|
|
|
hass.http.register_static_path(self.webcomponent_url, self.path)
|
|
|
|
self.REGISTERED_COMPONENTS.add(self.component_name)
|
|
|
|
|
|
|
|
|
2017-07-22 04:38:53 +00:00
|
|
|
@bind_hass
|
2017-10-25 02:36:27 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_register_built_in_panel(hass, component_name, sidebar_title=None,
|
|
|
|
sidebar_icon=None, frontend_url_path=None,
|
|
|
|
config=None):
|
2016-07-17 05:32:25 +00:00
|
|
|
"""Register a built-in panel."""
|
2017-10-25 02:36:27 +00:00
|
|
|
panel = BuiltInPanel(component_name, sidebar_title, sidebar_icon,
|
|
|
|
frontend_url_path, config)
|
|
|
|
yield from panel.async_register(hass)
|
2016-07-17 05:32:25 +00:00
|
|
|
|
|
|
|
|
2017-07-22 04:38:53 +00:00
|
|
|
@bind_hass
|
2017-10-25 02:36:27 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_register_panel(hass, component_name, path, md5=None,
|
|
|
|
sidebar_title=None, sidebar_icon=None,
|
|
|
|
frontend_url_path=None, config=None):
|
2016-07-17 05:32:25 +00:00
|
|
|
"""Register a panel for the frontend.
|
|
|
|
|
|
|
|
component_name: name of the web component
|
|
|
|
path: path to the HTML of the web component
|
2017-08-31 04:21:24 +00:00
|
|
|
(required unless url is provided)
|
2017-10-25 02:36:27 +00:00
|
|
|
md5: the md5 hash of the web component (for versioning in url, optional)
|
2016-08-08 04:56:17 +00:00
|
|
|
sidebar_title: title to show in the sidebar (optional)
|
|
|
|
sidebar_icon: icon to show next to title in sidebar (optional)
|
|
|
|
url_path: name to use in the url (defaults to component_name)
|
2016-07-17 05:32:25 +00:00
|
|
|
config: config to be passed into the web component
|
|
|
|
"""
|
2017-10-25 02:36:27 +00:00
|
|
|
panel = ExternalPanel(component_name, path, md5, sidebar_title,
|
|
|
|
sidebar_icon, frontend_url_path, config)
|
|
|
|
yield from panel.async_register(hass)
|
2016-07-17 05:32:25 +00:00
|
|
|
|
2015-06-24 06:22:32 +00:00
|
|
|
|
2017-08-27 16:07:58 +00:00
|
|
|
@bind_hass
|
2017-10-25 02:36:27 +00:00
|
|
|
@callback
|
2017-08-27 16:07:58 +00:00
|
|
|
def add_extra_html_url(hass, url):
|
|
|
|
"""Register extra html url to load."""
|
|
|
|
url_set = hass.data.get(DATA_EXTRA_HTML_URL)
|
|
|
|
if url_set is None:
|
|
|
|
url_set = hass.data[DATA_EXTRA_HTML_URL] = set()
|
|
|
|
url_set.add(url)
|
|
|
|
|
|
|
|
|
2016-08-14 08:10:07 +00:00
|
|
|
def add_manifest_json_key(key, val):
|
|
|
|
"""Add a keyval to the manifest.json."""
|
|
|
|
MANIFEST_JSON[key] = val
|
|
|
|
|
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the serving of the frontend."""
|
2016-10-24 06:48:01 +00:00
|
|
|
hass.http.register_view(ManifestJSONView)
|
2016-05-10 01:09:38 +00:00
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
conf = config.get(DOMAIN, {})
|
|
|
|
|
|
|
|
repo_path = conf.get(CONF_FRONTEND_REPO)
|
|
|
|
is_dev = repo_path is not None
|
|
|
|
|
|
|
|
if is_dev:
|
|
|
|
hass.http.register_static_path("/home-assistant-polymer", repo_path)
|
2017-10-27 04:46:21 +00:00
|
|
|
hass.http.register_static_path(
|
|
|
|
"/static/translations",
|
|
|
|
os.path.join(repo_path, "build/translations"))
|
2017-10-25 02:36:27 +00:00
|
|
|
sw_path = os.path.join(repo_path, "build/service_worker.js")
|
|
|
|
static_path = os.path.join(repo_path, 'hass_frontend')
|
2016-05-10 01:09:38 +00:00
|
|
|
else:
|
2017-10-27 05:28:07 +00:00
|
|
|
import hass_frontend
|
|
|
|
frontend_path = hass_frontend.where()
|
2017-10-25 02:36:27 +00:00
|
|
|
sw_path = os.path.join(frontend_path, "service_worker.js")
|
|
|
|
static_path = frontend_path
|
2016-05-10 01:09:38 +00:00
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
hass.http.register_static_path("/service_worker.js", sw_path, False)
|
2016-10-24 06:48:01 +00:00
|
|
|
hass.http.register_static_path("/robots.txt",
|
2017-10-27 05:28:07 +00:00
|
|
|
os.path.join(static_path, "robots.txt"))
|
2017-10-25 02:36:27 +00:00
|
|
|
hass.http.register_static_path("/static", static_path)
|
2016-10-24 06:48:01 +00:00
|
|
|
|
|
|
|
local = hass.config.path('www')
|
|
|
|
if os.path.isdir(local):
|
|
|
|
hass.http.register_static_path("/local", local)
|
2016-05-10 01:09:38 +00:00
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
index_view = IndexView(is_dev)
|
2016-11-25 05:37:56 +00:00
|
|
|
hass.http.register_view(index_view)
|
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def finalize_panel(panel):
|
|
|
|
"""Finalize setup of a panel."""
|
|
|
|
yield from panel.async_finalize(hass, repo_path)
|
|
|
|
panel.async_register_index_routes(hass.http.app.router, index_view)
|
2016-11-25 05:37:56 +00:00
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
yield from asyncio.wait([
|
|
|
|
async_register_built_in_panel(hass, panel)
|
|
|
|
for panel in ('dev-event', 'dev-info', 'dev-service', 'dev-state',
|
|
|
|
'dev-template', 'dev-mqtt', 'kiosk')], loop=hass.loop)
|
2017-08-27 16:07:58 +00:00
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
hass.data[DATA_FINALIZE_PANEL] = finalize_panel
|
2016-07-17 05:32:25 +00:00
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
# Finalize registration of panels that registered before frontend was setup
|
|
|
|
# This includes the built-in panels from line above.
|
|
|
|
yield from asyncio.wait(
|
|
|
|
[finalize_panel(panel) for panel in hass.data[DATA_PANELS].values()],
|
|
|
|
loop=hass.loop)
|
2017-07-13 01:08:13 +00:00
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
if DATA_EXTRA_HTML_URL not in hass.data:
|
|
|
|
hass.data[DATA_EXTRA_HTML_URL] = set()
|
|
|
|
|
|
|
|
for url in conf.get(CONF_EXTRA_HTML_URL, []):
|
2017-08-27 16:07:58 +00:00
|
|
|
add_extra_html_url(hass, url)
|
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
yield from async_setup_themes(hass, conf.get(CONF_THEMES))
|
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup_themes(hass, themes):
|
2017-07-13 01:08:13 +00:00
|
|
|
"""Set up themes data and services."""
|
|
|
|
hass.http.register_view(ThemesView)
|
2017-07-14 18:26:26 +00:00
|
|
|
hass.data[DATA_DEFAULT_THEME] = DEFAULT_THEME
|
|
|
|
if themes is None:
|
|
|
|
hass.data[DATA_THEMES] = {}
|
|
|
|
return
|
|
|
|
|
|
|
|
hass.data[DATA_THEMES] = themes
|
2017-07-13 01:08:13 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_theme_and_fire_event():
|
|
|
|
"""Update theme_color in manifest."""
|
|
|
|
name = hass.data[DATA_DEFAULT_THEME]
|
|
|
|
themes = hass.data[DATA_THEMES]
|
|
|
|
if name != DEFAULT_THEME and PRIMARY_COLOR in themes[name]:
|
|
|
|
MANIFEST_JSON['theme_color'] = themes[name][PRIMARY_COLOR]
|
|
|
|
else:
|
|
|
|
MANIFEST_JSON['theme_color'] = DEFAULT_THEME_COLOR
|
|
|
|
hass.bus.async_fire(EVENT_THEMES_UPDATED, {
|
|
|
|
'themes': themes,
|
|
|
|
'default_theme': name,
|
|
|
|
})
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def set_theme(call):
|
|
|
|
"""Set backend-prefered theme."""
|
|
|
|
data = call.data
|
|
|
|
name = data[CONF_NAME]
|
|
|
|
if name == DEFAULT_THEME or name in hass.data[DATA_THEMES]:
|
|
|
|
_LOGGER.info("Theme %s set as default", name)
|
|
|
|
hass.data[DATA_DEFAULT_THEME] = name
|
|
|
|
update_theme_and_fire_event()
|
|
|
|
else:
|
|
|
|
_LOGGER.warning("Theme %s is not defined.", name)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def reload_themes(_):
|
|
|
|
"""Reload themes."""
|
|
|
|
path = find_config_file(hass.config.config_dir)
|
2017-10-25 02:36:27 +00:00
|
|
|
new_themes = load_yaml_config_file(path)[DOMAIN].get(CONF_THEMES, {})
|
2017-07-13 01:08:13 +00:00
|
|
|
hass.data[DATA_THEMES] = new_themes
|
|
|
|
if hass.data[DATA_DEFAULT_THEME] not in new_themes:
|
|
|
|
hass.data[DATA_DEFAULT_THEME] = DEFAULT_THEME
|
|
|
|
update_theme_and_fire_event()
|
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
descriptions = yield from hass.async_add_job(
|
|
|
|
load_yaml_config_file,
|
2017-07-13 01:08:13 +00:00
|
|
|
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
hass.services.async_register(DOMAIN, SERVICE_SET_THEME,
|
|
|
|
set_theme,
|
|
|
|
descriptions[SERVICE_SET_THEME],
|
|
|
|
SERVICE_SET_THEME_SCHEMA)
|
|
|
|
hass.services.async_register(DOMAIN, SERVICE_RELOAD_THEMES, reload_themes,
|
|
|
|
descriptions[SERVICE_RELOAD_THEMES])
|
2016-05-10 01:09:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IndexView(HomeAssistantView):
|
2016-05-14 07:58:36 +00:00
|
|
|
"""Serve the frontend."""
|
|
|
|
|
2016-05-28 04:45:38 +00:00
|
|
|
url = '/'
|
2017-04-30 05:04:49 +00:00
|
|
|
name = 'frontend:index'
|
2016-05-14 07:58:36 +00:00
|
|
|
requires_auth = False
|
2017-07-07 03:58:21 +00:00
|
|
|
extra_urls = ['/states', '/states/{extra}']
|
2016-05-10 01:09:38 +00:00
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
def __init__(self, use_repo):
|
2016-05-14 07:58:36 +00:00
|
|
|
"""Initialize the frontend view."""
|
2016-05-12 05:55:24 +00:00
|
|
|
from jinja2 import FileSystemLoader, Environment
|
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
self.use_repo = use_repo
|
2016-05-14 07:58:36 +00:00
|
|
|
self.templates = Environment(
|
2017-10-13 05:01:29 +00:00
|
|
|
autoescape=True,
|
2016-05-12 05:55:24 +00:00
|
|
|
loader=FileSystemLoader(
|
|
|
|
os.path.join(os.path.dirname(__file__), 'templates/')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
@asyncio.coroutine
|
2017-07-07 03:58:21 +00:00
|
|
|
def get(self, request, extra=None):
|
2016-05-12 05:55:24 +00:00
|
|
|
"""Serve the index view."""
|
2016-11-25 21:04:06 +00:00
|
|
|
hass = request.app['hass']
|
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
if self.use_repo:
|
|
|
|
core_url = '/home-assistant-polymer/build/core.js'
|
2017-02-14 20:00:45 +00:00
|
|
|
compatibility_url = \
|
2017-10-25 02:36:27 +00:00
|
|
|
'/home-assistant-polymer/build/compatibility.js'
|
|
|
|
ui_url = '/home-assistant-polymer/src/home-assistant.html'
|
2017-10-27 05:28:07 +00:00
|
|
|
icons_fp = ''
|
|
|
|
icons_url = '/static/mdi.html'
|
2016-05-14 22:07:20 +00:00
|
|
|
else:
|
2017-10-27 05:28:07 +00:00
|
|
|
import hass_frontend
|
2016-07-17 05:32:25 +00:00
|
|
|
core_url = '/static/core-{}.js'.format(
|
2017-10-25 02:36:27 +00:00
|
|
|
hass_frontend.FINGERPRINTS['core.js'])
|
2017-02-14 20:00:45 +00:00
|
|
|
compatibility_url = '/static/compatibility-{}.js'.format(
|
2017-10-25 02:36:27 +00:00
|
|
|
hass_frontend.FINGERPRINTS['compatibility.js'])
|
2016-07-17 05:32:25 +00:00
|
|
|
ui_url = '/static/frontend-{}.html'.format(
|
2017-10-25 02:36:27 +00:00
|
|
|
hass_frontend.FINGERPRINTS['frontend.html'])
|
2017-10-27 05:28:07 +00:00
|
|
|
icons_fp = '-{}'.format(hass_frontend.FINGERPRINTS['mdi.html'])
|
|
|
|
icons_url = '/static/mdi{}.html'.format(icons_fp)
|
2016-05-10 01:09:38 +00:00
|
|
|
|
2016-07-20 06:36:46 +00:00
|
|
|
if request.path == '/':
|
|
|
|
panel = 'states'
|
|
|
|
else:
|
|
|
|
panel = request.path.split('/')[1]
|
|
|
|
|
2016-11-25 05:37:56 +00:00
|
|
|
if panel == 'states':
|
|
|
|
panel_url = ''
|
|
|
|
else:
|
2017-10-25 02:36:27 +00:00
|
|
|
panel_url = hass.data[DATA_PANELS][panel].webcomponent_url
|
2016-07-20 06:36:46 +00:00
|
|
|
|
2016-10-09 15:13:30 +00:00
|
|
|
no_auth = 'true'
|
2017-10-25 02:36:27 +00:00
|
|
|
if hass.config.api.api_password and not is_trusted_ip(request):
|
|
|
|
# do not try to auto connect on load
|
2016-10-09 15:13:30 +00:00
|
|
|
no_auth = 'false'
|
2016-05-10 01:09:38 +00:00
|
|
|
|
2017-05-26 15:28:07 +00:00
|
|
|
template = yield from hass.async_add_job(
|
|
|
|
self.templates.get_template, 'index.html')
|
2016-05-10 01:09:38 +00:00
|
|
|
|
2016-05-16 07:25:47 +00:00
|
|
|
# pylint is wrong
|
|
|
|
# pylint: disable=no-member
|
2016-10-24 06:48:01 +00:00
|
|
|
# This is a jinja2 template, not a HA template so we call 'render'.
|
2016-05-29 01:38:46 +00:00
|
|
|
resp = template.render(
|
2017-02-14 20:00:45 +00:00
|
|
|
core_url=core_url, ui_url=ui_url,
|
|
|
|
compatibility_url=compatibility_url, no_auth=no_auth,
|
2017-10-25 02:36:27 +00:00
|
|
|
icons_url=icons_url, icons=icons_fp,
|
2017-08-04 06:46:57 +00:00
|
|
|
panel_url=panel_url, panels=hass.data[DATA_PANELS],
|
2017-10-25 02:36:27 +00:00
|
|
|
dev_mode=self.use_repo,
|
2017-08-27 16:07:58 +00:00
|
|
|
theme_color=MANIFEST_JSON['theme_color'],
|
|
|
|
extra_urls=hass.data[DATA_EXTRA_HTML_URL])
|
2016-05-10 01:09:38 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
return web.Response(text=resp, content_type='text/html')
|
2016-08-14 08:10:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManifestJSONView(HomeAssistantView):
|
|
|
|
"""View to return a manifest.json."""
|
|
|
|
|
|
|
|
requires_auth = False
|
2017-04-30 05:04:49 +00:00
|
|
|
url = '/manifest.json'
|
|
|
|
name = 'manifestjson'
|
2016-08-14 08:10:07 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def get(self, request): # pylint: disable=no-self-use
|
2016-08-14 08:10:07 +00:00
|
|
|
"""Return the manifest.json."""
|
|
|
|
msg = json.dumps(MANIFEST_JSON, sort_keys=True).encode('UTF-8')
|
2016-10-24 06:48:01 +00:00
|
|
|
return web.Response(body=msg, content_type="application/manifest+json")
|
2017-07-13 01:08:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ThemesView(HomeAssistantView):
|
|
|
|
"""View to return defined themes."""
|
|
|
|
|
|
|
|
requires_auth = False
|
|
|
|
url = '/api/themes'
|
|
|
|
name = 'api:themes'
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def get(self, request):
|
|
|
|
"""Return themes."""
|
|
|
|
hass = request.app['hass']
|
|
|
|
|
|
|
|
return self.json({
|
|
|
|
'themes': hass.data[DATA_THEMES],
|
|
|
|
'default_theme': hass.data[DATA_DEFAULT_THEME],
|
|
|
|
})
|
2017-10-25 02:36:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _fingerprint(path):
|
|
|
|
"""Fingerprint a file."""
|
|
|
|
with open(path) as fil:
|
|
|
|
return hashlib.md5(fil.read().encode('utf-8')).hexdigest()
|