Basic logbook support for deCONZ events (#45400)
* Basic logbook support * Improve formulation of message * Improve typing
This commit is contained in:
parent
6813454821
commit
aaf4cd4a25
2 changed files with 91 additions and 0 deletions
33
homeassistant/components/deconz/logbook.py
Normal file
33
homeassistant/components/deconz/logbook.py
Normal file
|
@ -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)
|
58
tests/components/deconz/test_logbook.py
Normal file
58
tests/components/deconz/test_logbook.py
Normal file
|
@ -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'."
|
Loading…
Add table
Reference in a new issue