Coverage for custom_components/supernotify/methods/sms.py: 81%
31 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-28 14:21 +0000
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-28 14:21 +0000
1import logging
2import re
3from typing import Any
5from homeassistant.components.notify.const import ATTR_DATA, ATTR_TARGET
7from custom_components.supernotify import CONF_PHONE_NUMBER, METHOD_SMS
8from custom_components.supernotify.delivery_method import DeliveryMethod
9from custom_components.supernotify.envelope import Envelope
11RE_VALID_PHONE = r"^(\+\d{1,3})?\s?\(?\d{1,4}\)?[\s.-]?\d{3}[\s.-]?\d{4}$"
13_LOGGER = logging.getLogger(__name__)
16class SMSDeliveryMethod(DeliveryMethod):
17 method = METHOD_SMS
18 DEFAULT_TITLE_ONLY = False
20 def __init__(self, *args, **kwargs) -> None:
21 super().__init__(*args, **kwargs)
23 def select_target(self, target: str) -> bool:
24 return re.fullmatch(RE_VALID_PHONE, target) is not None
26 def recipient_target(self, recipient: dict) -> list[str]:
27 phone = recipient.get(CONF_PHONE_NUMBER)
28 return [phone] if phone else []
30 async def deliver(self, envelope: Envelope) -> bool:
31 _LOGGER.debug("SUPERNOTIFY notify_sms: %s", envelope.delivery_name)
33 data: dict[str, Any] = envelope.data or {}
34 mobile_numbers = envelope.targets or []
36 message: str | None = self.combined_message(envelope, default_title_only=self.DEFAULT_TITLE_ONLY)
37 if not message:
38 _LOGGER.warning("SUPERNOTIFY notify_sms: No message to send")
39 return False
41 action_data = {"message": message[:158], ATTR_TARGET: mobile_numbers}
42 if data and data.get("data"):
43 action_data[ATTR_DATA] = data.get("data", {})
45 return await self.call_action(envelope, action_data=action_data)