diff --git a/homeassistant/components/deconz/logbook.py b/homeassistant/components/deconz/logbook.py new file mode 100644 index 00000000000..016dae8d8a3 --- /dev/null +++ b/homeassistant/components/deconz/logbook.py @@ -0,0 +1,33 @@ +"""Describe deCONZ logbook events.""" + +from typing import Callable, Optional + +from homeassistant.const import ATTR_DEVICE_ID, CONF_EVENT +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.event import Event + +from .const import DOMAIN as DECONZ_DOMAIN +from .deconz_event import CONF_DECONZ_EVENT, DeconzEvent +from .device_trigger import _get_deconz_event_from_device_id + + +@callback +def async_describe_events( + hass: HomeAssistant, + async_describe_event: Callable[[str, str, Callable[[Event], dict]], None], +) -> None: + """Describe logbook events.""" + + @callback + def async_describe_deconz_event(event: Event) -> dict: + """Describe deCONZ logbook event.""" + deconz_event: Optional[DeconzEvent] = _get_deconz_event_from_device_id( + hass, event.data[ATTR_DEVICE_ID] + ) + + return { + "name": f"{deconz_event.device.name}", + "message": f"fired event '{event.data[CONF_EVENT]}'.", + } + + async_describe_event(DECONZ_DOMAIN, CONF_DECONZ_EVENT, async_describe_deconz_event) diff --git a/tests/components/deconz/test_logbook.py b/tests/components/deconz/test_logbook.py new file mode 100644 index 00000000000..ff314ab88f1 --- /dev/null +++ b/tests/components/deconz/test_logbook.py @@ -0,0 +1,58 @@ +"""The tests for deCONZ logbook.""" + +from copy import deepcopy + +from homeassistant.components import logbook +from homeassistant.components.deconz.deconz_event import CONF_DECONZ_EVENT +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry +from homeassistant.const import CONF_DEVICE_ID, CONF_EVENT, CONF_ID, CONF_UNIQUE_ID +from homeassistant.setup import async_setup_component + +from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration + +from tests.components.logbook.test_init import MockLazyEventPartialState + + +async def test_humanifying_deconz_event(hass): + """Test humanifying deCONZ event.""" + data = deepcopy(DECONZ_WEB_REQUEST) + data["sensors"] = { + "0": { + "id": "Switch 1 id", + "name": "Switch 1", + "type": "ZHASwitch", + "state": {"buttonevent": 1000}, + "config": {}, + "uniqueid": "00:00:00:00:00:00:00:01-00", + } + } + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) + event = gateway.events[0] + + hass.config.components.add("recorder") + assert await async_setup_component(hass, "logbook", {}) + entity_attr_cache = logbook.EntityAttributeCache(hass) + + event1 = list( + logbook.humanify( + hass, + [ + MockLazyEventPartialState( + CONF_DECONZ_EVENT, + { + CONF_DEVICE_ID: event.device_id, + CONF_EVENT: 2000, + CONF_ID: event.event_id, + CONF_UNIQUE_ID: event.serial, + }, + ), + ], + entity_attr_cache, + {}, + ) + )[0] + + assert event1["name"] == "Switch 1" + assert event1["domain"] == "deconz" + assert event1["message"] == "fired event '2000'."