diff --git a/.coveragerc b/.coveragerc index 303f9696fe3..3b44334249d 100644 --- a/.coveragerc +++ b/.coveragerc @@ -855,6 +855,7 @@ omit = homeassistant/components/nanoleaf/button.py homeassistant/components/nanoleaf/coordinator.py homeassistant/components/nanoleaf/entity.py + homeassistant/components/nanoleaf/event.py homeassistant/components/nanoleaf/light.py homeassistant/components/neato/__init__.py homeassistant/components/neato/api.py diff --git a/homeassistant/components/nanoleaf/__init__.py b/homeassistant/components/nanoleaf/__init__.py index f607c7277ec..4a34c2843aa 100644 --- a/homeassistant/components/nanoleaf/__init__.py +++ b/homeassistant/components/nanoleaf/__init__.py @@ -19,13 +19,14 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.dispatcher import async_dispatcher_send from .const import DOMAIN, NANOLEAF_EVENT, TOUCH_GESTURE_TRIGGER_MAP, TOUCH_MODELS from .coordinator import NanoleafCoordinator _LOGGER = logging.getLogger(__name__) -PLATFORMS = [Platform.BUTTON, Platform.LIGHT] +PLATFORMS = [Platform.BUTTON, Platform.EVENT, Platform.LIGHT] type NanoleafConfigEntry = ConfigEntry[NanoleafCoordinator] @@ -65,6 +66,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: NanoleafConfigEntry) -> NANOLEAF_EVENT, {CONF_DEVICE_ID: device_entry.id, CONF_TYPE: gesture_type}, ) + async_dispatcher_send( + hass, f"nanoleaf_gesture_{nanoleaf.serial_no}", gesture_type + ) event_listener = asyncio.create_task( nanoleaf.listen_events( diff --git a/homeassistant/components/nanoleaf/event.py b/homeassistant/components/nanoleaf/event.py new file mode 100644 index 00000000000..5763c2aa595 --- /dev/null +++ b/homeassistant/components/nanoleaf/event.py @@ -0,0 +1,55 @@ +"""Support for Nanoleaf event entity.""" + +from homeassistant.components.event import EventEntity +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import NanoleafConfigEntry, NanoleafCoordinator +from .const import TOUCH_MODELS +from .entity import NanoleafEntity + + +async def async_setup_entry( + hass: HomeAssistant, + entry: NanoleafConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the Nanoleaf event.""" + coordinator = entry.runtime_data + if coordinator.nanoleaf.model in TOUCH_MODELS: + async_add_entities([NanoleafGestureEvent(coordinator)]) + + +class NanoleafGestureEvent(NanoleafEntity, EventEntity): + """Representation of a Nanoleaf event entity.""" + + _attr_event_types = [ + "swipe_up", + "swipe_down", + "swipe_left", + "swipe_right", + ] + _attr_translation_key = "touch" + + def __init__(self, coordinator: NanoleafCoordinator) -> None: + """Initialize the Nanoleaf event entity.""" + super().__init__(coordinator) + self._attr_unique_id = f"{self._nanoleaf.serial_no}_gesture" + + async def async_added_to_hass(self) -> None: + """Subscribe to Nanoleaf events.""" + await super().async_added_to_hass() + self.async_on_remove( + async_dispatcher_connect( + self.hass, + f"nanoleaf_gesture_{self._nanoleaf.serial_no}", + self._async_handle_event, + ) + ) + + @callback + def _async_handle_event(self, gesture: str) -> None: + """Handle the event.""" + self._trigger_event(gesture) + self.async_write_ha_state() diff --git a/homeassistant/components/nanoleaf/icons.json b/homeassistant/components/nanoleaf/icons.json index 3f4ebf9ed9f..bedfc2f0718 100644 --- a/homeassistant/components/nanoleaf/icons.json +++ b/homeassistant/components/nanoleaf/icons.json @@ -1,5 +1,10 @@ { "entity": { + "event": { + "touch": { + "default": "mdi:gesture" + } + }, "light": { "light": { "default": "mdi:triangle-outline" diff --git a/homeassistant/components/nanoleaf/strings.json b/homeassistant/components/nanoleaf/strings.json index 13e7c9a11a3..40cd7294ec3 100644 --- a/homeassistant/components/nanoleaf/strings.json +++ b/homeassistant/components/nanoleaf/strings.json @@ -30,10 +30,27 @@ }, "device_automation": { "trigger_type": { - "swipe_up": "Swipe Up", - "swipe_down": "Swipe Down", - "swipe_left": "Swipe Left", - "swipe_right": "Swipe Right" + "swipe_up": "[%key:component::nanoleaf::entity::event::touch::state_attributes::event_type::state::swipe_up%]", + "swipe_down": "[%key:component::nanoleaf::entity::event::touch::state_attributes::event_type::state::swipe_down%]", + "swipe_left": "[%key:component::nanoleaf::entity::event::touch::state_attributes::event_type::state::swipe_left%]", + "swipe_right": "[%key:component::nanoleaf::entity::event::touch::state_attributes::event_type::state::swipe_right%]" + } + }, + "entity": { + "event": { + "touch": { + "name": "Touch gesture", + "state_attributes": { + "event_type": { + "state": { + "swipe_up": "Swipe up", + "swipe_down": "Swipe down", + "swipe_left": "Swipe left", + "swipe_right": "Swipe right" + } + } + } + } } } }