From 0c7945de9d5deff00ca3204dc01c3af69ed78d82 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 7 Oct 2026 09:10:42 +0200 Subject: [PATCH] Move qbittorrent service registration to services module (#184641) Co-authored-by: Claude Opus 5.5 --- .../components/qbittorrent/__init__.py | 91 ++---------------- .../components/qbittorrent/services.py | 95 +++++++++++++++++++ 2 files changed, 102 insertions(+), 84 deletions(-) create mode 100644 homeassistant/components/qbittorrent/services.py diff --git a/homeassistant/components/qbittorrent/__init__.py b/homeassistant/components/qbittorrent/__init__.py index 820157f2bd6d..e0a2475db0dc 100644 --- a/homeassistant/components/qbittorrent/__init__.py +++ b/homeassistant/components/qbittorrent/__init__.py @@ -1,34 +1,25 @@ """The qbittorrent component.""" import logging -from typing import Any from qbittorrentapi import APIConnectionError, Forbidden403Error, LoginFailed -from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ( - ATTR_DEVICE_ID, CONF_PASSWORD, CONF_URL, CONF_USERNAME, CONF_VERIFY_SSL, Platform, ) -from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse -from homeassistant.exceptions import ConfigEntryNotReady, ServiceValidationError -from homeassistant.helpers import config_validation as cv, device_registry as dr +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.typing import ConfigType -from .const import ( - DOMAIN, - SERVICE_GET_ALL_TORRENTS, - SERVICE_GET_TORRENTS, - STATE_ATTR_ALL_TORRENTS, - STATE_ATTR_TORRENTS, - TORRENT_FILTER, -) +from .const import DOMAIN from .coordinator import QBittorrentConfigEntry, QBittorrentDataCoordinator -from .helpers import format_torrents, setup_client +from .helpers import setup_client +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) @@ -42,75 +33,7 @@ CONF_ENTRY = "entry" async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up qBittorrent services.""" - async def handle_get_torrents(service_call: ServiceCall) -> dict[str, Any] | None: - device_registry = dr.async_get(hass) - device_entry = device_registry.async_get(service_call.data[ATTR_DEVICE_ID]) - - if device_entry is None: - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="invalid_device", - translation_placeholders={ - "device_id": service_call.data[ATTR_DEVICE_ID] - }, - ) - - entry_id = None - - for key, value in device_entry.identifiers: - if key == DOMAIN: - entry_id = value - break - else: - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="invalid_entry_id", - translation_placeholders={"device_id": entry_id or ""}, - ) - - entry: QBittorrentConfigEntry | None = hass.config_entries.async_get_entry( - entry_id - ) - if entry is None or entry.state is not ConfigEntryState.LOADED: - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="invalid_entry_id", - translation_placeholders={"device_id": entry_id}, - ) - coordinator = entry.runtime_data - items = await coordinator.get_torrents(service_call.data[TORRENT_FILTER]) - info = format_torrents(items) - return { - STATE_ATTR_TORRENTS: info, - } - - hass.services.async_register( - DOMAIN, - SERVICE_GET_TORRENTS, - handle_get_torrents, - supports_response=SupportsResponse.ONLY, - ) - - async def handle_get_all_torrents( - service_call: ServiceCall, - ) -> dict[str, Any] | None: - torrents = {} - - for entry in hass.config_entries.async_loaded_entries(DOMAIN): - coordinator: QBittorrentDataCoordinator = entry.runtime_data - items = await coordinator.get_torrents(service_call.data[TORRENT_FILTER]) - torrents[entry.entry_id] = format_torrents(items) - - return { - STATE_ATTR_ALL_TORRENTS: torrents, - } - - hass.services.async_register( - DOMAIN, - SERVICE_GET_ALL_TORRENTS, - handle_get_all_torrents, - supports_response=SupportsResponse.ONLY, - ) + async_setup_services(hass) return True diff --git a/homeassistant/components/qbittorrent/services.py b/homeassistant/components/qbittorrent/services.py new file mode 100644 index 000000000000..968197054d5c --- /dev/null +++ b/homeassistant/components/qbittorrent/services.py @@ -0,0 +1,95 @@ +"""Services for the qBittorrent integration.""" + +from typing import Any + +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ATTR_DEVICE_ID +from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse, callback +from homeassistant.exceptions import ServiceValidationError +from homeassistant.helpers import device_registry as dr + +from .const import ( + DOMAIN, + SERVICE_GET_ALL_TORRENTS, + SERVICE_GET_TORRENTS, + STATE_ATTR_ALL_TORRENTS, + STATE_ATTR_TORRENTS, + TORRENT_FILTER, +) +from .coordinator import QBittorrentConfigEntry, QBittorrentDataCoordinator +from .helpers import format_torrents + + +async def _handle_get_torrents(service_call: ServiceCall) -> dict[str, Any] | None: + hass = service_call.hass + device_registry = dr.async_get(hass) + device_entry = device_registry.async_get(service_call.data[ATTR_DEVICE_ID]) + + if device_entry is None: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_device", + translation_placeholders={"device_id": service_call.data[ATTR_DEVICE_ID]}, + ) + + entry_id = None + + for key, value in device_entry.identifiers: + if key == DOMAIN: + entry_id = value + break + else: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_entry_id", + translation_placeholders={"device_id": entry_id or ""}, + ) + + entry: QBittorrentConfigEntry | None = hass.config_entries.async_get_entry(entry_id) + if entry is None or entry.state is not ConfigEntryState.LOADED: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_entry_id", + translation_placeholders={"device_id": entry_id}, + ) + coordinator = entry.runtime_data + items = await coordinator.get_torrents(service_call.data[TORRENT_FILTER]) + info = format_torrents(items) + return { + STATE_ATTR_TORRENTS: info, + } + + +async def _handle_get_all_torrents( + service_call: ServiceCall, +) -> dict[str, Any] | None: + hass = service_call.hass + torrents = {} + + for entry in hass.config_entries.async_loaded_entries(DOMAIN): + coordinator: QBittorrentDataCoordinator = entry.runtime_data + items = await coordinator.get_torrents(service_call.data[TORRENT_FILTER]) + torrents[entry.entry_id] = format_torrents(items) + + return { + STATE_ATTR_ALL_TORRENTS: torrents, + } + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up the services for the qBittorrent integration.""" + + hass.services.async_register( + DOMAIN, + SERVICE_GET_TORRENTS, + _handle_get_torrents, + supports_response=SupportsResponse.ONLY, + ) + + hass.services.async_register( + DOMAIN, + SERVICE_GET_ALL_TORRENTS, + _handle_get_all_torrents, + supports_response=SupportsResponse.ONLY, + )