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

1import logging 

2import re 

3from typing import Any 

4 

5from homeassistant.components.notify.const import ATTR_MESSAGE 

6from homeassistant.const import ATTR_ENTITY_ID, CONF_ACTION, CONF_DEFAULT 

7 

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 

20 

21_LOGGER = logging.getLogger(__name__) 

22ACTION = "notify.send_message" 

23 

24 

25class AlexaDevicesDeliveryMethod(DeliveryMethod): 

26 """Notify via Home Assistant's built-in Alexa Devices integration 

27 

28 options: 

29 message_usage: standard | use_title | combine_title 

30 

31 """ 

32 

33 method = METHOD_ALEXA 

34 

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) 

43 

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 ) 

49 

50 async def deliver(self, envelope: Envelope) -> bool: 

51 _LOGGER.debug("SUPERNOTIFY notify_alexa_devices: %s", envelope.message) 

52 

53 targets = envelope.targets or [] 

54 

55 if not targets: 

56 _LOGGER.debug("SUPERNOTIFY skipping alexa devices, no targets") 

57 return False 

58 

59 action_data: dict[str, Any] = {ATTR_MESSAGE: envelope.message or ""} 

60 target_data: dict[str, Any] = {ATTR_ENTITY_ID: targets} 

61 

62 return await self.call_action(envelope, action_data=action_data, target_data=target_data)