Coverage for custom_components/supernotify/methods/generic.py: 93%

28 statements  

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

1import logging 

2 

3from homeassistant.const import CONF_ACTION, CONF_TARGET # ATTR_VARIABLES from script.const has import issues 

4 

5from custom_components.supernotify import CONF_DATA, METHOD_GENERIC 

6from custom_components.supernotify.delivery_method import DeliveryMethod 

7from custom_components.supernotify.envelope import Envelope 

8 

9_LOGGER = logging.getLogger(__name__) 

10 

11 

12class GenericDeliveryMethod(DeliveryMethod): 

13 """Call any service, including non-notify ones, like switch.turn_on or mqtt.publish""" 

14 

15 method = METHOD_GENERIC 

16 

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

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

19 

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

21 if action is not None and "." in action: 

22 return True 

23 _LOGGER.warning("SUPERNOTIFY generic method must have a qualified action name, e.g. notify.foo") 

24 return False 

25 

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

27 data = envelope.data or {} 

28 targets = envelope.targets or [] 

29 config = self.delivery_config(envelope.delivery_name) 

30 

31 qualified_action = config.get(CONF_ACTION) 

32 if qualified_action and qualified_action.startswith("notify."): 

33 action_data = envelope.core_action_data() 

34 if targets is not None: 

35 action_data[CONF_TARGET] = targets 

36 if data is not None: 

37 action_data[CONF_DATA] = data 

38 else: 

39 action_data = data 

40 

41 return await self.call_action(envelope, qualified_action, action_data=action_data)