Describe hue events in the logbook (#72220)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>pull/72224/head
parent
ba4031718d
commit
7cad1571a2
|
@ -0,0 +1,73 @@
|
||||||
|
"""Describe hue logbook events."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
|
||||||
|
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",
|
||||||
|
}
|
||||||
|
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",
|
||||||
|
"repeat": "{subtype} held down",
|
||||||
|
"short_release": "{subtype} released after short press",
|
||||||
|
"long_release": "{subtype} released after long press",
|
||||||
|
"double_short_release": "both {subtype} released",
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
"name": name,
|
||||||
|
"message": str(message),
|
||||||
|
}
|
||||||
|
|
||||||
|
async_describe_event(DOMAIN, ATTR_HUE_EVENT, async_describe_hue_event)
|
|
@ -0,0 +1,104 @@
|
||||||
|
"""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.helpers import device_registry
|
||||||
|
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, mock_bridge_v2):
|
||||||
|
"""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]
|
||||||
|
|
||||||
|
dev_reg = device_registry.async_get(hass)
|
||||||
|
v1_device = dev_reg.async_get_or_create(
|
||||||
|
identifiers={(DOMAIN, "v1")}, name="Remote 1", config_entry_id=entry.entry_id
|
||||||
|
)
|
||||||
|
v2_device = dev_reg.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, mock_bridge_v2):
|
||||||
|
"""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"
|
Loading…
Reference in New Issue