Add event entity to Nanoleaf (#120013)

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
Joost Lekkerkerker 2024-06-21 11:16:00 +02:00 committed by GitHub
parent 7af79ba013
commit d21908a0e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 87 additions and 5 deletions

View File

@ -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

View File

@ -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(

View File

@ -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()

View File

@ -1,5 +1,10 @@
{
"entity": {
"event": {
"touch": {
"default": "mdi:gesture"
}
},
"light": {
"light": {
"default": "mdi:triangle-outline"

View File

@ -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"
}
}
}
}
}
}
}