diff --git a/homeassistant/components/nanoleaf/const.py b/homeassistant/components/nanoleaf/const.py index 7e246209733a..31265611dd3f 100644 --- a/homeassistant/components/nanoleaf/const.py +++ b/homeassistant/components/nanoleaf/const.py @@ -7,6 +7,8 @@ NANOLEAF_EVENT = f"{DOMAIN}_event" TOUCH_MODELS = {"NL29", "NL42", "NL52"} TOUCH_GESTURE_TRIGGER_MAP = { + 0: "single_tap", + 1: "double_tap", 2: "swipe_up", 3: "swipe_down", 4: "swipe_left", diff --git a/homeassistant/components/nanoleaf/event.py b/homeassistant/components/nanoleaf/event.py index 40e1bdaa0281..2acfe20d627d 100644 --- a/homeassistant/components/nanoleaf/event.py +++ b/homeassistant/components/nanoleaf/event.py @@ -27,6 +27,8 @@ class NanoleafGestureEvent(NanoleafEntity, EventEntity): """Representation of a Nanoleaf event entity.""" _attr_event_types = [ + "single_tap", + "double_tap", "swipe_up", "swipe_down", "swipe_left", diff --git a/homeassistant/components/nanoleaf/strings.json b/homeassistant/components/nanoleaf/strings.json index 9744bf0dff32..e280a1a70521 100644 --- a/homeassistant/components/nanoleaf/strings.json +++ b/homeassistant/components/nanoleaf/strings.json @@ -29,6 +29,8 @@ }, "device_automation": { "trigger_type": { + "double_tap": "[%key:component::nanoleaf::entity::event::touch::state_attributes::event_type::state::double_tap%]", + "single_tap": "[%key:component::nanoleaf::entity::event::touch::state_attributes::event_type::state::single_tap%]", "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%]", @@ -42,6 +44,8 @@ "state_attributes": { "event_type": { "state": { + "double_tap": "Double tap", + "single_tap": "Single tap", "swipe_down": "Swipe down", "swipe_left": "Swipe left", "swipe_right": "Swipe right", diff --git a/tests/components/nanoleaf/test_event.py b/tests/components/nanoleaf/test_event.py new file mode 100644 index 000000000000..77e21017e471 --- /dev/null +++ b/tests/components/nanoleaf/test_event.py @@ -0,0 +1,74 @@ +"""Tests for the Nanoleaf event platform.""" + +from unittest.mock import AsyncMock + +from aionanoleaf2 import TouchEvent +import pytest + +from homeassistant.const import STATE_UNKNOWN +from homeassistant.core import HomeAssistant + +from . import setup_integration + +from tests.common import MockConfigEntry + + +@pytest.mark.parametrize( + ("gesture_id", "gesture", "panel_id"), + [ + pytest.param(0, "single_tap", 7, id="single_tap"), + pytest.param(1, "double_tap", 7, id="double_tap"), + pytest.param(2, "swipe_up", -1, id="swipe_up"), + pytest.param(3, "swipe_down", -1, id="swipe_down"), + pytest.param(4, "swipe_left", -1, id="swipe_left"), + pytest.param(5, "swipe_right", -1, id="swipe_right"), + ], +) +async def test_touch_events( + hass: HomeAssistant, + mock_nanoleaf: AsyncMock, + mock_config_entry: MockConfigEntry, + caplog: pytest.LogCaptureFixture, + gesture_id: int, + gesture: str, + panel_id: int, +) -> None: + """Test documented gestures update the entity state.""" + mock_nanoleaf.model = "NL42" + await setup_integration(hass, mock_config_entry) + + touch_callback = mock_nanoleaf.listen_events.call_args.kwargs["touch_callback"] + await touch_callback(TouchEvent({"gesture": gesture_id, "panelId": panel_id})) + await hass.async_block_till_done() + + state = hass.states.get("event.nanoleaf_touch_gesture") + assert state is not None + assert state.state != STATE_UNKNOWN + assert state.attributes["event_type"] == gesture + assert "Received unknown touch gesture" not in caplog.text + + +async def test_unknown_touch_event( + hass: HomeAssistant, + mock_nanoleaf: AsyncMock, + mock_config_entry: MockConfigEntry, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test an undocumented gesture preserves the last event and is logged.""" + mock_nanoleaf.model = "NL42" + await setup_integration(hass, mock_config_entry) + + touch_callback = mock_nanoleaf.listen_events.call_args.kwargs["touch_callback"] + await touch_callback(TouchEvent({"gesture": 2, "panelId": -1})) + await hass.async_block_till_done() + + previous_state = hass.states.get("event.nanoleaf_touch_gesture") + assert previous_state is not None + assert previous_state.state != STATE_UNKNOWN + assert previous_state.attributes["event_type"] == "swipe_up" + + await touch_callback(TouchEvent({"gesture": 6, "panelId": 7})) + await hass.async_block_till_done() + + assert hass.states.get("event.nanoleaf_touch_gesture") == previous_state + assert "Received unknown touch gesture ID 6" in caplog.text