2016-03-09 10:15:04 +00:00
|
|
|
"""Handle the frontend for Home Assistant."""
|
2015-01-30 07:56:04 +00:00
|
|
|
import re
|
|
|
|
import os
|
2015-01-30 16:26:06 +00:00
|
|
|
import logging
|
2015-01-30 07:56:04 +00:00
|
|
|
|
2015-11-03 08:20:20 +00:00
|
|
|
from . import version, mdi_version
|
2016-05-14 07:58:36 +00:00
|
|
|
from homeassistant.const import URL_ROOT
|
2016-02-14 08:04:08 +00:00
|
|
|
from homeassistant.components import api
|
2016-05-14 07:58:36 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView
|
2015-01-30 07:56:04 +00:00
|
|
|
|
|
|
|
DOMAIN = 'frontend'
|
|
|
|
DEPENDENCIES = ['api']
|
|
|
|
|
2015-03-04 05:15:15 +00:00
|
|
|
INDEX_PATH = os.path.join(os.path.dirname(__file__), 'index.html.template')
|
|
|
|
|
2015-01-30 16:26:06 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2015-06-24 06:22:32 +00:00
|
|
|
FRONTEND_URLS = [
|
2015-09-21 06:14:58 +00:00
|
|
|
URL_ROOT, '/logbook', '/history', '/map', '/devService', '/devState',
|
2016-01-26 06:47:13 +00:00
|
|
|
'/devEvent', '/devInfo', '/devTemplate',
|
|
|
|
re.compile(r'/states(/([a-zA-Z\._\-0-9/]+)|)'),
|
|
|
|
]
|
2015-06-24 06:22:32 +00:00
|
|
|
|
2016-02-14 08:04:08 +00:00
|
|
|
URL_API_BOOTSTRAP = "/api/bootstrap"
|
|
|
|
|
2015-11-02 08:03:44 +00:00
|
|
|
_FINGERPRINT = re.compile(r'^(\w+)-[a-z0-9]{32}\.(\w+)$', re.IGNORECASE)
|
|
|
|
|
2015-06-24 06:22:32 +00:00
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
def setup(hass, config):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Setup serving the frontend."""
|
2016-05-10 01:09:38 +00:00
|
|
|
hass.wsgi.register_view(IndexView)
|
|
|
|
hass.wsgi.register_view(BootstrapView)
|
|
|
|
|
|
|
|
www_static_path = os.path.join(os.path.dirname(__file__), 'www_static')
|
|
|
|
if hass.wsgi.development:
|
|
|
|
sw_path = "home-assistant-polymer/build/service_worker.js"
|
|
|
|
else:
|
|
|
|
sw_path = "service_worker.js"
|
|
|
|
|
|
|
|
hass.wsgi.register_static_path(
|
|
|
|
"/service_worker.js",
|
|
|
|
os.path.join(www_static_path, sw_path)
|
|
|
|
)
|
|
|
|
hass.wsgi.register_static_path("/static", www_static_path)
|
|
|
|
hass.wsgi.register_static_path("/local", hass.config.path('www'))
|
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2016-05-10 01:09:38 +00:00
|
|
|
class BootstrapView(HomeAssistantView):
|
2016-05-14 07:58:36 +00:00
|
|
|
"""View to bootstrap frontend with all needed data."""
|
|
|
|
|
2016-05-10 01:09:38 +00:00
|
|
|
url = URL_API_BOOTSTRAP
|
|
|
|
name = "api:bootstrap"
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
"""Return all data needed to bootstrap Home Assistant."""
|
2016-05-14 07:58:36 +00:00
|
|
|
return self.json({
|
2016-05-10 01:09:38 +00:00
|
|
|
'config': self.hass.config.as_dict(),
|
|
|
|
'states': self.hass.states.all(),
|
|
|
|
'events': api.events_json(self.hass),
|
|
|
|
'services': api.services_json(self.hass),
|
2016-05-14 07:58:36 +00:00
|
|
|
})
|
2016-05-10 01:09:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IndexView(HomeAssistantView):
|
2016-05-14 07:58:36 +00:00
|
|
|
"""Serve the frontend."""
|
|
|
|
|
2016-05-10 01:09:38 +00:00
|
|
|
url = URL_ROOT
|
|
|
|
name = "frontend:index"
|
2016-05-14 07:58:36 +00:00
|
|
|
requires_auth = False
|
2016-05-10 01:09:38 +00:00
|
|
|
extra_urls = ['/logbook', '/history', '/map', '/devService', '/devState',
|
2016-05-14 22:07:20 +00:00
|
|
|
'/devEvent', '/devInfo', '/devTemplate',
|
2016-05-15 23:38:19 +00:00
|
|
|
'/states', '/states/<entity:entity_id>']
|
2016-05-10 01:09:38 +00:00
|
|
|
|
2016-05-12 05:55:24 +00:00
|
|
|
def __init__(self, hass):
|
2016-05-14 07:58:36 +00:00
|
|
|
"""Initialize the frontend view."""
|
2016-05-12 05:55:24 +00:00
|
|
|
super().__init__(hass)
|
|
|
|
|
|
|
|
from jinja2 import FileSystemLoader, Environment
|
|
|
|
|
2016-05-14 07:58:36 +00:00
|
|
|
self.templates = Environment(
|
2016-05-12 05:55:24 +00:00
|
|
|
loader=FileSystemLoader(
|
|
|
|
os.path.join(os.path.dirname(__file__), 'templates/')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2016-05-14 22:07:20 +00:00
|
|
|
def get(self, request, entity_id=None):
|
2016-05-12 05:55:24 +00:00
|
|
|
"""Serve the index view."""
|
2016-05-14 22:07:20 +00:00
|
|
|
if self.hass.wsgi.development:
|
|
|
|
app_url = 'home-assistant-polymer/src/home-assistant.html'
|
|
|
|
else:
|
|
|
|
app_url = "frontend-{}.html".format(version.VERSION)
|
2016-05-10 01:09:38 +00:00
|
|
|
|
|
|
|
# auto login if no password was set, else check api_password param
|
2016-05-14 22:07:20 +00:00
|
|
|
if self.hass.config.api.api_password is None:
|
|
|
|
auth = 'no_password_set'
|
|
|
|
else:
|
2016-05-15 09:23:21 +00:00
|
|
|
auth = request.values.get('api_password', '')
|
2016-05-10 01:09:38 +00:00
|
|
|
|
2016-05-14 07:58:36 +00:00
|
|
|
template = self.templates.get_template('index.html')
|
2016-05-10 01:09:38 +00:00
|
|
|
|
|
|
|
resp = template.render(app_url=app_url, auth=auth,
|
|
|
|
icons=mdi_version.VERSION)
|
|
|
|
|
2016-05-12 05:55:24 +00:00
|
|
|
return self.Response(resp, mimetype="text/html")
|