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
|
|
|
|
import logging
|
|
|
|
import os
|
2017-04-27 05:36:48 +00:00
|
|
|
import re
|
2017-04-07 05:19:08 +00:00
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
from aiohttp import web
|
2017-05-02 07:27:50 +00:00
|
|
|
from aiohttp.web_exceptions import HTTPBadGateway
|
2017-04-27 05:36:48 +00:00
|
|
|
from aiohttp.hdrs import CONTENT_TYPE
|
2017-04-07 05:19:08 +00:00
|
|
|
import async_timeout
|
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
from homeassistant.const import CONTENT_TYPE_TEXT_PLAIN
|
2017-05-22 18:00:02 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView, KEY_AUTHENTICATED
|
2017-04-07 05:19:08 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2017-04-27 07:57:40 +00:00
|
|
|
from homeassistant.components.frontend import register_built_in_panel
|
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']
|
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
TIMEOUT = 10
|
|
|
|
|
|
|
|
ADDON_REST_COMMANDS = {
|
|
|
|
'install': ['POST'],
|
|
|
|
'uninstall': ['POST'],
|
|
|
|
'start': ['POST'],
|
|
|
|
'stop': ['POST'],
|
|
|
|
'update': ['POST'],
|
|
|
|
'options': ['POST'],
|
|
|
|
'info': ['GET'],
|
|
|
|
'logs': ['GET'],
|
2017-04-07 05:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the HASSio component."""
|
2017-04-07 05:19:08 +00:00
|
|
|
try:
|
|
|
|
host = os.environ['HASSIO']
|
|
|
|
except KeyError:
|
|
|
|
_LOGGER.error("No HassIO supervisor detect!")
|
|
|
|
return False
|
|
|
|
|
|
|
|
websession = async_get_clientsession(hass)
|
|
|
|
hassio = HassIO(hass.loop, websession, host)
|
|
|
|
|
|
|
|
api_ok = yield from hassio.is_connected()
|
|
|
|
if not api_ok:
|
|
|
|
_LOGGER.error("Not connected with HassIO!")
|
|
|
|
return False
|
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
hass.http.register_view(HassIOView(hassio))
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2017-04-27 07:57:40 +00:00
|
|
|
if 'frontend' in hass.config.components:
|
|
|
|
register_built_in_panel(hass, 'hassio', 'Hass.io',
|
|
|
|
'mdi:access-point-network')
|
|
|
|
|
2017-04-07 05:19:08 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class HassIO(object):
|
|
|
|
"""Small API wrapper for HassIO."""
|
|
|
|
|
|
|
|
def __init__(self, loop, websession, ip):
|
|
|
|
"""Initialze HassIO api."""
|
|
|
|
self.loop = loop
|
|
|
|
self.websession = websession
|
|
|
|
self._ip = ip
|
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
@asyncio.coroutine
|
2017-04-07 05:19:08 +00:00
|
|
|
def is_connected(self):
|
|
|
|
"""Return True if it connected to HassIO supervisor.
|
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
This method is a coroutine.
|
2017-04-07 05:19:08 +00:00
|
|
|
"""
|
|
|
|
try:
|
2017-04-27 05:36:48 +00:00
|
|
|
with async_timeout.timeout(TIMEOUT, loop=self.loop):
|
2017-04-07 05:19:08 +00:00
|
|
|
request = yield from self.websession.get(
|
2017-04-27 05:36:48 +00:00
|
|
|
"http://{}{}".format(self._ip, "/supervisor/ping")
|
2017-04-07 05:19:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if request.status != 200:
|
2017-04-27 05:36:48 +00:00
|
|
|
_LOGGER.error("Ping return code %d.", request.status)
|
|
|
|
return False
|
2017-04-24 13:16:28 +00:00
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
answer = yield from request.json()
|
|
|
|
return answer and answer['result'] == 'ok'
|
2017-04-07 05:19:08 +00:00
|
|
|
|
|
|
|
except asyncio.TimeoutError:
|
2017-04-27 05:36:48 +00:00
|
|
|
_LOGGER.error("Timeout on ping request")
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
except aiohttp.ClientError as err:
|
|
|
|
_LOGGER.error("Client error on ping request %s", err)
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
return False
|
2017-04-07 05:19:08 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-04-27 05:36:48 +00:00
|
|
|
def command_proxy(self, path, request):
|
|
|
|
"""Return a client request with proxy origin for HassIO supervisor.
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
data = None
|
|
|
|
headers = None
|
|
|
|
with async_timeout.timeout(TIMEOUT, loop=self.loop):
|
|
|
|
data = yield from request.read()
|
|
|
|
if data:
|
|
|
|
headers = {CONTENT_TYPE: request.content_type}
|
|
|
|
else:
|
|
|
|
data = None
|
|
|
|
|
|
|
|
method = getattr(self.websession, request.method.lower())
|
|
|
|
client = yield from method(
|
|
|
|
"http://{}/{}".format(self._ip, path), data=data,
|
|
|
|
headers=headers
|
|
|
|
)
|
|
|
|
|
|
|
|
return client
|
|
|
|
|
|
|
|
except aiohttp.ClientError as err:
|
|
|
|
_LOGGER.error("Client error on api %s request %s.", path, err)
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
_LOGGER.error("Client timeout error on api request %s.", path)
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
raise HTTPBadGateway()
|
2017-04-07 05:19:08 +00:00
|
|
|
|
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
class HassIOView(HomeAssistantView):
|
|
|
|
"""HassIO view to handle base part."""
|
2017-04-24 13:16:28 +00:00
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
name = "api:hassio"
|
|
|
|
url = "/api/hassio/{path:.+}"
|
2017-05-22 18:00:02 +00:00
|
|
|
requires_auth = False
|
2017-04-24 13:16:28 +00:00
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
def __init__(self, hassio):
|
2017-04-24 13:16:28 +00:00
|
|
|
"""Initialize a hassio base view."""
|
|
|
|
self.hassio = hassio
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-04-27 05:36:48 +00:00
|
|
|
def _handle(self, request, path):
|
|
|
|
"""Route data to hassio."""
|
2017-05-22 18:00:02 +00:00
|
|
|
if path != 'panel' and not request[KEY_AUTHENTICATED]:
|
|
|
|
return web.Response(status=401)
|
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
client = yield from self.hassio.command_proxy(path, request)
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
data = yield from client.read()
|
|
|
|
if path.endswith('/logs'):
|
|
|
|
return _create_response_log(client, data)
|
|
|
|
return _create_response(client, data)
|
2017-04-07 05:19:08 +00:00
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
get = _handle
|
|
|
|
post = _handle
|
2017-04-24 13:16:28 +00:00
|
|
|
|
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
def _create_response(client, data):
|
|
|
|
"""Convert a response from client request."""
|
|
|
|
return web.Response(
|
|
|
|
body=data,
|
|
|
|
status=client.status,
|
|
|
|
content_type=client.content_type,
|
|
|
|
)
|
2017-04-24 13:16:28 +00:00
|
|
|
|
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
def _create_response_log(client, data):
|
|
|
|
"""Convert a response from client request."""
|
|
|
|
# Remove color codes
|
|
|
|
log = re.sub(r"\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))", "", data.decode())
|
2017-04-24 13:16:28 +00:00
|
|
|
|
2017-04-27 05:36:48 +00:00
|
|
|
return web.Response(
|
|
|
|
text=log,
|
|
|
|
status=client.status,
|
|
|
|
content_type=CONTENT_TYPE_TEXT_PLAIN,
|
|
|
|
)
|