Coverage for custom_components/supernotify/transports/mqtt.py: 100%
35 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-04-01 15:06 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-04-01 15:06 +0000
1import json
2import logging
3from typing import TYPE_CHECKING, Any
5from homeassistant.components.mqtt.const import ATTR_TOPIC
7from custom_components.supernotify.const import TRANSPORT_MQTT
8from custom_components.supernotify.model import DebugTrace, Target, TargetRequired, TransportConfig, TransportFeature
9from custom_components.supernotify.transport import (
10 Transport,
11)
13if TYPE_CHECKING:
14 from custom_components.supernotify.envelope import Envelope
16RE_VALID_PHONE = r"^(\+\d{1,3})?\s?\(?\d{1,4}\)?[\s.-]?\d{3}[\s.-]?\d{4}$"
18_LOGGER = logging.getLogger(__name__)
21class MQTTTransport(Transport):
22 name = TRANSPORT_MQTT
24 def __init__(self, *args: Any, **kwargs: Any) -> None:
25 super().__init__(*args, **kwargs)
27 @property
28 def supported_features(self) -> TransportFeature:
29 return TransportFeature.MESSAGE | TransportFeature.TITLE
31 @property
32 def default_config(self) -> TransportConfig:
33 config = TransportConfig()
34 config.delivery_defaults.action = "mqtt.publish"
35 config.delivery_defaults.target_required = TargetRequired.NEVER
36 config.delivery_defaults.options = {}
37 return config
39 def validate_action(self, action: str | None) -> bool:
40 """Override in subclass if transport has fixed action or doesn't require one"""
41 return action is self.delivery_defaults.action
43 def recipient_target(self, recipient: dict[str, Any]) -> Target | None: # noqa: ARG002
44 return None
46 async def deliver(self, envelope: Envelope, debug_trace: DebugTrace | None = None) -> bool: # noqa: ARG002
47 _LOGGER.debug("SUPERNOTIFY notify_mqtt: %s", envelope.delivery_name)
49 if not envelope.data or ATTR_TOPIC not in envelope.data:
50 _LOGGER.warning("SUPERNOTIFY notify_mqtt: No topic for publication")
51 action_data: dict[str, Any] = envelope.data
52 if isinstance(action_data["payload"], dict):
53 action_data["payload"] = json.dumps(action_data["payload"])
54 return await self.call_action(envelope, action_data=action_data)