Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Energenie Power-Sockets (EGPS) integration."""
2 
3 from pyegps import PowerStripUSB, get_device
4 from pyegps.exceptions import MissingLibrary, UsbError
5 
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import Platform
8 from homeassistant.core import HomeAssistant
9 from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
10 
11 from .const import CONF_DEVICE_API_ID, DOMAIN
12 
13 PLATFORMS = [Platform.SWITCH]
14 
15 
16 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
17  """Set up Energenie Power Sockets."""
18  try:
19  powerstrip: PowerStripUSB | None = get_device(entry.data[CONF_DEVICE_API_ID])
20 
21  except (MissingLibrary, UsbError) as ex:
22  raise ConfigEntryError("Can't access usb devices.") from ex
23 
24  if powerstrip is None:
25  raise ConfigEntryNotReady(
26  "Can't access Energenie Power Sockets, will retry later."
27  )
28 
29  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = powerstrip
30  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
31 
32  return True
33 
34 
35 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
36  """Unload config entry."""
37  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
38  powerstrip = hass.data[DOMAIN].pop(entry.entry_id)
39  powerstrip.release()
40 
41  if not hass.data[DOMAIN]:
42  hass.data.pop(DOMAIN)
43 
44  return unload_ok
DeviceEntry get_device(HomeAssistant hass, str unique_id)
Definition: util.py:12
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:16
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:35