2020-11-19 17:22:12 +00:00
|
|
|
"""The twinkly component."""
|
|
|
|
|
2022-01-13 17:44:27 +00:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from aiohttp import ClientError
|
|
|
|
from ttls.client import Twinkly
|
2020-11-19 17:22:12 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-04 13:10:01 +00:00
|
|
|
from homeassistant.const import Platform
|
2021-04-22 14:53:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-01-13 17:44:27 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2020-11-19 17:22:12 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
|
2022-01-14 21:07:15 +00:00
|
|
|
from .const import CONF_HOST, DATA_CLIENT, DATA_DEVICE_INFO, DOMAIN
|
2020-11-19 17:22:12 +00:00
|
|
|
|
2021-12-04 13:10:01 +00:00
|
|
|
PLATFORMS = [Platform.LIGHT]
|
2020-11-19 17:22:12 +00:00
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-11-19 17:22:12 +00:00
|
|
|
"""Set up entries from config flow."""
|
2022-01-13 17:44:27 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2020-11-19 17:22:12 +00:00
|
|
|
|
|
|
|
# We setup the client here so if at some point we add any other entity for this device,
|
|
|
|
# we will be able to properly share the connection.
|
2022-01-14 21:07:15 +00:00
|
|
|
host = entry.data[CONF_HOST]
|
2020-11-19 17:22:12 +00:00
|
|
|
|
2022-01-14 21:07:15 +00:00
|
|
|
hass.data[DOMAIN].setdefault(entry.entry_id, {})
|
2022-01-13 17:44:27 +00:00
|
|
|
|
|
|
|
client = Twinkly(host, async_get_clientsession(hass))
|
|
|
|
|
|
|
|
try:
|
|
|
|
device_info = await client.get_details()
|
|
|
|
except (asyncio.TimeoutError, ClientError) as exception:
|
|
|
|
raise ConfigEntryNotReady from exception
|
|
|
|
|
2022-01-14 21:07:15 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id][DATA_CLIENT] = client
|
|
|
|
hass.data[DOMAIN][entry.entry_id][DATA_DEVICE_INFO] = device_info
|
2020-11-19 17:22:12 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2021-04-27 20:19:57 +00:00
|
|
|
|
2020-11-19 17:22:12 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-11-19 17:22:12 +00:00
|
|
|
"""Remove a twinkly entry."""
|
|
|
|
|
2022-01-14 21:07:15 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2020-11-19 17:22:12 +00:00
|
|
|
|
2022-01-14 21:07:15 +00:00
|
|
|
return unload_ok
|