Coverage for custom_components/autoarm/logbook.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-26 20:57 +0000

1"""Describe AutoArm's logbook events, so the changes it makes on its own show what caused them""" 

2 

3from collections.abc import Callable 

4from typing import Any 

5 

6from homeassistant.components.logbook import LazyEventPartialState # type: ignore[attr-defined] 

7from homeassistant.components.logbook.const import ( 

8 LOGBOOK_ENTRY_ENTITY_ID, 

9 LOGBOOK_ENTRY_MESSAGE, 

10 LOGBOOK_ENTRY_NAME, 

11 LOGBOOK_ENTRY_SOURCE, 

12) 

13from homeassistant.const import ATTR_ENTITY_ID 

14from homeassistant.core import HomeAssistant, callback 

15 

16from .const import DOMAIN, EVENT_TRIGGERED, ChangeSource 

17 

18 

19@callback 

20def async_describe_events( 

21 hass: HomeAssistant, 

22 async_describe_event: Callable[[str, str, Callable[[LazyEventPartialState], dict[str, Any]]], None], 

23) -> None: 

24 """Describe logbook events.""" 

25 

26 @callback 

27 def async_describe_triggered(event: LazyEventPartialState) -> dict[str, Any]: 

28 data = event.data 

29 cause: str = data.get("source", "unknown") 

30 if data.get("alarm_state"): 

31 # following the alarm state, as for the Frigate profile 

32 cause = f"alarm {data['alarm_state']}" 

33 if data.get("summary"): 

34 kind = "times of the day sensor" if cause == ChangeSource.TOD else "calendar event" 

35 cause = f"{kind} {data['summary']}" 

36 if data.get("deferred"): 

37 cause = f"deferred from {cause}" 

38 return { 

39 # the cause is in the name, as it's all the logbook shows of this event against the panel's change 

40 LOGBOOK_ENTRY_NAME: f"Auto Arm ({cause})", 

41 LOGBOOK_ENTRY_MESSAGE: f"triggered by {cause}", 

42 LOGBOOK_ENTRY_SOURCE: cause, 

43 LOGBOOK_ENTRY_ENTITY_ID: data.get(ATTR_ENTITY_ID), 

44 } 

45 

46 async_describe_event(DOMAIN, EVENT_TRIGGERED, async_describe_triggered)