mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Add Nanoleaf Swipe Device Trigger (#66195)
This commit is contained in:
parent
a08165a8d7
commit
a54e3ca1f8
@ -762,6 +762,7 @@ omit =
|
|||||||
homeassistant/components/nad/media_player.py
|
homeassistant/components/nad/media_player.py
|
||||||
homeassistant/components/nanoleaf/__init__.py
|
homeassistant/components/nanoleaf/__init__.py
|
||||||
homeassistant/components/nanoleaf/button.py
|
homeassistant/components/nanoleaf/button.py
|
||||||
|
homeassistant/components/nanoleaf/device_trigger.py
|
||||||
homeassistant/components/nanoleaf/diagnostics.py
|
homeassistant/components/nanoleaf/diagnostics.py
|
||||||
homeassistant/components/nanoleaf/entity.py
|
homeassistant/components/nanoleaf/entity.py
|
||||||
homeassistant/components/nanoleaf/light.py
|
homeassistant/components/nanoleaf/light.py
|
||||||
|
@ -6,16 +6,32 @@ from dataclasses import dataclass
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
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.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.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
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.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
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]
|
PLATFORMS = [Platform.BUTTON, Platform.LIGHT]
|
||||||
|
|
||||||
@ -46,7 +62,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
coordinator = DataUpdateCoordinator(
|
coordinator = DataUpdateCoordinator(
|
||||||
hass,
|
hass,
|
||||||
logging.getLogger(__name__),
|
_LOGGER,
|
||||||
name=entry.title,
|
name=entry.title,
|
||||||
update_interval=timedelta(minutes=1),
|
update_interval=timedelta(minutes=1),
|
||||||
update_method=async_get_state,
|
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()
|
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."""
|
"""Receive state and effect event."""
|
||||||
coordinator.async_set_updated_data(None)
|
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(
|
event_listener = asyncio.create_task(
|
||||||
nanoleaf.listen_events(
|
nanoleaf.listen_events(
|
||||||
state_callback=update_light_state_callback,
|
state_callback=light_event_callback,
|
||||||
effects_callback=update_light_state_callback,
|
effects_callback=light_event_callback,
|
||||||
|
touch_callback=touch_event_callback if supports_touch else None,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,3 +1,14 @@
|
|||||||
"""Constants for Nanoleaf integration."""
|
"""Constants for Nanoleaf integration."""
|
||||||
|
|
||||||
DOMAIN = "nanoleaf"
|
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",
|
||||||
|
}
|
||||||
|
79
homeassistant/components/nanoleaf/device_trigger.py
Normal file
79
homeassistant/components/nanoleaf/device_trigger.py
Normal file
@ -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"
|
||||||
|
)
|
@ -24,5 +24,13 @@
|
|||||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
|
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
|
||||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
"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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,5 +24,13 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"device_automation": {
|
||||||
|
"trigger_type": {
|
||||||
|
"swipe_down": "Swipe Down",
|
||||||
|
"swipe_left": "Swipe Left",
|
||||||
|
"swipe_right": "Swipe Right",
|
||||||
|
"swipe_up": "Swipe Up"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user