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-13 17:44:27 +00:00
|
|
|
from .const import CONF_ENTRY_HOST, CONF_ENTRY_ID, 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.
|
2021-05-27 13:56:20 +00:00
|
|
|
uuid = entry.data[CONF_ENTRY_ID]
|
|
|
|
host = entry.data[CONF_ENTRY_HOST]
|
2020-11-19 17:22:12 +00:00
|
|
|
|
2022-01-13 17:44:27 +00:00
|
|
|
hass.data[DOMAIN].setdefault(uuid, {})
|
|
|
|
|
|
|
|
client = Twinkly(host, async_get_clientsession(hass))
|
|
|
|
|
|
|
|
try:
|
|
|
|
device_info = await client.get_details()
|
|
|
|
except (asyncio.TimeoutError, ClientError) as exception:
|
|
|
|
raise ConfigEntryNotReady from exception
|
|
|
|
|
|
|
|
hass.data[DOMAIN][uuid][DATA_CLIENT] = client
|
|
|
|
hass.data[DOMAIN][uuid][DATA_DEVICE_INFO] = device_info
|
2020-11-19 17:22:12 +00:00
|
|
|
|
2021-05-27 13:56:20 +00:00
|
|
|
hass.config_entries.async_setup_platforms(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."""
|
|
|
|
|
|
|
|
# For now light entries don't have unload method, so we don't have to async_forward_entry_unload
|
|
|
|
# However we still have to cleanup the shared client!
|
2021-05-27 13:56:20 +00:00
|
|
|
uuid = entry.data[CONF_ENTRY_ID]
|
2020-11-19 17:22:12 +00:00
|
|
|
hass.data[DOMAIN].pop(uuid)
|
|
|
|
|
|
|
|
return True
|