mirror of
https://github.com/home-assistant/core.git
synced 2026-12-05 13:21:11 -04:00
Move qbittorrent service registration to services module (#184641)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
f6bdf0a323
commit
0c7945de9d
@@ -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
|
||||
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
Reference in New Issue
Block a user