core/homeassistant/components/skybell/__init__.py

86 lines
2.7 KiB
Python
Raw Normal View History

"""Support for the Skybell HD Doorbell."""
2022-06-05 02:37:08 +00:00
from __future__ import annotations
2022-06-05 02:37:08 +00:00
import asyncio
from aioskybell import Skybell
from aioskybell.exceptions import SkybellAuthenticationException, SkybellException
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
2022-06-05 02:37:08 +00:00
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN
2022-06-05 02:37:08 +00:00
from .coordinator import SkybellDataUpdateCoordinator
2022-06-05 02:37:08 +00:00
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.CAMERA,
Platform.LIGHT,
Platform.SENSOR,
Platform.SWITCH,
]
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the SkyBell component."""
hass.data.setdefault(DOMAIN, {})
if DOMAIN in config:
async_create_issue(
hass,
DOMAIN,
"removed_yaml",
breaks_in_ha_version="2022.9.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="removed_yaml",
2022-06-05 02:37:08 +00:00
)
2022-06-05 02:37:08 +00:00
return True
2022-06-05 02:37:08 +00:00
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Skybell from a config entry."""
email = entry.data[CONF_EMAIL]
password = entry.data[CONF_PASSWORD]
2022-06-05 02:37:08 +00:00
api = Skybell(
username=email,
password=password,
get_devices=True,
cache_path=hass.config.path(f"./skybell_{entry.unique_id}.pickle"),
session=async_get_clientsession(hass),
)
try:
2022-06-05 02:37:08 +00:00
devices = await api.async_initialize()
except SkybellAuthenticationException as ex:
raise ConfigEntryAuthFailed from ex
2022-06-05 02:37:08 +00:00
except SkybellException as ex:
raise ConfigEntryNotReady(f"Unable to connect to Skybell service: {ex}") from ex
device_coordinators: list[SkybellDataUpdateCoordinator] = [
SkybellDataUpdateCoordinator(hass, device) for device in devices
]
await asyncio.gather(
*[
coordinator.async_config_entry_first_refresh()
for coordinator in device_coordinators
]
)
hass.data[DOMAIN][entry.entry_id] = device_coordinators
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
2022-06-05 02:37:08 +00:00
return True
2022-06-05 02:37:08 +00:00
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok