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