2021-05-05 23:41:32 +00:00
|
|
|
"""Support for Elgato Lights."""
|
2021-03-09 18:52:53 +00:00
|
|
|
import logging
|
|
|
|
|
2019-12-08 08:26:31 +00:00
|
|
|
from elgato import Elgato, ElgatoConnectionError
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-04 12:26:40 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
|
2019-12-08 08:26:31 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
|
2021-11-12 17:03:57 +00:00
|
|
|
from .const import DOMAIN
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2021-12-04 12:26:40 +00:00
|
|
|
PLATFORMS = [Platform.BUTTON, Platform.LIGHT]
|
2021-04-27 06:46:49 +00:00
|
|
|
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-05-05 23:41:32 +00:00
|
|
|
"""Set up Elgato Light from a config entry."""
|
2019-12-08 08:26:31 +00:00
|
|
|
session = async_get_clientsession(hass)
|
2020-08-27 11:56:20 +00:00
|
|
|
elgato = Elgato(
|
|
|
|
entry.data[CONF_HOST],
|
|
|
|
port=entry.data[CONF_PORT],
|
|
|
|
session=session,
|
|
|
|
)
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
# Ensure we can connect to it
|
|
|
|
try:
|
|
|
|
await elgato.info()
|
|
|
|
except ElgatoConnectionError as exception:
|
2021-03-09 18:52:53 +00:00
|
|
|
logging.getLogger(__name__).debug("Unable to connect: %s", exception)
|
2019-12-08 08:26:31 +00:00
|
|
|
raise ConfigEntryNotReady from exception
|
|
|
|
|
2021-11-12 17:03:57 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = elgato
|
2021-04-27 06:46:49 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-05-05 23:41:32 +00:00
|
|
|
"""Unload Elgato Light config entry."""
|
2019-12-08 08:26:31 +00:00
|
|
|
# Unload entities for this entry/device.
|
2021-04-27 06:46:49 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
if unload_ok:
|
|
|
|
# Cleanup
|
|
|
|
del hass.data[DOMAIN][entry.entry_id]
|
|
|
|
if not hass.data[DOMAIN]:
|
|
|
|
del hass.data[DOMAIN]
|
|
|
|
return unload_ok
|