1 """Support for monitoring an SABnzbd NZB client."""
3 from __future__
import annotations
5 from collections.abc
import Callable, Coroutine
9 import voluptuous
as vol
27 from .coordinator
import SabnzbdUpdateCoordinator
28 from .helpers
import get_client
30 PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.NUMBER, Platform.SENSOR]
31 _LOGGER = logging.getLogger(__name__)
39 SERVICE_BASE_SCHEMA = vol.Schema(
41 vol.Required(ATTR_API_KEY): cv.string,
45 SERVICE_SPEED_SCHEMA = SERVICE_BASE_SCHEMA.extend(
47 vol.Optional(ATTR_SPEED, default=DEFAULT_SPEED_LIMIT): cv.string,
51 type SabnzbdConfigEntry = ConfigEntry[SabnzbdUpdateCoordinator]
56 hass: HomeAssistant, call: ServiceCall
57 ) -> SabnzbdConfigEntry:
58 """Get the entry ID related to a service call (by device ID)."""
59 call_data_api_key = call.data[ATTR_API_KEY]
61 for entry
in hass.config_entries.async_entries(DOMAIN):
62 if entry.data[ATTR_API_KEY] == call_data_api_key:
65 raise ValueError(f
"No api for API key: {call_data_api_key}")
69 """Set up the SabNzbd Component."""
73 raise ConfigEntryNotReady
76 await coordinator.async_config_entry_first_refresh()
77 entry.runtime_data = coordinator
82 [ServiceCall, SabnzbdUpdateCoordinator], Coroutine[Any, Any,
None]
84 ) -> Callable[[ServiceCall], Coroutine[Any, Any,
None]]:
85 """Define a decorator to get the correct api for a service call."""
87 async
def wrapper(call: ServiceCall) ->
None:
88 """Wrap the service function."""
90 coordinator = config_entry.runtime_data
93 await func(call, coordinator)
94 except Exception
as err:
96 f
"Error while executing {func.__name__}: {err}"
102 async
def async_pause_queue(
103 call: ServiceCall, coordinator: SabnzbdUpdateCoordinator
105 ir.async_create_issue(
108 "pause_action_deprecated",
110 severity=ir.IssueSeverity.WARNING,
111 breaks_in_ha_version=
"2025.6",
112 translation_key=
"pause_action_deprecated",
114 await coordinator.sab_api.pause_queue()
117 async
def async_resume_queue(
118 call: ServiceCall, coordinator: SabnzbdUpdateCoordinator
120 ir.async_create_issue(
123 "resume_action_deprecated",
125 severity=ir.IssueSeverity.WARNING,
126 breaks_in_ha_version=
"2025.6",
127 translation_key=
"resume_action_deprecated",
129 await coordinator.sab_api.resume_queue()
132 async
def async_set_queue_speed(
133 call: ServiceCall, coordinator: SabnzbdUpdateCoordinator
135 ir.async_create_issue(
138 "set_speed_action_deprecated",
140 severity=ir.IssueSeverity.WARNING,
141 breaks_in_ha_version=
"2025.6",
142 translation_key=
"set_speed_action_deprecated",
144 speed = call.data.get(ATTR_SPEED)
145 await coordinator.sab_api.set_speed_limit(speed)
147 for service, method, schema
in (
148 (SERVICE_PAUSE, async_pause_queue, SERVICE_BASE_SCHEMA),
149 (SERVICE_RESUME, async_resume_queue, SERVICE_BASE_SCHEMA),
150 (SERVICE_SET_SPEED, async_set_queue_speed, SERVICE_SPEED_SCHEMA),
152 if hass.services.has_service(DOMAIN, service):
155 hass.services.async_register(DOMAIN, service, method, schema=schema)
157 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
163 """Unload a Sabnzbd config entry."""
164 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
168 for entry
in hass.config_entries.async_entries(DOMAIN)
169 if entry.state == ConfigEntryState.LOADED
171 if len(loaded_entries) == 1:
174 for service_name
in SERVICES:
175 hass.services.async_remove(DOMAIN, service_name)
def get_client(HomeAssistant hass, data)
bool async_setup_entry(HomeAssistant hass, SabnzbdConfigEntry entry)
SabnzbdConfigEntry async_get_entry_for_service_call(HomeAssistant hass, ServiceCall call)
bool async_unload_entry(HomeAssistant hass, SabnzbdConfigEntry entry)