Coverage for custom_components/supernotify/methods/media_player_image.py: 92%

38 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-10-26 08:54 +0000

1import logging 

2import re 

3import urllib.parse 

4from typing import Any 

5 

6from homeassistant.const import CONF_ACTION, CONF_DEFAULT 

7 

8from custom_components.supernotify import CONF_TARGETS_REQUIRED, METHOD_MEDIA 

9from custom_components.supernotify.delivery_method import DeliveryMethod 

10from custom_components.supernotify.envelope import Envelope 

11 

12RE_VALID_MEDIA_PLAYER = r"media_player\.[A-Za-z0-9_]+" 

13 

14_LOGGER = logging.getLogger(__name__) 

15ACTION = "media_player.play_media" 

16 

17 

18class MediaPlayerImageDeliveryMethod(DeliveryMethod): 

19 """Requires Alex Media Player integration""" 

20 

21 method = METHOD_MEDIA 

22 

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

24 kwargs.setdefault(CONF_DEFAULT, {}) 

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

26 kwargs[CONF_TARGETS_REQUIRED] = False 

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

28 

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

30 return re.fullmatch(RE_VALID_MEDIA_PLAYER, target) is not None 

31 

32 def validate_action(self, action: str | None) -> bool: 

33 return action is None or action == "media_player.play_media" 

34 

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

36 _LOGGER.debug("SUPERNOTIFY notify_media: %s", envelope.data) 

37 

38 data: dict[str, Any] = envelope.data or {} 

39 media_players: list[str] = envelope.targets or [] 

40 if not media_players: 

41 _LOGGER.debug("SUPERNOTIFY skipping media show, no targets") 

42 return False 

43 

44 snapshot_url = data.get("snapshot_url") 

45 if snapshot_url is None: 

46 _LOGGER.debug("SUPERNOTIFY skipping media player, no image url") 

47 return False 

48 # absolutize relative URL for external URl, probably preferred by Alexa Show etc 

49 snapshot_url = urllib.parse.urljoin(self.context.hass_external_url, snapshot_url) 

50 

51 action_data: dict[str, Any] = { 

52 "media_content_id": snapshot_url, 

53 "media_content_type": "image", 

54 "entity_id": media_players, 

55 } 

56 if data and data.get("data"): 

57 action_data["extra"] = data.get("data", {}) 

58 

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