Coverage for custom_components/supernotify/methods/alexa_devices.py: 94%
31 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-10-26 08:54 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-10-26 08:54 +0000
1import logging
2import re
3from typing import Any
5from homeassistant.components.notify.const import ATTR_MESSAGE
6from homeassistant.const import ATTR_ENTITY_ID, CONF_ACTION, CONF_DEFAULT
8from custom_components.supernotify import (
9 CONF_OPTIONS,
10 METHOD_ALEXA,
11 MessageOnlyPolicy,
12)
13from custom_components.supernotify.delivery_method import (
14 OPTION_MESSAGE_USAGE,
15 OPTION_SIMPLIFY_TEXT,
16 OPTION_STRIP_URLS,
17 DeliveryMethod,
18)
19from custom_components.supernotify.envelope import Envelope
21_LOGGER = logging.getLogger(__name__)
22ACTION = "notify.send_message"
25class AlexaDevicesDeliveryMethod(DeliveryMethod):
26 """Notify via Home Assistant's built-in Alexa Devices integration
28 options:
29 message_usage: standard | use_title | combine_title
31 """
33 method = METHOD_ALEXA
35 def __init__(self, *args: Any, **kwargs: Any) -> None:
36 kwargs.setdefault(CONF_DEFAULT, {})
37 kwargs[CONF_DEFAULT].setdefault(CONF_ACTION, ACTION)
38 kwargs[CONF_DEFAULT].setdefault(CONF_OPTIONS, {})
39 kwargs[CONF_DEFAULT][CONF_OPTIONS].setdefault(OPTION_SIMPLIFY_TEXT, True)
40 kwargs[CONF_DEFAULT][CONF_OPTIONS].setdefault(OPTION_STRIP_URLS, True)
41 kwargs[CONF_DEFAULT][CONF_OPTIONS].setdefault(OPTION_MESSAGE_USAGE, MessageOnlyPolicy.STANDARD)
42 super().__init__(*args, **kwargs)
44 def select_target(self, target: str) -> bool:
45 return (
46 re.fullmatch(r"notify\.[a-z0-9_]+\_(speak|announce)", target) is not None
47 or re.fullmatch(r"group\.[a-z0-9_]+", target) is not None
48 )
50 async def deliver(self, envelope: Envelope) -> bool:
51 _LOGGER.debug("SUPERNOTIFY notify_alexa_devices: %s", envelope.message)
53 targets = envelope.targets or []
55 if not targets:
56 _LOGGER.debug("SUPERNOTIFY skipping alexa devices, no targets")
57 return False
59 action_data: dict[str, Any] = {ATTR_MESSAGE: envelope.message or ""}
60 target_data: dict[str, Any] = {ATTR_ENTITY_ID: targets}
62 return await self.call_action(envelope, action_data=action_data, target_data=target_data)