Coverage for custom_components/appliance_autonotify/supernotify_api.py: 100%
28 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-14 15:14 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-14 15:14 +0000
1"""The parts of Supernotify used, all through its actions and entities so there's no import dependency"""
3from __future__ import annotations
5from typing import Any
7from homeassistant.core import Context, HomeAssistant, callback
9from .const import SUPERNOTIFY_DOMAIN
12@callback
13def supernotify_available(hass: HomeAssistant) -> bool:
14 return hass.services.has_service(SUPERNOTIFY_DOMAIN, "notify")
17async def _enquire(hass: HomeAssistant, action: str, context: Context) -> dict[str, Any]:
18 response = await hass.services.async_call(SUPERNOTIFY_DOMAIN, action, blocking=True, context=context, return_response=True)
19 return dict(response) if response else {}
22async def deliveries_by_transport(hass: HomeAssistant, context: Context | None = None) -> dict[str, list[str]]:
23 """Delivery names for each transport, both those configured and those Supernotify set up itself"""
24 # one context for both enquiries, where the caller has none of its own to pass on
25 context = context or Context()
26 by_transport: dict[str, list[str]] = {}
27 # every delivery has a switch, the only place to find one Supernotify set up itself but that
28 # Delivery Control has made explicit or scenario only, so is neither implicit nor configured
29 for state in hass.states.async_all("switch"):
30 if state.entity_id.startswith(f"switch.{SUPERNOTIFY_DOMAIN}_delivery_"):
31 name, transport = state.attributes.get("name"), state.attributes.get("transport")
32 if name and transport:
33 by_transport.setdefault(transport, []).append(name)
34 implicit = await _enquire(hass, "enquire_implicit_deliveries", context)
35 for transport, names in implicit.items():
36 known = by_transport.setdefault(transport, [])
37 known.extend(name for name in names if name not in known)
38 configured = (await _enquire(hass, "enquire_configuration", context)).get("delivery") or {}
39 for name, delivery in configured.items():
40 names = by_transport.setdefault(delivery.get("transport", name), [])
41 if name not in names:
42 names.append(name)
43 return by_transport