core/homeassistant/components/frontend/__init__.py

102 lines
3.0 KiB
Python
Raw Normal View History

2016-03-09 10:15:04 +00:00
"""Handle the frontend for Home Assistant."""
import os
from homeassistant.components import api
2016-05-14 07:58:36 +00:00
from homeassistant.components.http import HomeAssistantView
2016-07-08 01:54:16 +00:00
from . import version, mdi_version
DOMAIN = 'frontend'
DEPENDENCIES = ['api']
def setup(hass, config):
2016-03-09 10:15:04 +00:00
"""Setup serving the frontend."""
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",
2016-05-28 04:45:38 +00:00
os.path.join(www_static_path, sw_path),
0
)
2016-06-05 22:48:59 +00:00
hass.wsgi.register_static_path(
"/robots.txt",
os.path.join(www_static_path, "robots.txt")
)
hass.wsgi.register_static_path("/static", www_static_path)
hass.wsgi.register_static_path("/local", hass.config.path('www'))
return True
class BootstrapView(HomeAssistantView):
2016-05-14 07:58:36 +00:00
"""View to bootstrap frontend with all needed data."""
2016-05-28 04:45:38 +00:00
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({
'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
})
class IndexView(HomeAssistantView):
2016-05-14 07:58:36 +00:00
"""Serve the frontend."""
2016-05-28 04:45:38 +00:00
url = '/'
name = "frontend:index"
2016-05-14 07:58:36 +00:00
requires_auth = False
extra_urls = ['/logbook', '/history', '/map', '/devService', '/devState',
2016-05-14 22:07:20 +00:00
'/devEvent', '/devInfo', '/devTemplate',
'/states', '/states/<entity:entity_id>']
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:
2016-05-29 01:38:46 +00:00
core_url = 'home-assistant-polymer/build/_core_compiled.js'
ui_url = 'home-assistant-polymer/src/home-assistant.html'
2016-05-14 22:07:20 +00:00
else:
2016-05-29 01:38:46 +00:00
core_url = 'core-{}.js'.format(version.CORE)
ui_url = 'frontend-{}.html'.format(version.UI)
2016-05-28 04:45:38 +00:00
# auto login if no password was set
2016-05-14 22:07:20 +00:00
if self.hass.config.api.api_password is None:
2016-05-29 01:38:46 +00:00
auth = 'true'
2016-05-14 22:07:20 +00:00
else:
2016-05-29 01:38:46 +00:00
auth = 'false'
icons_url = 'mdi-{}.html'.format(mdi_version.VERSION)
2016-05-14 07:58:36 +00:00
template = self.templates.get_template('index.html')
2016-05-16 07:25:47 +00:00
# pylint is wrong
# pylint: disable=no-member
2016-05-29 01:38:46 +00:00
resp = template.render(
core_url=core_url, ui_url=ui_url, auth=auth,
icons_url=icons_url, icons=mdi_version.VERSION)
2016-05-29 01:38:46 +00:00
return self.Response(resp, mimetype='text/html')