diff --git a/.coveragerc b/.coveragerc index 5ce91028102..696cd5ce3b0 100644 --- a/.coveragerc +++ b/.coveragerc @@ -762,6 +762,7 @@ omit = homeassistant/components/nad/media_player.py homeassistant/components/nanoleaf/__init__.py homeassistant/components/nanoleaf/button.py + homeassistant/components/nanoleaf/device_trigger.py homeassistant/components/nanoleaf/diagnostics.py homeassistant/components/nanoleaf/entity.py homeassistant/components/nanoleaf/light.py diff --git a/homeassistant/components/nanoleaf/__init__.py b/homeassistant/components/nanoleaf/__init__.py index 677c0d4cc2a..9e9cf1d6ca4 100644 --- a/homeassistant/components/nanoleaf/__init__.py +++ b/homeassistant/components/nanoleaf/__init__.py @@ -6,16 +6,32 @@ from dataclasses import dataclass from datetime import timedelta import logging -from aionanoleaf import EffectsEvent, InvalidToken, Nanoleaf, StateEvent, Unavailable +from aionanoleaf import ( + EffectsEvent, + InvalidToken, + Nanoleaf, + StateEvent, + TouchEvent, + Unavailable, +) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_HOST, CONF_TOKEN, Platform +from homeassistant.const import ( + CONF_DEVICE_ID, + CONF_HOST, + CONF_TOKEN, + CONF_TYPE, + Platform, +) from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed +from homeassistant.helpers import device_registry as dr from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed -from .const import DOMAIN +from .const import DOMAIN, NANOLEAF_EVENT, TOUCH_GESTURE_TRIGGER_MAP, TOUCH_MODELS + +_LOGGER = logging.getLogger(__name__) PLATFORMS = [Platform.BUTTON, Platform.LIGHT] @@ -46,7 +62,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: coordinator = DataUpdateCoordinator( hass, - logging.getLogger(__name__), + _LOGGER, name=entry.title, update_interval=timedelta(minutes=1), update_method=async_get_state, @@ -54,14 +70,34 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await coordinator.async_config_entry_first_refresh() - async def update_light_state_callback(event: StateEvent | EffectsEvent) -> None: + async def light_event_callback(event: StateEvent | EffectsEvent) -> None: """Receive state and effect event.""" coordinator.async_set_updated_data(None) + if supports_touch := nanoleaf.model in TOUCH_MODELS: + device_registry = dr.async_get(hass) + device_entry = device_registry.async_get_or_create( + config_entry_id=entry.entry_id, + identifiers={(DOMAIN, nanoleaf.serial_no)}, + ) + + async def touch_event_callback(event: TouchEvent) -> None: + """Receive touch event.""" + gesture_type = TOUCH_GESTURE_TRIGGER_MAP.get(event.gesture_id) + if gesture_type is None: + _LOGGER.debug("Received unknown touch gesture ID %s", event.gesture_id) + return + _LOGGER.warning("Received touch gesture %s", gesture_type) + hass.bus.async_fire( + NANOLEAF_EVENT, + {CONF_DEVICE_ID: device_entry.id, CONF_TYPE: gesture_type}, + ) + event_listener = asyncio.create_task( nanoleaf.listen_events( - state_callback=update_light_state_callback, - effects_callback=update_light_state_callback, + state_callback=light_event_callback, + effects_callback=light_event_callback, + touch_callback=touch_event_callback if supports_touch else None, ) ) diff --git a/homeassistant/components/nanoleaf/const.py b/homeassistant/components/nanoleaf/const.py index 505af8ce69d..7e246209733 100644 --- a/homeassistant/components/nanoleaf/const.py +++ b/homeassistant/components/nanoleaf/const.py @@ -1,3 +1,14 @@ """Constants for Nanoleaf integration.""" DOMAIN = "nanoleaf" + +NANOLEAF_EVENT = f"{DOMAIN}_event" + +TOUCH_MODELS = {"NL29", "NL42", "NL52"} + +TOUCH_GESTURE_TRIGGER_MAP = { + 2: "swipe_up", + 3: "swipe_down", + 4: "swipe_left", + 5: "swipe_right", +} diff --git a/homeassistant/components/nanoleaf/device_trigger.py b/homeassistant/components/nanoleaf/device_trigger.py new file mode 100644 index 00000000000..311d12506ba --- /dev/null +++ b/homeassistant/components/nanoleaf/device_trigger.py @@ -0,0 +1,79 @@ +"""Provides device triggers for Nanoleaf.""" +from __future__ import annotations + +from typing import Any + +import voluptuous as vol + +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) +from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA +from homeassistant.components.device_automation.exceptions import DeviceNotFound +from homeassistant.components.homeassistant.triggers import event as event_trigger +from homeassistant.const import ( + CONF_DEVICE_ID, + CONF_DOMAIN, + CONF_EVENT, + CONF_PLATFORM, + CONF_TYPE, +) +from homeassistant.core import CALLBACK_TYPE, HomeAssistant +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.typing import ConfigType + +from .const import DOMAIN, NANOLEAF_EVENT, TOUCH_GESTURE_TRIGGER_MAP, TOUCH_MODELS + +TRIGGER_TYPES = TOUCH_GESTURE_TRIGGER_MAP.values() + +TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend( + { + vol.Required(CONF_DOMAIN): DOMAIN, + vol.Required(CONF_DEVICE_ID): str, + vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES), + } +) + + +async def async_get_triggers( + hass: HomeAssistant, device_id: str +) -> list[dict[str, Any]]: + """List device triggers for Nanoleaf devices.""" + device_registry = dr.async_get(hass) + device_entry = device_registry.async_get(device_id) + if device_entry is None: + raise DeviceNotFound(f"Device ID {device_id} is not valid") + if device_entry.model not in TOUCH_MODELS: + return [] + return [ + { + CONF_PLATFORM: "device", + CONF_DOMAIN: DOMAIN, + CONF_DEVICE_ID: device_id, + CONF_TYPE: trigger_type, + } + for trigger_type in TRIGGER_TYPES + ] + + +async def async_attach_trigger( + hass: HomeAssistant, + config: ConfigType, + action: AutomationActionType, + automation_info: AutomationTriggerInfo, +) -> CALLBACK_TYPE: + """Attach a trigger.""" + event_config = event_trigger.TRIGGER_SCHEMA( + { + event_trigger.CONF_PLATFORM: CONF_EVENT, + event_trigger.CONF_EVENT_TYPE: NANOLEAF_EVENT, + event_trigger.CONF_EVENT_DATA: { + CONF_TYPE: config[CONF_TYPE], + CONF_DEVICE_ID: config[CONF_DEVICE_ID], + }, + } + ) + return await event_trigger.async_attach_trigger( + hass, event_config, action, automation_info, platform_type="device" + ) diff --git a/homeassistant/components/nanoleaf/strings.json b/homeassistant/components/nanoleaf/strings.json index 96fcfd2622a..80eb2ded7d0 100644 --- a/homeassistant/components/nanoleaf/strings.json +++ b/homeassistant/components/nanoleaf/strings.json @@ -24,5 +24,13 @@ "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]", "unknown": "[%key:common::config_flow::error::unknown%]" } + }, + "device_automation": { + "trigger_type": { + "swipe_up": "Swipe Up", + "swipe_down": "Swipe Down", + "swipe_left": "Swipe Left", + "swipe_right": "Swipe Right" + } } } diff --git a/homeassistant/components/nanoleaf/translations/en.json b/homeassistant/components/nanoleaf/translations/en.json index 7696f056aa3..7064c617b0e 100644 --- a/homeassistant/components/nanoleaf/translations/en.json +++ b/homeassistant/components/nanoleaf/translations/en.json @@ -24,5 +24,13 @@ } } } + }, + "device_automation": { + "trigger_type": { + "swipe_down": "Swipe Down", + "swipe_left": "Swipe Left", + "swipe_right": "Swipe Right", + "swipe_up": "Swipe Up" + } } } \ No newline at end of file