Coverage for custom_components/supernotify/methods/persistent.py: 100%

24 statements  

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

1import logging 

2from typing import Any 

3 

4from homeassistant.const import CONF_ACTION, CONF_DEFAULT 

5 

6from custom_components.supernotify import ATTR_NOTIFICATION_ID, CONF_TARGETS_REQUIRED, METHOD_PERSISTENT 

7from custom_components.supernotify.delivery_method import DeliveryMethod 

8from custom_components.supernotify.envelope import Envelope 

9 

10_LOGGER = logging.getLogger(__name__) 

11ACTION = "persistent_notification.create" 

12 

13 

14class PersistentDeliveryMethod(DeliveryMethod): 

15 method = METHOD_PERSISTENT 

16 

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

18 kwargs.setdefault(CONF_DEFAULT, {}) 

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

20 kwargs.setdefault(CONF_TARGETS_REQUIRED, False) 

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

22 

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

24 return action is None or action == ACTION 

25 

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

27 data = envelope.data or {} 

28 config = self.delivery_config(envelope.delivery_name) 

29 

30 notification_id = data.get(ATTR_NOTIFICATION_ID, config.get(ATTR_NOTIFICATION_ID)) 

31 action_data = envelope.core_action_data() 

32 action_data["notification_id"] = notification_id 

33 

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