Hue event entity follow up (#97336)

This commit is contained in:
Marcel van der Veldt 2023-07-27 16:58:09 +02:00 committed by GitHub
parent cc47ff30b3
commit 7ada88eab3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 6 additions and 188 deletions

View File

@ -43,11 +43,11 @@ REQUEST_REFRESH_DELAY = 0.3
# V2 API SPECIFIC CONSTANTS ##################
DEFAULT_BUTTON_EVENT_TYPES = (
# I have never ever seen the `DOUBLE_SHORT_RELEASE`
# or `DOUBLE_SHORT_RELEASE` events so leave them out here
# I have never ever seen the `DOUBLE_SHORT_RELEASE` event so leave it out here
ButtonEvent.INITIAL_PRESS,
ButtonEvent.REPEAT,
ButtonEvent.SHORT_RELEASE,
ButtonEvent.LONG_PRESS,
ButtonEvent.LONG_RELEASE,
)

View File

@ -1,78 +0,0 @@
"""Describe hue logbook events."""
from __future__ import annotations
from collections.abc import Callable
from homeassistant.components.logbook import LOGBOOK_ENTRY_MESSAGE, LOGBOOK_ENTRY_NAME
from homeassistant.const import CONF_DEVICE_ID, CONF_EVENT, CONF_ID, CONF_TYPE
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.helpers import device_registry as dr
from .const import ATTR_HUE_EVENT, CONF_SUBTYPE, DOMAIN
TRIGGER_SUBTYPE = {
"button_1": "first button",
"button_2": "second button",
"button_3": "third button",
"button_4": "fourth button",
"double_buttons_1_3": "first and third buttons",
"double_buttons_2_4": "second and fourth buttons",
"dim_down": "dim down",
"dim_up": "dim up",
"turn_off": "turn off",
"turn_on": "turn on",
"1": "first button",
"2": "second button",
"3": "third button",
"4": "fourth button",
"clock_wise": "Rotation clockwise",
"counter_clock_wise": "Rotation counter-clockwise",
}
TRIGGER_TYPE = {
"remote_button_long_release": "{subtype} released after long press",
"remote_button_short_press": "{subtype} pressed",
"remote_button_short_release": "{subtype} released",
"remote_double_button_long_press": "both {subtype} released after long press",
"remote_double_button_short_press": "both {subtype} released",
"initial_press": "{subtype} pressed initially",
"long_press": "{subtype} long press",
"repeat": "{subtype} held down",
"short_release": "{subtype} released after short press",
"long_release": "{subtype} released after long press",
"double_short_release": "both {subtype} released",
"start": '"{subtype}" pressed initially',
}
UNKNOWN_TYPE = "unknown type"
UNKNOWN_SUB_TYPE = "unknown sub type"
@callback
def async_describe_events(
hass: HomeAssistant,
async_describe_event: Callable[[str, str, Callable[[Event], dict[str, str]]], None],
) -> None:
"""Describe hue logbook events."""
@callback
def async_describe_hue_event(event: Event) -> dict[str, str]:
"""Describe hue logbook event."""
data = event.data
name: str | None = None
if dev_ent := dr.async_get(hass).async_get(data[CONF_DEVICE_ID]):
name = dev_ent.name
if name is None:
name = data[CONF_ID]
if CONF_TYPE in data: # v2
subtype = TRIGGER_SUBTYPE.get(str(data[CONF_SUBTYPE]), UNKNOWN_SUB_TYPE)
message = TRIGGER_TYPE.get(data[CONF_TYPE], UNKNOWN_TYPE).format(
subtype=subtype
)
else:
message = f"Event {data[CONF_EVENT]}" # v1
return {
LOGBOOK_ENTRY_NAME: name,
LOGBOOK_ENTRY_MESSAGE: message,
}
async_describe_event(DOMAIN, ATTR_HUE_EVENT, async_describe_hue_event)

View File

@ -76,7 +76,8 @@
"initial_press": "Initial press",
"repeat": "Repeat",
"short_release": "Short press",
"long_release": "Long press",
"long_press": "Long press",
"long_release": "Long release",
"double_short_release": "Double press"
}
}

View File

@ -94,6 +94,7 @@ async def test_get_triggers(
ButtonEvent.INITIAL_PRESS,
ButtonEvent.LONG_RELEASE,
ButtonEvent.REPEAT,
ButtonEvent.LONG_PRESS,
ButtonEvent.SHORT_RELEASE,
)
for control_id, resource_id in (

View File

@ -28,6 +28,7 @@ async def test_event(
"initial_press",
"repeat",
"short_release",
"long_press",
"long_release",
]
# trigger firing 'initial_press' event from the device

View File

@ -1,107 +0,0 @@
"""The tests for hue logbook."""
from homeassistant.components.hue.const import ATTR_HUE_EVENT, CONF_SUBTYPE, DOMAIN
from homeassistant.components.hue.v1.hue_event import CONF_LAST_UPDATED
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_DEVICE_ID,
CONF_EVENT,
CONF_ID,
CONF_TYPE,
CONF_UNIQUE_ID,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.setup import async_setup_component
from .conftest import setup_platform
from tests.components.logbook.common import MockRow, mock_humanify
# v1 event
SAMPLE_V1_EVENT = {
CONF_DEVICE_ID: "fe346f17a9f8c15be633f9cc3f3d6631",
CONF_EVENT: 18,
CONF_ID: "hue_tap",
CONF_LAST_UPDATED: "2019-12-28T22:58:03",
CONF_UNIQUE_ID: "00:00:00:00:00:44:23:08-f2",
}
# v2 event
SAMPLE_V2_EVENT = {
CONF_DEVICE_ID: "f974028e7933aea703a2199a855bc4a3",
CONF_ID: "wall_switch_with_2_controls_button",
CONF_SUBTYPE: 1,
CONF_TYPE: "initial_press",
CONF_UNIQUE_ID: "c658d3d8-a013-4b81-8ac6-78b248537e70",
}
async def test_humanify_hue_events(
hass: HomeAssistant, mock_bridge_v2, device_registry: dr.DeviceRegistry
) -> None:
"""Test hue events when the devices are present in the registry."""
await setup_platform(hass, mock_bridge_v2, "sensor")
hass.config.components.add("recorder")
assert await async_setup_component(hass, "logbook", {})
await hass.async_block_till_done()
entry: ConfigEntry = hass.config_entries.async_entries(DOMAIN)[0]
v1_device = device_registry.async_get_or_create(
identifiers={(DOMAIN, "v1")}, name="Remote 1", config_entry_id=entry.entry_id
)
v2_device = device_registry.async_get_or_create(
identifiers={(DOMAIN, "v2")}, name="Remote 2", config_entry_id=entry.entry_id
)
(v1_event, v2_event) = mock_humanify(
hass,
[
MockRow(
ATTR_HUE_EVENT,
{**SAMPLE_V1_EVENT, CONF_DEVICE_ID: v1_device.id},
),
MockRow(
ATTR_HUE_EVENT,
{**SAMPLE_V2_EVENT, CONF_DEVICE_ID: v2_device.id},
),
],
)
assert v1_event["name"] == "Remote 1"
assert v1_event["domain"] == DOMAIN
assert v1_event["message"] == "Event 18"
assert v2_event["name"] == "Remote 2"
assert v2_event["domain"] == DOMAIN
assert v2_event["message"] == "first button pressed initially"
async def test_humanify_hue_events_devices_removed(
hass: HomeAssistant, mock_bridge_v2
) -> None:
"""Test hue events when the devices have been removed from the registry."""
await setup_platform(hass, mock_bridge_v2, "sensor")
hass.config.components.add("recorder")
assert await async_setup_component(hass, "logbook", {})
await hass.async_block_till_done()
(v1_event, v2_event) = mock_humanify(
hass,
[
MockRow(
ATTR_HUE_EVENT,
SAMPLE_V1_EVENT,
),
MockRow(
ATTR_HUE_EVENT,
SAMPLE_V2_EVENT,
),
],
)
assert v1_event["name"] == "hue_tap"
assert v1_event["domain"] == DOMAIN
assert v1_event["message"] == "Event 18"
assert v2_event["name"] == "wall_switch_with_2_controls_button"
assert v2_event["domain"] == DOMAIN
assert v2_event["message"] == "first button pressed initially"