Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The twinkly component."""
2 
3 from aiohttp import ClientError
4 from ttls.client import Twinkly
5 
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import ATTR_SW_VERSION, CONF_HOST, Platform
8 from homeassistant.core import HomeAssistant
9 from homeassistant.exceptions import ConfigEntryNotReady
10 from homeassistant.helpers.aiohttp_client import async_get_clientsession
11 
12 from .const import ATTR_VERSION, DATA_CLIENT, DATA_DEVICE_INFO, DOMAIN
13 
14 PLATFORMS = [Platform.LIGHT]
15 
16 
17 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
18  """Set up entries from config flow."""
19  hass.data.setdefault(DOMAIN, {})
20 
21  # We setup the client here so if at some point we add any other entity for this device,
22  # we will be able to properly share the connection.
23  host = entry.data[CONF_HOST]
24 
25  hass.data[DOMAIN].setdefault(entry.entry_id, {})
26 
27  client = Twinkly(host, async_get_clientsession(hass))
28 
29  try:
30  device_info = await client.get_details()
31  software_version = await client.get_firmware_version()
32  except (TimeoutError, ClientError) as exception:
33  raise ConfigEntryNotReady from exception
34 
35  hass.data[DOMAIN][entry.entry_id] = {
36  DATA_CLIENT: client,
37  DATA_DEVICE_INFO: device_info,
38  ATTR_SW_VERSION: software_version.get(ATTR_VERSION),
39  }
40  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
41 
42  return True
43 
44 
45 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
46  """Remove a twinkly entry."""
47 
48  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
49  if unload_ok:
50  hass.data[DOMAIN].pop(entry.entry_id)
51 
52  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:45
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:17
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)