Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Plum Lightpad devices."""
2 
3 import logging
4 
5 from aiohttp import ContentTypeError
6 from requests.exceptions import ConnectTimeout, HTTPError
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import (
10  CONF_PASSWORD,
11  CONF_USERNAME,
12  EVENT_HOMEASSISTANT_STOP,
13  Platform,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.exceptions import ConfigEntryNotReady
17 
18 from .const import DOMAIN
19 from .utils import load_plum
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 PLATFORMS = [Platform.LIGHT]
24 
25 
26 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
27  """Set up Plum Lightpad from a config entry."""
28  _LOGGER.debug("Setting up config entry with ID = %s", entry.unique_id)
29 
30  username = entry.data[CONF_USERNAME]
31  password = entry.data[CONF_PASSWORD]
32 
33  try:
34  plum = await load_plum(username, password, hass)
35  except ContentTypeError as ex:
36  _LOGGER.error("Unable to authenticate to Plum cloud: %s", ex)
37  return False
38  except (ConnectTimeout, HTTPError) as ex:
39  _LOGGER.error("Unable to connect to Plum cloud: %s", ex)
40  raise ConfigEntryNotReady from ex
41 
42  hass.data.setdefault(DOMAIN, {})
43  hass.data[DOMAIN][entry.entry_id] = plum
44 
45  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
46 
47  def cleanup(event):
48  """Clean up resources."""
49  plum.cleanup()
50 
51  entry.async_on_unload(hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, cleanup))
52  return True
Plum load_plum(str username, str password, HomeAssistant hass)
Definition: utils.py:9
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:26