1 """The Nanoleaf integration."""
3 from __future__
import annotations
6 from contextlib
import suppress
9 from aionanoleaf
import EffectsEvent, Nanoleaf, StateEvent, TouchEvent
24 from .const
import DOMAIN, NANOLEAF_EVENT, TOUCH_GESTURE_TRIGGER_MAP, TOUCH_MODELS
25 from .coordinator
import NanoleafCoordinator
27 _LOGGER = logging.getLogger(__name__)
29 PLATFORMS = [Platform.BUTTON, Platform.EVENT, Platform.LIGHT]
32 type NanoleafConfigEntry = ConfigEntry[NanoleafCoordinator]
36 """Set up Nanoleaf from a config entry."""
43 await coordinator.async_config_entry_first_refresh()
45 async
def light_event_callback(event: StateEvent | EffectsEvent) ->
None:
46 """Receive state and effect event."""
47 coordinator.async_set_updated_data(
None)
49 if supports_touch := nanoleaf.model
in TOUCH_MODELS:
50 device_registry = dr.async_get(hass)
51 device_entry = device_registry.async_get_or_create(
52 config_entry_id=entry.entry_id,
53 identifiers={(DOMAIN, nanoleaf.serial_no)},
56 async
def touch_event_callback(event: TouchEvent) ->
None:
57 """Receive touch event."""
58 gesture_type = TOUCH_GESTURE_TRIGGER_MAP.get(event.gesture_id)
59 if gesture_type
is None:
61 "Received unknown touch gesture ID %s", event.gesture_id
64 _LOGGER.debug(
"Received touch gesture %s", gesture_type)
67 {CONF_DEVICE_ID: device_entry.id, CONF_TYPE: gesture_type},
70 hass, f
"nanoleaf_gesture_{nanoleaf.serial_no}", gesture_type
73 event_listener = asyncio.create_task(
74 nanoleaf.listen_events(
75 state_callback=light_event_callback,
76 effects_callback=light_event_callback,
77 touch_callback=touch_event_callback
if supports_touch
else None,
81 async
def _cancel_listener() -> None:
82 event_listener.cancel()
83 with suppress(asyncio.CancelledError):
86 entry.async_on_unload(_cancel_listener)
88 entry.runtime_data = coordinator
90 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
96 """Unload a config entry."""
97 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, NanoleafConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, NanoleafConfigEntry entry)
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)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)