2020-08-12 09:03:22 -06:00
|
|
|
"""The ge_kitchen integration."""
|
|
|
|
|
2020-08-13 15:10:55 -06:00
|
|
|
import asyncio
|
|
|
|
import async_timeout
|
|
|
|
import logging
|
2020-08-12 09:03:22 -06:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-08-25 11:09:28 -06:00
|
|
|
from gekitchen import GeAuthError, GeServerError
|
|
|
|
|
2020-08-13 15:10:55 -06:00
|
|
|
from homeassistant.config_entries import ConfigEntry, SOURCE_IMPORT
|
2020-08-25 11:09:28 -06:00
|
|
|
from homeassistant.const import CONF_USERNAME
|
2020-08-12 09:03:22 -06:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-08-25 11:09:28 -06:00
|
|
|
from . import config_flow
|
2020-08-13 15:10:55 -06:00
|
|
|
from .const import (
|
|
|
|
AUTH_HANDLER,
|
|
|
|
COORDINATOR,
|
|
|
|
DOMAIN,
|
|
|
|
OAUTH2_AUTH_URL,
|
|
|
|
OAUTH2_TOKEN_URL,
|
2020-08-12 09:03:22 -06:00
|
|
|
)
|
2020-08-25 11:09:28 -06:00
|
|
|
from .exceptions import AuthError, CannotConnect
|
2020-08-13 15:10:55 -06:00
|
|
|
from .update_coordinator import GeKitchenUpdateCoordinator
|
2020-08-12 09:03:22 -06:00
|
|
|
|
2020-08-13 15:10:55 -06:00
|
|
|
CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA)
|
2020-08-24 19:22:32 -06:00
|
|
|
PLATFORMS = ["sensor", "binary_sensor", "switch"]
|
2020-08-12 09:03:22 -06:00
|
|
|
|
2020-08-13 15:10:55 -06:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2020-08-12 09:03:22 -06:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
|
|
"""Set up the ge_kitchen component."""
|
2020-08-13 15:10:55 -06:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2020-08-12 09:03:22 -06:00
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Set up ge_kitchen from a config entry."""
|
2020-08-24 19:22:32 -06:00
|
|
|
coordinator = GeKitchenUpdateCoordinator(hass, entry)
|
2020-08-13 15:10:55 -06:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = coordinator
|
2020-08-12 09:03:22 -06:00
|
|
|
|
2020-08-25 11:09:28 -06:00
|
|
|
try:
|
|
|
|
await coordinator.async_start_client()
|
|
|
|
except GeAuthError as exc:
|
|
|
|
raise AuthError('Authentication failure') from exc
|
|
|
|
except GeServerError as exc:
|
|
|
|
raise CannotConnect('Cannot connect (server error)') from exc
|
|
|
|
except Exception as exc:
|
|
|
|
raise CannotConnect('Unknown connection failure') from exc
|
|
|
|
|
|
|
|
try:
|
|
|
|
with async_timeout.timeout(30):
|
|
|
|
await coordinator.initialization_future
|
|
|
|
except TimeoutError as exc:
|
|
|
|
raise CannotConnect('Initialization timed out') from exc
|
2020-08-12 09:03:22 -06:00
|
|
|
|
|
|
|
for component in PLATFORMS:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, component)
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
unload_ok = all(
|
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
|
|
|
hass.config_entries.async_forward_entry_unload(entry, component)
|
|
|
|
for component in PLATFORMS
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|
2020-08-25 11:09:28 -06:00
|
|
|
|
|
|
|
|
|
|
|
async def async_update_options(hass, config_entry):
|
|
|
|
"""Update options."""
|
|
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|