Coverage for custom_components/supernotify/transports/notify_entity.py: 59%
37 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-02-06 15:56 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-02-06 15:56 +0000
1import logging
2from typing import Any
4from homeassistant.const import ATTR_ENTITY_ID # ATTR_VARIABLES from script.const has import issues
5from homeassistant.exceptions import ServiceValidationError
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.envelope import Envelope
17from custom_components.supernotify.hass_api import HomeAssistantAPI
18from custom_components.supernotify.model import (
19 DebugTrace,
20 DeliveryConfig,
21 MessageOnlyPolicy,
22 TransportConfig,
23 TransportFeature,
24)
25from custom_components.supernotify.schema import SelectionRank
26from custom_components.supernotify.transport import (
27 Transport,
28)
30_LOGGER = logging.getLogger(__name__)
32RE_NOTIFY_ENTITY = r"notify\.[A-Za-z0-9_]+"
33FIXED_ACTION = "notify.send_message"
36class NotifyEntityTransport(Transport):
37 """Call any notify entity"""
39 name = TRANSPORT_NOTIFY_ENTITY
41 def __init__(self, *args: Any, **kwargs: Any) -> None:
42 super().__init__(*args, **kwargs)
44 @property
45 def supported_features(self) -> TransportFeature:
46 return TransportFeature.MESSAGE | TransportFeature.TITLE
48 @property
49 def default_config(self) -> TransportConfig:
50 config = TransportConfig()
51 config.delivery_defaults.action = FIXED_ACTION
52 config.delivery_defaults.selection_rank = SelectionRank.LAST
53 config.delivery_defaults.options = {
54 OPTION_SIMPLIFY_TEXT: False,
55 OPTION_STRIP_URLS: False,
56 OPTION_MESSAGE_USAGE: MessageOnlyPolicy.STANDARD,
57 OPTION_UNIQUE_TARGETS: True,
58 OPTION_TARGET_CATEGORIES: [ATTR_ENTITY_ID],
59 OPTION_TARGET_SELECT: [RE_NOTIFY_ENTITY],
60 }
61 return config
63 def auto_configure(self, hass_api: HomeAssistantAPI) -> DeliveryConfig | None: # noqa: ARG002
64 return self.delivery_defaults
66 async def deliver(self, envelope: Envelope, debug_trace: DebugTrace | None = None) -> bool: # noqa: ARG002
67 action_data = envelope.core_action_data()
68 targets = envelope.target.entity_ids or []
69 if not targets:
70 raise ServiceValidationError("No targets for notify entity transport")
71 target_data: dict[str, Any] = {ATTR_ENTITY_ID: targets}
72 # area_id
73 # device_id
74 # label_id
75 action_data = envelope.core_action_data()
77 return await self.call_action(envelope, FIXED_ACTION, action_data=action_data, target_data=target_data)