Coverage for custom_components/supernotify/transports/persistent.py: 100%

31 statements  

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

1from __future__ import annotations 

2 

3import logging 

4from typing import TYPE_CHECKING, Any 

5 

6from homeassistant.helpers.typing import ConfigType 

7 

8from custom_components.supernotify.const import ( 

9 ATTR_NOTIFICATION_ID, 

10 TRANSPORT_PERSISTENT, 

11) 

12from custom_components.supernotify.model import DebugTrace, TransportConfig, TransportFeature 

13from custom_components.supernotify.transport import Transport 

14 

15if TYPE_CHECKING: 

16 from custom_components.supernotify.envelope import Envelope 

17 from custom_components.supernotify.hass_api import HomeAssistantAPI 

18 

19_LOGGER = logging.getLogger(__name__) 

20 

21 

22class PersistentTransport(Transport): 

23 name = TRANSPORT_PERSISTENT 

24 

25 def __init__(self, *args: Any, **kwargs: Any) -> None: 

26 super().__init__(*args, **kwargs) 

27 

28 @property 

29 def supported_features(self) -> TransportFeature: 

30 return TransportFeature.MESSAGE | TransportFeature.TITLE 

31 

32 @property 

33 def default_config(self) -> TransportConfig: 

34 config = super().default_config 

35 config.delivery_defaults.action = "persistent_notification.create" 

36 return config 

37 

38 def is_viable(self, hass_api: HomeAssistantAPI) -> bool: 

39 # persistent_notification is always available in HA core, no integration to discover - 

40 # but a UI popup on every single notification would be intrusive, so require opt-in 

41 return True 

42 

43 def build_standard_deliveries(self, hass_api: HomeAssistantAPI) -> dict[str, ConfigType]: 

44 return {self.name: {}} 

45 

46 async def deliver(self, envelope: Envelope, debug_trace: DebugTrace | None = None) -> bool: 

47 data = envelope.data or {} 

48 

49 notification_id = data.get(ATTR_NOTIFICATION_ID) or envelope.delivery.data.get(ATTR_NOTIFICATION_ID) 

50 action_data = envelope.core_action_data() 

51 if notification_id is not None: 

52 action_data["notification_id"] = notification_id 

53 

54 return await self.call_action(envelope, action_data=action_data)