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

33 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-25 14:29 +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, TargetRequired, 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 = TransportConfig() 

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

36 config.delivery_defaults.target_required = TargetRequired.NEVER 

37 config.delivery_defaults.inclusion = self.inclusion_mode 

38 return config 

39 

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

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

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

43 return True 

44 

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

46 return {self.name: {}} 

47 

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

49 data = envelope.data or {} 

50 

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

52 action_data = envelope.core_action_data() 

53 if notification_id is not None: 

54 action_data["notification_id"] = notification_id 

55 

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