2017-04-07 05:19:08 +00:00
|
|
|
"""
|
2017-04-30 05:04:49 +00:00
|
|
|
Exposes regular REST commands as services.
|
2017-04-07 05:19:08 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/hassio/
|
|
|
|
"""
|
|
|
|
import asyncio
|
2018-01-10 18:48:31 +00:00
|
|
|
from datetime import timedelta
|
2017-04-07 05:19:08 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2017-10-04 05:52:45 +00:00
|
|
|
import voluptuous as vol
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2018-01-12 14:29:58 +00:00
|
|
|
from homeassistant.components import SERVICE_CHECK_CONFIG
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.const import (
|
2018-05-14 11:05:52 +00:00
|
|
|
ATTR_NAME, SERVICE_HOMEASSISTANT_RESTART, SERVICE_HOMEASSISTANT_STOP)
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.core import DOMAIN as HASS_DOMAIN
|
|
|
|
from homeassistant.core import callback
|
2018-01-12 14:29:58 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
2018-01-10 18:48:31 +00:00
|
|
|
from homeassistant.util.dt import utcnow
|
2018-05-14 11:05:52 +00:00
|
|
|
|
2018-02-20 23:24:31 +00:00
|
|
|
from .handler import HassIO
|
|
|
|
from .http import HassIOView
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-04-07 05:19:08 +00:00
|
|
|
DOMAIN = 'hassio'
|
|
|
|
DEPENDENCIES = ['http']
|
|
|
|
|
2018-05-29 06:51:08 +00:00
|
|
|
CONF_FRONTEND_REPO = 'development_repo'
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
vol.Optional(DOMAIN): vol.Schema({
|
|
|
|
vol.Optional(CONF_FRONTEND_REPO): cv.isdir,
|
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
|
2018-01-10 18:48:31 +00:00
|
|
|
DATA_HOMEASSISTANT_VERSION = 'hassio_hass_version'
|
2018-01-12 14:29:58 +00:00
|
|
|
HASSIO_UPDATE_INTERVAL = timedelta(minutes=55)
|
2018-01-10 18:48:31 +00:00
|
|
|
|
2017-10-04 05:52:45 +00:00
|
|
|
SERVICE_ADDON_START = 'addon_start'
|
|
|
|
SERVICE_ADDON_STOP = 'addon_stop'
|
|
|
|
SERVICE_ADDON_RESTART = 'addon_restart'
|
|
|
|
SERVICE_ADDON_STDIN = 'addon_stdin'
|
2017-10-13 13:45:22 +00:00
|
|
|
SERVICE_HOST_SHUTDOWN = 'host_shutdown'
|
|
|
|
SERVICE_HOST_REBOOT = 'host_reboot'
|
2018-01-10 18:48:31 +00:00
|
|
|
SERVICE_SNAPSHOT_FULL = 'snapshot_full'
|
|
|
|
SERVICE_SNAPSHOT_PARTIAL = 'snapshot_partial'
|
|
|
|
SERVICE_RESTORE_FULL = 'restore_full'
|
|
|
|
SERVICE_RESTORE_PARTIAL = 'restore_partial'
|
2017-10-04 05:52:45 +00:00
|
|
|
|
|
|
|
ATTR_ADDON = 'addon'
|
|
|
|
ATTR_INPUT = 'input'
|
2018-01-10 18:48:31 +00:00
|
|
|
ATTR_SNAPSHOT = 'snapshot'
|
|
|
|
ATTR_ADDONS = 'addons'
|
|
|
|
ATTR_FOLDERS = 'folders'
|
|
|
|
ATTR_HOMEASSISTANT = 'homeassistant'
|
2018-02-19 22:49:52 +00:00
|
|
|
ATTR_PASSWORD = 'password'
|
2017-10-04 05:52:45 +00:00
|
|
|
|
2018-01-10 18:48:31 +00:00
|
|
|
SCHEMA_NO_DATA = vol.Schema({})
|
|
|
|
|
2017-10-04 05:52:45 +00:00
|
|
|
SCHEMA_ADDON = vol.Schema({
|
|
|
|
vol.Required(ATTR_ADDON): cv.slug,
|
|
|
|
})
|
|
|
|
|
|
|
|
SCHEMA_ADDON_STDIN = SCHEMA_ADDON.extend({
|
|
|
|
vol.Required(ATTR_INPUT): vol.Any(dict, cv.string)
|
|
|
|
})
|
|
|
|
|
2018-01-10 18:48:31 +00:00
|
|
|
SCHEMA_SNAPSHOT_FULL = vol.Schema({
|
|
|
|
vol.Optional(ATTR_NAME): cv.string,
|
2018-02-19 22:49:52 +00:00
|
|
|
vol.Optional(ATTR_PASSWORD): cv.string,
|
2018-01-10 18:48:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
SCHEMA_SNAPSHOT_PARTIAL = SCHEMA_SNAPSHOT_FULL.extend({
|
|
|
|
vol.Optional(ATTR_FOLDERS): vol.All(cv.ensure_list, [cv.string]),
|
|
|
|
vol.Optional(ATTR_ADDONS): vol.All(cv.ensure_list, [cv.string]),
|
|
|
|
})
|
|
|
|
|
|
|
|
SCHEMA_RESTORE_FULL = vol.Schema({
|
|
|
|
vol.Required(ATTR_SNAPSHOT): cv.slug,
|
2018-02-19 22:49:52 +00:00
|
|
|
vol.Optional(ATTR_PASSWORD): cv.string,
|
2018-01-10 18:48:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
SCHEMA_RESTORE_PARTIAL = SCHEMA_RESTORE_FULL.extend({
|
|
|
|
vol.Optional(ATTR_HOMEASSISTANT): cv.boolean,
|
|
|
|
vol.Optional(ATTR_FOLDERS): vol.All(cv.ensure_list, [cv.string]),
|
|
|
|
vol.Optional(ATTR_ADDONS): vol.All(cv.ensure_list, [cv.string]),
|
|
|
|
})
|
|
|
|
|
2017-10-04 05:52:45 +00:00
|
|
|
MAP_SERVICE_API = {
|
2018-01-10 18:48:31 +00:00
|
|
|
SERVICE_ADDON_START: ('/addons/{addon}/start', SCHEMA_ADDON, 60, False),
|
|
|
|
SERVICE_ADDON_STOP: ('/addons/{addon}/stop', SCHEMA_ADDON, 60, False),
|
|
|
|
SERVICE_ADDON_RESTART:
|
|
|
|
('/addons/{addon}/restart', SCHEMA_ADDON, 60, False),
|
|
|
|
SERVICE_ADDON_STDIN:
|
|
|
|
('/addons/{addon}/stdin', SCHEMA_ADDON_STDIN, 60, False),
|
|
|
|
SERVICE_HOST_SHUTDOWN: ('/host/shutdown', SCHEMA_NO_DATA, 60, False),
|
|
|
|
SERVICE_HOST_REBOOT: ('/host/reboot', SCHEMA_NO_DATA, 60, False),
|
|
|
|
SERVICE_SNAPSHOT_FULL:
|
|
|
|
('/snapshots/new/full', SCHEMA_SNAPSHOT_FULL, 300, True),
|
|
|
|
SERVICE_SNAPSHOT_PARTIAL:
|
|
|
|
('/snapshots/new/partial', SCHEMA_SNAPSHOT_PARTIAL, 300, True),
|
|
|
|
SERVICE_RESTORE_FULL:
|
|
|
|
('/snapshots/{snapshot}/restore/full', SCHEMA_RESTORE_FULL, 300, True),
|
|
|
|
SERVICE_RESTORE_PARTIAL:
|
|
|
|
('/snapshots/{snapshot}/restore/partial', SCHEMA_RESTORE_PARTIAL, 300,
|
|
|
|
True),
|
2017-10-04 05:52:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2018-01-12 14:29:58 +00:00
|
|
|
@callback
|
2018-01-10 18:48:31 +00:00
|
|
|
@bind_hass
|
|
|
|
def get_homeassistant_version(hass):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Return latest available Home Assistant version.
|
2018-01-12 14:29:58 +00:00
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2018-01-10 18:48:31 +00:00
|
|
|
return hass.data.get(DATA_HOMEASSISTANT_VERSION)
|
|
|
|
|
|
|
|
|
2018-01-12 14:29:58 +00:00
|
|
|
@callback
|
|
|
|
@bind_hass
|
|
|
|
def is_hassio(hass):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Return true if hass.io is loaded.
|
2018-01-12 14:29:58 +00:00
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
|
|
|
return DOMAIN in hass.config.components
|
|
|
|
|
|
|
|
|
|
|
|
@bind_hass
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_check_config(hass):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Check configuration over Hass.io API."""
|
2018-02-21 21:42:55 +00:00
|
|
|
hassio = hass.data[DOMAIN]
|
|
|
|
result = yield from hassio.check_homeassistant_config()
|
2018-01-12 14:29:58 +00:00
|
|
|
|
|
|
|
if not result:
|
|
|
|
return "Hass.io config check API error"
|
2018-07-23 08:16:05 +00:00
|
|
|
if result['result'] == "error":
|
2018-01-12 14:29:58 +00:00
|
|
|
return result['message']
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2017-04-07 05:19:08 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup(hass, config):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up the Hass.io component."""
|
2017-04-07 05:19:08 +00:00
|
|
|
try:
|
|
|
|
host = os.environ['HASSIO']
|
|
|
|
except KeyError:
|
2018-05-29 06:51:08 +00:00
|
|
|
_LOGGER.error("Missing HASSIO environment variable.")
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.environ['HASSIO_TOKEN']
|
|
|
|
except KeyError:
|
|
|
|
_LOGGER.error("Missing HASSIO_TOKEN environment variable.")
|
2017-04-07 05:19:08 +00:00
|
|
|
return False
|
|
|
|
|
2018-01-10 18:48:31 +00:00
|
|
|
websession = hass.helpers.aiohttp_client.async_get_clientsession()
|
2018-01-12 14:29:58 +00:00
|
|
|
hass.data[DOMAIN] = hassio = HassIO(hass.loop, websession, host)
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2017-10-04 05:52:45 +00:00
|
|
|
if not (yield from hassio.is_connected()):
|
2018-01-21 06:35:38 +00:00
|
|
|
_LOGGER.error("Not connected with Hass.io")
|
2017-04-07 05:19:08 +00:00
|
|
|
return False
|
|
|
|
|
2018-05-29 06:51:08 +00:00
|
|
|
# This overrides the normal API call that would be forwarded
|
|
|
|
development_repo = config.get(DOMAIN, {}).get(CONF_FRONTEND_REPO)
|
|
|
|
if development_repo is not None:
|
|
|
|
hass.http.register_static_path(
|
2018-06-08 20:50:19 +00:00
|
|
|
'/api/hassio/app',
|
|
|
|
os.path.join(development_repo, 'hassio/build'), False)
|
2018-05-29 06:51:08 +00:00
|
|
|
|
2018-02-20 23:24:31 +00:00
|
|
|
hass.http.register_view(HassIOView(host, websession))
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2017-04-27 07:57:40 +00:00
|
|
|
if 'frontend' in hass.config.components:
|
2018-06-08 20:50:19 +00:00
|
|
|
yield from hass.components.panel_custom.async_register_panel(
|
|
|
|
frontend_url_path='hassio',
|
|
|
|
webcomponent_name='hassio-main',
|
|
|
|
sidebar_title='Hass.io',
|
|
|
|
sidebar_icon='hass:home-assistant',
|
|
|
|
js_url='/api/hassio/app/entrypoint.js',
|
|
|
|
embed_iframe=True,
|
|
|
|
)
|
2017-04-27 07:57:40 +00:00
|
|
|
|
2017-10-04 05:52:45 +00:00
|
|
|
if 'http' in config:
|
2017-10-13 13:45:22 +00:00
|
|
|
yield from hassio.update_hass_api(config['http'])
|
|
|
|
|
|
|
|
if 'homeassistant' in config:
|
|
|
|
yield from hassio.update_hass_timezone(config['homeassistant'])
|
2017-10-04 05:52:45 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_service_handler(service):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Handle service calls for Hass.io."""
|
2017-10-04 05:52:45 +00:00
|
|
|
api_command = MAP_SERVICE_API[service.service][0]
|
2018-01-10 18:48:31 +00:00
|
|
|
data = service.data.copy()
|
|
|
|
addon = data.pop(ATTR_ADDON, None)
|
|
|
|
snapshot = data.pop(ATTR_SNAPSHOT, None)
|
|
|
|
payload = None
|
|
|
|
|
|
|
|
# Pass data to hass.io API
|
|
|
|
if service.service == SERVICE_ADDON_STDIN:
|
|
|
|
payload = data[ATTR_INPUT]
|
|
|
|
elif MAP_SERVICE_API[service.service][3]:
|
|
|
|
payload = data
|
|
|
|
|
|
|
|
# Call API
|
2018-01-12 14:29:58 +00:00
|
|
|
ret = yield from hassio.send_command(
|
2018-01-10 18:48:31 +00:00
|
|
|
api_command.format(addon=addon, snapshot=snapshot),
|
|
|
|
payload=payload, timeout=MAP_SERVICE_API[service.service][2]
|
|
|
|
)
|
2017-10-04 05:52:45 +00:00
|
|
|
|
2018-01-12 14:29:58 +00:00
|
|
|
if not ret or ret['result'] != "ok":
|
|
|
|
_LOGGER.error("Error on Hass.io API: %s", ret['message'])
|
|
|
|
|
2017-10-04 05:52:45 +00:00
|
|
|
for service, settings in MAP_SERVICE_API.items():
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, service, async_service_handler, schema=settings[1])
|
|
|
|
|
2018-01-10 18:48:31 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def update_homeassistant_version(now):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Update last available Home Assistant version."""
|
2018-01-10 18:48:31 +00:00
|
|
|
data = yield from hassio.get_homeassistant_info()
|
|
|
|
if data:
|
2018-02-21 21:42:55 +00:00
|
|
|
hass.data[DATA_HOMEASSISTANT_VERSION] = data['last_version']
|
2018-01-10 18:48:31 +00:00
|
|
|
|
|
|
|
hass.helpers.event.async_track_point_in_utc_time(
|
|
|
|
update_homeassistant_version, utcnow() + HASSIO_UPDATE_INTERVAL)
|
|
|
|
|
|
|
|
# Fetch last version
|
|
|
|
yield from update_homeassistant_version(None)
|
|
|
|
|
2018-01-12 14:29:58 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_handle_core_service(call):
|
|
|
|
"""Service handler for handling core services."""
|
|
|
|
if call.service == SERVICE_HOMEASSISTANT_STOP:
|
2018-02-21 21:42:55 +00:00
|
|
|
yield from hassio.stop_homeassistant()
|
2018-01-12 14:29:58 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
error = yield from async_check_config(hass)
|
|
|
|
if error:
|
|
|
|
_LOGGER.error(error)
|
|
|
|
hass.components.persistent_notification.async_create(
|
|
|
|
"Config error. See dev-info panel for details.",
|
|
|
|
"Config validating", "{0}.check_config".format(HASS_DOMAIN))
|
|
|
|
return
|
|
|
|
|
|
|
|
if call.service == SERVICE_HOMEASSISTANT_RESTART:
|
2018-02-21 21:42:55 +00:00
|
|
|
yield from hassio.restart_homeassistant()
|
2018-01-12 14:29:58 +00:00
|
|
|
|
|
|
|
# Mock core services
|
|
|
|
for service in (SERVICE_HOMEASSISTANT_STOP, SERVICE_HOMEASSISTANT_RESTART,
|
|
|
|
SERVICE_CHECK_CONFIG):
|
|
|
|
hass.services.async_register(
|
|
|
|
HASS_DOMAIN, service, async_handle_core_service)
|
|
|
|
|
2017-04-07 05:19:08 +00:00
|
|
|
return True
|