2019-02-14 04:35:12 +00:00
|
|
|
"""Support for the LiteJet lighting system."""
|
2024-03-08 18:15:59 +00:00
|
|
|
|
2021-02-23 20:20:58 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import pylitejet
|
2016-11-02 03:44:25 +00:00
|
|
|
|
2024-01-12 21:50:15 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-01-17 15:53:16 +00:00
|
|
|
from homeassistant.const import CONF_PORT, EVENT_HOMEASSISTANT_STOP
|
2023-08-21 10:20:48 +00:00
|
|
|
from homeassistant.core import Event, HomeAssistant
|
2021-02-23 20:20:58 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2016-11-02 03:44:25 +00:00
|
|
|
|
2024-01-12 21:50:15 +00:00
|
|
|
from .const import DOMAIN, PLATFORMS
|
2016-11-02 03:44:25 +00:00
|
|
|
|
2021-02-23 20:20:58 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-11-02 03:44:25 +00:00
|
|
|
|
|
|
|
|
2021-02-23 20:20:58 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up LiteJet via a config entry."""
|
|
|
|
port = entry.data[CONF_PORT]
|
2016-11-02 03:44:25 +00:00
|
|
|
|
2021-02-23 20:20:58 +00:00
|
|
|
try:
|
2023-01-17 15:53:16 +00:00
|
|
|
system = await pylitejet.open(port)
|
2023-01-24 13:22:16 +00:00
|
|
|
except pylitejet.LiteJetError as exc:
|
|
|
|
raise ConfigEntryNotReady from exc
|
|
|
|
|
|
|
|
def handle_connected_changed(connected: bool, reason: str) -> None:
|
|
|
|
if connected:
|
|
|
|
_LOGGER.info("Connected")
|
|
|
|
else:
|
|
|
|
_LOGGER.warning("Disconnected %s", reason)
|
|
|
|
|
|
|
|
system.on_connected_changed(handle_connected_changed)
|
2016-11-02 03:44:25 +00:00
|
|
|
|
2023-02-24 16:51:48 +00:00
|
|
|
async def handle_stop(event: Event) -> None:
|
2023-01-17 15:53:16 +00:00
|
|
|
await system.close()
|
|
|
|
|
|
|
|
entry.async_on_unload(
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, handle_stop)
|
|
|
|
)
|
|
|
|
|
2021-02-23 20:20:58 +00:00
|
|
|
hass.data[DOMAIN] = system
|
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2016-11-02 03:44:25 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-02-23 20:20:58 +00:00
|
|
|
"""Unload a LiteJet config entry."""
|
2021-04-27 16:49:13 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2021-02-23 20:20:58 +00:00
|
|
|
|
|
|
|
if unload_ok:
|
2023-01-17 15:53:16 +00:00
|
|
|
await hass.data[DOMAIN].close()
|
2021-02-23 20:20:58 +00:00
|
|
|
hass.data.pop(DOMAIN)
|
|
|
|
|
|
|
|
return unload_ok
|