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

35 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-04-01 15:06 +0000

1import logging 

2from typing import TYPE_CHECKING, Any 

3 

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

5from homeassistant.exceptions import ServiceValidationError 

6 

7from custom_components.supernotify.const import ( 

8 OPTION_MESSAGE_USAGE, 

9 OPTION_SIMPLIFY_TEXT, 

10 OPTION_STRIP_URLS, 

11 OPTION_TARGET_CATEGORIES, 

12 OPTION_TARGET_SELECT, 

13 OPTION_UNIQUE_TARGETS, 

14 TRANSPORT_NOTIFY_ENTITY, 

15) 

16from custom_components.supernotify.model import ( 

17 DebugTrace, 

18 DeliveryConfig, 

19 MessageOnlyPolicy, 

20 TransportConfig, 

21 TransportFeature, 

22) 

23from custom_components.supernotify.schema import SelectionRank 

24from custom_components.supernotify.transport import ( 

25 Transport, 

26) 

27 

28if TYPE_CHECKING: 

29 from custom_components.supernotify.envelope import Envelope 

30 from custom_components.supernotify.hass_api import HomeAssistantAPI 

31 

32_LOGGER = logging.getLogger(__name__) 

33 

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

35FIXED_ACTION = "notify.send_message" 

36 

37 

38class NotifyEntityTransport(Transport): 

39 """Call any notify entity""" 

40 

41 name = TRANSPORT_NOTIFY_ENTITY 

42 

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

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

45 

46 @property 

47 def supported_features(self) -> TransportFeature: 

48 return TransportFeature.MESSAGE | TransportFeature.TITLE 

49 

50 @property 

51 def default_config(self) -> TransportConfig: 

52 config = TransportConfig() 

53 config.delivery_defaults.action = FIXED_ACTION 

54 config.delivery_defaults.selection_rank = SelectionRank.LAST 

55 config.delivery_defaults.options = { 

56 OPTION_SIMPLIFY_TEXT: False, 

57 OPTION_STRIP_URLS: False, 

58 OPTION_MESSAGE_USAGE: MessageOnlyPolicy.STANDARD, 

59 OPTION_UNIQUE_TARGETS: True, 

60 OPTION_TARGET_CATEGORIES: [ATTR_ENTITY_ID], 

61 OPTION_TARGET_SELECT: [RE_NOTIFY_ENTITY], 

62 } 

63 return config 

64 

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

66 return self.delivery_defaults 

67 

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

69 action_data = envelope.core_action_data() 

70 targets = envelope.target.entity_ids or [] 

71 if not targets: 

72 raise ServiceValidationError("No targets for notify entity transport") 

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

74 # area_id 

75 # device_id 

76 # label_id 

77 action_data = envelope.core_action_data() 

78 

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