2019-05-10 16:34:28 +00:00
|
|
|
"""Support for a Genius Hub system."""
|
|
|
|
from datetime import timedelta
|
2019-04-16 21:54:46 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-05-10 16:34:28 +00:00
|
|
|
from geniushubclient import GeniusHubClient
|
|
|
|
|
2019-04-16 21:54:46 +00:00
|
|
|
from homeassistant.const import (
|
2019-04-18 12:37:52 +00:00
|
|
|
CONF_HOST, CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME)
|
2019-04-16 21:54:46 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from homeassistant.helpers.discovery import async_load_platform
|
2019-05-10 16:34:28 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
from homeassistant.helpers.event import async_track_time_interval
|
2019-04-16 21:54:46 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DOMAIN = 'geniushub'
|
|
|
|
|
2019-05-10 16:34:28 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=60)
|
|
|
|
|
2019-04-18 12:37:52 +00:00
|
|
|
_V1_API_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_TOKEN): cv.string,
|
|
|
|
})
|
|
|
|
_V3_API_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
})
|
2019-04-16 21:54:46 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
2019-04-18 12:37:52 +00:00
|
|
|
DOMAIN: vol.Any(
|
|
|
|
_V3_API_SCHEMA,
|
|
|
|
_V1_API_SCHEMA,
|
|
|
|
)
|
2019-04-16 21:54:46 +00:00
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass, hass_config):
|
|
|
|
"""Create a Genius Hub system."""
|
2019-04-18 12:37:52 +00:00
|
|
|
kwargs = dict(hass_config[DOMAIN])
|
|
|
|
if CONF_HOST in kwargs:
|
|
|
|
args = (kwargs.pop(CONF_HOST), )
|
|
|
|
else:
|
|
|
|
args = (kwargs.pop(CONF_TOKEN), )
|
|
|
|
|
2019-05-10 16:34:28 +00:00
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
data = hass.data[DOMAIN]['data'] = GeniusData(hass, args, kwargs)
|
2019-04-16 21:54:46 +00:00
|
|
|
try:
|
2019-05-10 16:34:28 +00:00
|
|
|
await data._client.hub.update() # pylint: disable=protected-access
|
2019-04-16 21:54:46 +00:00
|
|
|
except AssertionError: # assert response.status == HTTP_OK
|
|
|
|
_LOGGER.warning(
|
2019-05-10 16:34:28 +00:00
|
|
|
"Setup failed, check your configuration.",
|
2019-04-16 21:54:46 +00:00
|
|
|
exc_info=True)
|
|
|
|
return False
|
|
|
|
|
2019-05-10 16:34:28 +00:00
|
|
|
async_track_time_interval(hass, data.async_update, SCAN_INTERVAL)
|
2019-04-16 21:54:46 +00:00
|
|
|
|
2019-05-10 16:34:28 +00:00
|
|
|
for platform in ['climate', 'water_heater']:
|
|
|
|
hass.async_create_task(async_load_platform(
|
|
|
|
hass, platform, DOMAIN, {}, hass_config))
|
2019-05-07 15:37:47 +00:00
|
|
|
|
2019-04-16 21:54:46 +00:00
|
|
|
return True
|
2019-05-10 16:34:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GeniusData:
|
|
|
|
"""Container for geniushub client and data."""
|
|
|
|
|
|
|
|
def __init__(self, hass, args, kwargs):
|
|
|
|
"""Initialize the geniushub client."""
|
|
|
|
self._hass = hass
|
|
|
|
self._client = hass.data[DOMAIN]['client'] = GeniusHubClient(
|
|
|
|
*args, **kwargs, session=async_get_clientsession(hass))
|
|
|
|
|
|
|
|
async def async_update(self, now, **kwargs):
|
|
|
|
"""Update the geniushub client's data."""
|
|
|
|
try:
|
|
|
|
await self._client.hub.update()
|
|
|
|
except AssertionError: # assert response.status == HTTP_OK
|
|
|
|
_LOGGER.warning("Update failed.", exc_info=True)
|
|
|
|
return
|
|
|
|
async_dispatcher_send(self._hass, DOMAIN)
|