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
|
|
|
|
2016-05-10 01:09:38 +00:00
|
|
|
from jinja2 import FileSystemLoader, Environment
|
|
|
|
from werkzeug.wrappers import Response
|
|
|
|
|
2015-11-03 08:20:20 +00:00
|
|
|
from . import version, mdi_version
|
2015-01-30 07:56:04 +00:00
|
|
|
import homeassistant.util as util
|
2015-02-14 02:27:13 +00:00
|
|
|
from homeassistant.const import URL_ROOT, HTTP_OK
|
2016-02-14 08:04:08 +00:00
|
|
|
from homeassistant.components import api
|
2016-05-10 01:09:38 +00:00
|
|
|
from homeassistant.components.wsgi 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')
|
|
|
|
|
2016-05-10 01:09:38 +00:00
|
|
|
TEMPLATES = Environment(
|
|
|
|
loader=FileSystemLoader(
|
|
|
|
os.path.join(os.path.dirname(__file__), 'templates/')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
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."""
|
2015-06-24 06:22:32 +00:00
|
|
|
for url in FRONTEND_URLS:
|
|
|
|
hass.http.register_path('GET', url, _handle_get_root, False)
|
|
|
|
|
2015-12-08 07:43:28 +00:00
|
|
|
hass.http.register_path('GET', '/service_worker.js',
|
|
|
|
_handle_get_service_worker, False)
|
2015-01-30 07:56:04 +00:00
|
|
|
|
2016-02-14 08:04:08 +00:00
|
|
|
# Bootstrap API
|
|
|
|
hass.http.register_path(
|
|
|
|
'GET', URL_API_BOOTSTRAP, _handle_get_api_bootstrap)
|
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
# Static files
|
|
|
|
hass.http.register_path(
|
|
|
|
'GET', re.compile(r'/static/(?P<file>[a-zA-Z\._\-0-9/]+)'),
|
|
|
|
_handle_get_static, False)
|
|
|
|
hass.http.register_path(
|
|
|
|
'HEAD', re.compile(r'/static/(?P<file>[a-zA-Z\._\-0-9/]+)'),
|
|
|
|
_handle_get_static, False)
|
2015-10-08 14:10:33 +00:00
|
|
|
hass.http.register_path(
|
|
|
|
'GET', re.compile(r'/local/(?P<file>[a-zA-Z\._\-0-9/]+)'),
|
|
|
|
_handle_get_local, False)
|
2015-01-30 07:56:04 +00:00
|
|
|
|
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):
|
|
|
|
url = URL_API_BOOTSTRAP
|
|
|
|
name = "api:bootstrap"
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
"""Return all data needed to bootstrap Home Assistant."""
|
|
|
|
|
|
|
|
return {
|
|
|
|
'config': self.hass.config.as_dict(),
|
|
|
|
'states': self.hass.states.all(),
|
|
|
|
'events': api.events_json(self.hass),
|
|
|
|
'services': api.services_json(self.hass),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class IndexView(HomeAssistantView):
|
|
|
|
url = URL_ROOT
|
|
|
|
name = "frontend:index"
|
|
|
|
extra_urls = ['/logbook', '/history', '/map', '/devService', '/devState',
|
|
|
|
'/devEvent', '/devInfo', '/devTemplate', '/states/<entity>']
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
app_url = "frontend-{}.html".format(version.VERSION)
|
|
|
|
|
|
|
|
# auto login if no password was set, else check api_password param
|
|
|
|
auth = ('no_password_set' if request.api_password is None
|
|
|
|
else request.values.get('api_password', ''))
|
|
|
|
|
|
|
|
template = TEMPLATES.get_template('index.html')
|
|
|
|
|
|
|
|
resp = template.render(app_url=app_url, auth=auth,
|
|
|
|
icons=mdi_version.VERSION)
|
|
|
|
|
|
|
|
return Response(resp, mimetype="text/html")
|
|
|
|
|
|
|
|
|
2016-02-14 08:04:08 +00:00
|
|
|
def _handle_get_api_bootstrap(handler, path_match, data):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Return all data needed to bootstrap Home Assistant."""
|
2016-02-14 08:04:08 +00:00
|
|
|
hass = handler.server.hass
|
|
|
|
|
|
|
|
handler.write_json({
|
|
|
|
'config': hass.config.as_dict(),
|
|
|
|
'states': hass.states.all(),
|
|
|
|
'events': api.events_json(hass),
|
|
|
|
'services': api.services_json(hass),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
def _handle_get_root(handler, path_match, data):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Render the frontend."""
|
2015-01-30 07:56:04 +00:00
|
|
|
if handler.server.development:
|
2015-07-13 03:43:07 +00:00
|
|
|
app_url = "home-assistant-polymer/src/home-assistant.html"
|
2015-01-30 07:56:04 +00:00
|
|
|
else:
|
2015-01-30 16:26:06 +00:00
|
|
|
app_url = "frontend-{}.html".format(version.VERSION)
|
2015-01-30 07:56:04 +00:00
|
|
|
|
|
|
|
# auto login if no password was set, else check api_password param
|
2015-11-29 02:32:15 +00:00
|
|
|
auth = ('no_password_set' if handler.server.api_password is None
|
2015-01-30 07:56:04 +00:00
|
|
|
else data.get('api_password', ''))
|
|
|
|
|
2015-03-04 05:15:15 +00:00
|
|
|
with open(INDEX_PATH) as template_file:
|
|
|
|
template_html = template_file.read()
|
|
|
|
|
|
|
|
template_html = template_html.replace('{{ app_url }}', app_url)
|
|
|
|
template_html = template_html.replace('{{ auth }}', auth)
|
2015-11-03 08:20:20 +00:00
|
|
|
template_html = template_html.replace('{{ icons }}', mdi_version.VERSION)
|
2015-03-04 05:15:15 +00:00
|
|
|
|
2016-03-27 18:57:15 +00:00
|
|
|
handler.send_response(HTTP_OK)
|
|
|
|
handler.write_content(template_html.encode("UTF-8"),
|
|
|
|
'text/html; charset=utf-8')
|
2015-01-30 07:56:04 +00:00
|
|
|
|
|
|
|
|
2015-12-08 07:43:28 +00:00
|
|
|
def _handle_get_service_worker(handler, path_match, data):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Return service worker for the frontend."""
|
2015-12-08 07:43:28 +00:00
|
|
|
if handler.server.development:
|
|
|
|
sw_path = "home-assistant-polymer/build/service_worker.js"
|
|
|
|
else:
|
|
|
|
sw_path = "service_worker.js"
|
|
|
|
|
|
|
|
handler.write_file(os.path.join(os.path.dirname(__file__), 'www_static',
|
|
|
|
sw_path))
|
|
|
|
|
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
def _handle_get_static(handler, path_match, data):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Return a static file for the frontend."""
|
2015-01-30 07:56:04 +00:00
|
|
|
req_file = util.sanitize_path(path_match.group('file'))
|
|
|
|
|
2015-11-02 08:03:44 +00:00
|
|
|
# Strip md5 hash out
|
|
|
|
fingerprinted = _FINGERPRINT.match(req_file)
|
|
|
|
if fingerprinted:
|
|
|
|
req_file = "{}.{}".format(*fingerprinted.groups())
|
2015-01-30 07:56:04 +00:00
|
|
|
|
|
|
|
path = os.path.join(os.path.dirname(__file__), 'www_static', req_file)
|
|
|
|
|
2015-01-30 16:26:06 +00:00
|
|
|
handler.write_file(path)
|
2015-10-08 14:10:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _handle_get_local(handler, path_match, data):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Return a static file from the hass.config.path/www for the frontend."""
|
2015-10-08 14:10:33 +00:00
|
|
|
req_file = util.sanitize_path(path_match.group('file'))
|
|
|
|
|
2015-12-13 21:40:42 +00:00
|
|
|
path = handler.server.hass.config.path('www', req_file)
|
2015-10-08 14:10:33 +00:00
|
|
|
|
|
|
|
handler.write_file(path)
|