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

24 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-08 17:42 +0000

1"""Describe SuperNotify's logbook events, so each notification shows against what caused it""" 

2 

3from collections.abc import Callable 

4from typing import Any 

5 

6from homeassistant.components.logbook import ( 

7 LOGBOOK_ENTRY_ICON, 

8 LOGBOOK_ENTRY_MESSAGE, 

9 LOGBOOK_ENTRY_NAME, 

10 LazyEventPartialState, 

11) 

12from homeassistant.core import HomeAssistant, callback 

13from homeassistant.helpers.translation import async_get_cached_translations 

14 

15from . import DOMAIN 

16from .const import EVENT_NOTIFICATION 

17from .schema import DeliveryOutcome 

18 

19 

20@callback 

21def async_describe_events( 

22 hass: HomeAssistant, 

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

24) -> None: 

25 """Describe logbook events.""" 

26 

27 def text(key: str, **placeholders: str) -> str: 

28 # translations were cached when the integration was set up, in the configured language 

29 full_key: str = f"component.{DOMAIN}.common.{key}" 

30 template: str | None = async_get_cached_translations(hass, hass.config.language, "common", DOMAIN).get( 

31 full_key 

32 ) or async_get_cached_translations(hass, "en", "common", DOMAIN).get(full_key) 

33 return template.format(**placeholders) if template else key 

34 

35 @callback 

36 def async_describe_notification(event: LazyEventPartialState) -> dict[str, Any]: 

37 data = event.data 

38 outcome: str = data.get("outcome", "") 

39 key: str = f"logbook_{outcome}" if outcome in DeliveryOutcome else "logbook_notified" 

40 message: str = text(key, summary=data.get("summary", "")) 

41 if data.get("deliveries"): 

42 message = text("logbook_via", message=message, deliveries=", ".join(data["deliveries"])) 

43 return {LOGBOOK_ENTRY_NAME: "Supernotify", LOGBOOK_ENTRY_MESSAGE: message, LOGBOOK_ENTRY_ICON: "mdi:bell-ring-outline"} 

44 

45 async_describe_event(DOMAIN, EVENT_NOTIFICATION, async_describe_notification)