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

1import json 

2import logging 

3from typing import Any 

4 

5from homeassistant.components.mqtt.const import ATTR_TOPIC 

6 

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) 

13 

14RE_VALID_PHONE = r"^(\+\d{1,3})?\s?\(?\d{1,4}\)?[\s.-]?\d{3}[\s.-]?\d{4}$" 

15 

16_LOGGER = logging.getLogger(__name__) 

17 

18 

19class MQTTTransport(Transport): 

20 name = TRANSPORT_MQTT 

21 

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

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

24 

25 @property 

26 def supported_features(self) -> TransportFeature: 

27 return TransportFeature.MESSAGE | TransportFeature.TITLE 

28 

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 

36 

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 

40 

41 def recipient_target(self, recipient: dict[str, Any]) -> Target | None: # noqa: ARG002 

42 return None 

43 

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) 

46 

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)