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

35 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-06-11 22:18 +0000

1from __future__ import annotations 

2 

3import logging 

4from typing import TYPE_CHECKING, Any 

5 

6from homeassistant.const import ATTR_ENTITY_ID # ATTR_VARIABLES from script.const has import issues 

7 

8from custom_components.supernotify.const import ( 

9 OPTION_MESSAGE_USAGE, 

10 OPTION_SIMPLIFY_TEXT, 

11 OPTION_STRIP_URLS, 

12 OPTION_TARGET_CATEGORIES, 

13 OPTION_TARGET_SELECT, 

14 OPTION_UNIQUE_TARGETS, 

15 TRANSPORT_NOTIFY_ENTITY, 

16) 

17from custom_components.supernotify.model import ( 

18 DebugTrace, 

19 DeliveryConfig, 

20 MessageOnlyPolicy, 

21 TransportConfig, 

22 TransportFeature, 

23) 

24from custom_components.supernotify.schema import SelectionRank 

25from custom_components.supernotify.transport import ( 

26 Transport, 

27) 

28 

29if TYPE_CHECKING: 

30 from custom_components.supernotify.envelope import Envelope 

31 from custom_components.supernotify.hass_api import HomeAssistantAPI 

32 

33_LOGGER = logging.getLogger(__name__) 

34 

35RE_NOTIFY_ENTITY = r"notify\.[A-Za-z0-9_]+" 

36FIXED_ACTION = "notify.send_message" 

37 

38 

39class NotifyEntityTransport(Transport): 

40 """Call any notify entity""" 

41 

42 name = TRANSPORT_NOTIFY_ENTITY 

43 

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

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

46 

47 @property 

48 def supported_features(self) -> TransportFeature: 

49 return TransportFeature.MESSAGE | TransportFeature.TITLE 

50 

51 @property 

52 def default_config(self) -> TransportConfig: 

53 config = TransportConfig() 

54 config.delivery_defaults.action = FIXED_ACTION 

55 config.delivery_defaults.selection_rank = SelectionRank.LAST 

56 config.delivery_defaults.options = { 

57 OPTION_SIMPLIFY_TEXT: False, 

58 OPTION_STRIP_URLS: False, 

59 OPTION_MESSAGE_USAGE: MessageOnlyPolicy.STANDARD, 

60 OPTION_UNIQUE_TARGETS: True, 

61 OPTION_TARGET_CATEGORIES: [ATTR_ENTITY_ID], 

62 OPTION_TARGET_SELECT: [RE_NOTIFY_ENTITY], 

63 } 

64 return config 

65 

66 def auto_configure(self, hass_api: HomeAssistantAPI) -> DeliveryConfig | None: # noqa: ARG002 

67 return self.delivery_defaults 

68 

69 async def deliver(self, envelope: Envelope, debug_trace: DebugTrace | None = None) -> bool: # noqa: ARG002 

70 targets = envelope.target.entity_ids or [] 

71 if not targets: 

72 _LOGGER.warning("SUPERNOTIFY notify_entity: no targets") 

73 return False 

74 target_data: dict[str, Any] = {ATTR_ENTITY_ID: targets} 

75 # area_id 

76 # device_id 

77 # label_id 

78 action_data = envelope.core_action_data() 

79 

80 return await self.call_action(envelope, FIXED_ACTION, action_data=action_data, target_data=target_data)