Coverage for custom_components/supernotify/transports/mqtt.py: 56%
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 json
2import logging
3from typing import Any
5from homeassistant.components.mqtt.const import ATTR_TOPIC
7from custom_components.supernotify import TRANSPORT_MQTT
8from custom_components.supernotify.envelope import Envelope
9from custom_components.supernotify.model import DebugTrace, Target, TargetRequired, TransportConfig, TransportFeature
10from custom_components.supernotify.transport import (
11 Transport,
12)
14RE_VALID_PHONE = r"^(\+\d{1,3})?\s?\(?\d{1,4}\)?[\s.-]?\d{3}[\s.-]?\d{4}$"
16_LOGGER = logging.getLogger(__name__)
19class MQTTTransport(Transport):
20 name = TRANSPORT_MQTT
22 def __init__(self, *args: Any, **kwargs: Any) -> None:
23 super().__init__(*args, **kwargs)
25 @property
26 def supported_features(self) -> TransportFeature:
27 return TransportFeature.MESSAGE | TransportFeature.TITLE
29 @property
30 def default_config(self) -> TransportConfig:
31 config = TransportConfig()
32 config.delivery_defaults.action = "mqtt.publish"
33 config.delivery_defaults.target_required = TargetRequired.NEVER
34 config.delivery_defaults.options = {}
35 return config
37 def validate_action(self, action: str | None) -> bool:
38 """Override in subclass if transport has fixed action or doesn't require one"""
39 return action is self.delivery_defaults.action
41 def recipient_target(self, recipient: dict[str, Any]) -> Target | None: # noqa: ARG002
42 return None
44 async def deliver(self, envelope: Envelope, debug_trace: DebugTrace | None = None) -> bool: # noqa: ARG002
45 _LOGGER.debug("SUPERNOTIFY notify_mqtt: %s", envelope.delivery_name)
47 if not envelope.data or ATTR_TOPIC not in envelope.data:
48 _LOGGER.warning("SUPERNOTIFY notify_mqtt: No topic for publication")
49 action_data: dict[str, Any] = envelope.data
50 if isinstance(action_data["payload"], dict):
51 action_data["payload"] = json.dumps(action_data["payload"])
52 return await self.call_action(envelope, action_data=action_data)