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

27 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-28 14:21 +0000

1import logging 

2import re 

3from typing import Any 

4 

5from homeassistant.components.notify.const import ATTR_DATA, ATTR_TARGET 

6 

7from custom_components.supernotify import METHOD_ALEXA 

8from custom_components.supernotify.delivery_method import DeliveryMethod 

9from custom_components.supernotify.envelope import Envelope 

10 

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

12 

13_LOGGER = logging.getLogger(__name__) 

14 

15 

16class AlexaMediaPlayerDeliveryMethod(DeliveryMethod): 

17 """Notify via Amazon Alexa announcements 

18 

19 options: 

20 TITLE_ONLY: true 

21 

22 """ 

23 

24 method = METHOD_ALEXA 

25 DEFAULT_TITLE_ONLY = True 

26 

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

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

29 

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

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

32 

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

34 _LOGGER.info("SUPERNOTIFY notify_alexa: %s", envelope.message) 

35 

36 media_players = envelope.targets or [] 

37 

38 if not media_players: 

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

40 return False 

41 

42 message = self.combined_message(envelope, default_title_only=self.DEFAULT_TITLE_ONLY) 

43 

44 action_data: dict[str, Any] = {"message": message or "", ATTR_DATA: {"type": "announce"}, ATTR_TARGET: media_players} 

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

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

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