2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Canary devices."""
|
2020-09-19 04:22:19 +00:00
|
|
|
import asyncio
|
2017-12-08 09:40:45 +00:00
|
|
|
from datetime import timedelta
|
2019-10-18 23:01:59 +00:00
|
|
|
import logging
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2019-10-18 23:01:59 +00:00
|
|
|
from canary.api import Api
|
2017-12-08 09:40:45 +00:00
|
|
|
from requests import ConnectTimeout, HTTPError
|
2019-10-18 23:01:59 +00:00
|
|
|
import voluptuous as vol
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
from homeassistant.components.camera.const import DOMAIN as CAMERA_DOMAIN
|
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2019-10-18 23:01:59 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME
|
2020-09-19 04:22:19 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-10-18 23:01:59 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2020-09-19 04:22:19 +00:00
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_FFMPEG_ARGUMENTS,
|
2020-10-01 08:26:26 +00:00
|
|
|
DATA_COORDINATOR,
|
2020-09-19 04:22:19 +00:00
|
|
|
DATA_UNDO_UPDATE_LISTENER,
|
|
|
|
DEFAULT_FFMPEG_ARGUMENTS,
|
|
|
|
DEFAULT_TIMEOUT,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2020-10-01 08:26:26 +00:00
|
|
|
from .coordinator import CanaryDataUpdateCoordinator
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
PLATFORMS = ["alarm_control_panel", "camera", "sensor"]
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass: HomeAssistantType, config: dict) -> bool:
|
|
|
|
"""Set up the Canary integration."""
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
|
|
|
|
if hass.config_entries.async_entries(DOMAIN):
|
|
|
|
return True
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
ffmpeg_arguments = DEFAULT_FFMPEG_ARGUMENTS
|
|
|
|
if CAMERA_DOMAIN in config:
|
|
|
|
camera_config = next(
|
|
|
|
(item for item in config[CAMERA_DOMAIN] if item["platform"] == DOMAIN),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
|
|
|
|
if camera_config:
|
|
|
|
ffmpeg_arguments = camera_config.get(
|
|
|
|
CONF_FFMPEG_ARGUMENTS, DEFAULT_FFMPEG_ARGUMENTS
|
|
|
|
)
|
|
|
|
|
|
|
|
if DOMAIN in config:
|
|
|
|
if ffmpeg_arguments != DEFAULT_FFMPEG_ARGUMENTS:
|
|
|
|
config[DOMAIN][CONF_FFMPEG_ARGUMENTS] = ffmpeg_arguments
|
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data=config[DOMAIN],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return True
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up Canary from a config entry."""
|
|
|
|
if not entry.options:
|
|
|
|
options = {
|
|
|
|
CONF_FFMPEG_ARGUMENTS: entry.data.get(
|
|
|
|
CONF_FFMPEG_ARGUMENTS, DEFAULT_FFMPEG_ARGUMENTS
|
|
|
|
),
|
|
|
|
CONF_TIMEOUT: entry.data.get(CONF_TIMEOUT, DEFAULT_TIMEOUT),
|
|
|
|
}
|
|
|
|
hass.config_entries.async_update_entry(entry, options=options)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
try:
|
2020-10-01 08:26:26 +00:00
|
|
|
canary_api = await hass.async_add_executor_job(_get_canary_api_instance, entry)
|
2020-09-19 04:22:19 +00:00
|
|
|
except (ConnectTimeout, HTTPError) as error:
|
|
|
|
_LOGGER.error("Unable to connect to Canary service: %s", str(error))
|
|
|
|
raise ConfigEntryNotReady from error
|
|
|
|
|
2020-10-01 08:26:26 +00:00
|
|
|
coordinator = CanaryDataUpdateCoordinator(hass, api=canary_api)
|
|
|
|
await coordinator.async_refresh()
|
|
|
|
|
|
|
|
if not coordinator.last_update_success:
|
|
|
|
raise ConfigEntryNotReady
|
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
undo_listener = entry.add_update_listener(_async_update_listener)
|
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
2020-10-01 08:26:26 +00:00
|
|
|
DATA_COORDINATOR: coordinator,
|
2020-09-19 04:22:19 +00:00
|
|
|
DATA_UNDO_UPDATE_LISTENER: undo_listener,
|
|
|
|
}
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
for component in PLATFORMS:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, component)
|
|
|
|
)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
|
|
|
"""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][entry.entry_id][DATA_UNDO_UPDATE_LISTENER]()
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
|
|
|
async def _async_update_listener(hass: HomeAssistantType, entry: ConfigEntry) -> None:
|
|
|
|
"""Handle options update."""
|
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|
|
|
|
|
|
|
|
|
2020-10-01 08:26:26 +00:00
|
|
|
def _get_canary_api_instance(entry: ConfigEntry) -> Api:
|
|
|
|
"""Initialize a new instance of CanaryApi."""
|
2020-09-20 22:38:02 +00:00
|
|
|
canary = Api(
|
|
|
|
entry.data[CONF_USERNAME],
|
|
|
|
entry.data[CONF_PASSWORD],
|
|
|
|
entry.options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT),
|
|
|
|
)
|
|
|
|
|
2020-10-01 08:26:26 +00:00
|
|
|
return canary
|