Coverage for custom_components/supernotify/methods/alexa.py: 93%
28 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-10-18 09:29 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-10-18 09:29 +0000
1import logging
2import re
3from typing import Any
5from homeassistant.components.notify.const import ATTR_MESSAGE
6from homeassistant.const import ATTR_ENTITY_ID
8from custom_components.supernotify import METHOD_ALEXA
9from custom_components.supernotify.delivery_method import DeliveryMethod
10from custom_components.supernotify.envelope import Envelope
12_LOGGER = logging.getLogger(__name__)
13ACTION = "notify.send_message"
16class AlexaDeliveryMethod(DeliveryMethod):
17 """Notify via Home Assistant's built-in Alexa integration
19 options:
20 TITLE_ONLY: true
22 """
24 method = METHOD_ALEXA
25 DEFAULT_TITLE_ONLY = True
27 def __init__(self, *args: Any, **kwargs: Any) -> None:
28 kwargs["default_action"] = ACTION
29 super().__init__(*args, **kwargs)
31 def select_target(self, target: str) -> bool:
32 return (
33 re.fullmatch(r"notify\.[a-z0-9_]+\_(speak|announce)", target) is not None
34 or re.fullmatch(r"group\.[a-z0-9_]+", target) is not None
35 )
37 async def deliver(self, envelope: Envelope) -> bool:
38 _LOGGER.info("SUPERNOTIFY notify_alexa: %s", envelope.message)
40 targets = envelope.targets or []
42 if not targets:
43 _LOGGER.debug("SUPERNOTIFY skipping alexa, no targets")
44 return False
46 message = self.combined_message(envelope, default_title_only=self.DEFAULT_TITLE_ONLY)
48 action_data: dict[str, Any] = {ATTR_MESSAGE: message or ""}
49 target_data: dict[str, Any] = {ATTR_ENTITY_ID: targets}
51 return await self.call_action(envelope, action_data=action_data, target_data=target_data)