Coverage for custom_components/supernotify/methods/alexa_media_player.py: 91%

33 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_DATA, ATTR_TARGET 

6from homeassistant.const import CONF_ACTION, CONF_DEFAULT 

7 

8from custom_components.supernotify import CONF_OPTIONS, METHOD_ALEXA_MEDIA_PLAYER, MessageOnlyPolicy 

9from custom_components.supernotify.delivery_method import ( 

10 OPTION_MESSAGE_USAGE, 

11 OPTION_SIMPLIFY_TEXT, 

12 OPTION_STRIP_URLS, 

13 DeliveryMethod, 

14) 

15from custom_components.supernotify.envelope import Envelope 

16 

17RE_VALID_ALEXA = r"media_player\.[A-Za-z0-9_]+" 

18ACTION = "notify.alexa_media" 

19 

20_LOGGER = logging.getLogger(__name__) 

21 

22 

23class AlexaMediaPlayerDeliveryMethod(DeliveryMethod): 

24 """Notify via Amazon Alexa announcements 

25 

26 options: 

27 message_usage: standard | use_title | combine_title 

28 

29 """ 

30 

31 method = METHOD_ALEXA_MEDIA_PLAYER 

32 

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

34 kwargs.setdefault(CONF_DEFAULT, {}) 

35 kwargs[CONF_DEFAULT].setdefault(CONF_ACTION, ACTION) 

36 kwargs[CONF_DEFAULT].setdefault(CONF_OPTIONS, {}) 

37 kwargs[CONF_DEFAULT][CONF_OPTIONS].setdefault(OPTION_SIMPLIFY_TEXT, True) 

38 kwargs[CONF_DEFAULT][CONF_OPTIONS].setdefault(OPTION_STRIP_URLS, True) 

39 kwargs[CONF_DEFAULT][CONF_OPTIONS].setdefault(OPTION_MESSAGE_USAGE, MessageOnlyPolicy.STANDARD) 

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

41 

42 def select_target(self, target: str) -> bool: 

43 return re.fullmatch(RE_VALID_ALEXA, target) is not None 

44 

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

46 _LOGGER.debug("SUPERNOTIFY notify_alexa_media %s", envelope.message) 

47 

48 media_players = envelope.targets or [] 

49 

50 if not media_players: 

51 _LOGGER.debug("SUPERNOTIFY skipping alexa media player, no targets") 

52 return False 

53 

54 action_data: dict[str, Any] = {"message": envelope.message, ATTR_DATA: {"type": "announce"}, ATTR_TARGET: media_players} 

55 if envelope.data and envelope.data.get("data"): 

56 action_data[ATTR_DATA].update(envelope.data.get("data")) 

57 return await self.call_action(envelope, action_data=action_data)