Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for the LiteJet lighting system."""
2 
3 import logging
4 
5 import pylitejet
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_PORT, EVENT_HOMEASSISTANT_STOP
9 from homeassistant.core import Event, HomeAssistant
10 from homeassistant.exceptions import ConfigEntryNotReady
11 
12 from .const import DOMAIN, PLATFORMS
13 
14 _LOGGER = logging.getLogger(__name__)
15 
16 
17 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
18  """Set up LiteJet via a config entry."""
19  port = entry.data[CONF_PORT]
20 
21  try:
22  system = await pylitejet.open(port)
23  except pylitejet.LiteJetError as exc:
24  raise ConfigEntryNotReady from exc
25 
26  def handle_connected_changed(connected: bool, reason: str) -> None:
27  if connected:
28  _LOGGER.debug("Connected")
29  else:
30  _LOGGER.warning("Disconnected %s", reason)
31 
32  system.on_connected_changed(handle_connected_changed)
33 
34  async def handle_stop(event: Event) -> None:
35  await system.close()
36 
37  entry.async_on_unload(
38  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, handle_stop)
39  )
40 
41  hass.data[DOMAIN] = system
42 
43  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
44 
45  return True
46 
47 
48 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
49  """Unload a LiteJet config entry."""
50  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
51 
52  if unload_ok:
53  await hass.data[DOMAIN].close()
54  hass.data.pop(DOMAIN)
55 
56  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:17
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:48