From 41605213494548323c021b341eec361fcfa048ca Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 12 Jun 2025 12:17:00 +0200 Subject: [PATCH] Simplify overseerr service actions (#146607) --- .../components/overseerr/__init__.py | 4 +- .../components/overseerr/services.py | 71 ++++++++++--------- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/homeassistant/components/overseerr/__init__.py b/homeassistant/components/overseerr/__init__.py index 597d44f66cf..3e7b5f32272 100644 --- a/homeassistant/components/overseerr/__init__.py +++ b/homeassistant/components/overseerr/__init__.py @@ -25,7 +25,7 @@ from homeassistant.helpers.typing import ConfigType from .const import DOMAIN, EVENT_KEY, JSON_PAYLOAD, LOGGER, REGISTERED_NOTIFICATIONS from .coordinator import OverseerrConfigEntry, OverseerrCoordinator -from .services import setup_services +from .services import async_setup_services PLATFORMS: list[Platform] = [Platform.EVENT, Platform.SENSOR] CONF_CLOUDHOOK_URL = "cloudhook_url" @@ -35,7 +35,7 @@ CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the Overseerr component.""" - setup_services(hass) + async_setup_services(hass) return True diff --git a/homeassistant/components/overseerr/services.py b/homeassistant/components/overseerr/services.py index 4631e578af8..4e72f555603 100644 --- a/homeassistant/components/overseerr/services.py +++ b/homeassistant/components/overseerr/services.py @@ -12,6 +12,7 @@ from homeassistant.core import ( ServiceCall, ServiceResponse, SupportsResponse, + callback, ) from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.util.json import JsonValueType @@ -39,7 +40,7 @@ SERVICE_GET_REQUESTS_SCHEMA = vol.Schema( ) -def async_get_entry(hass: HomeAssistant, config_entry_id: str) -> OverseerrConfigEntry: +def _async_get_entry(hass: HomeAssistant, config_entry_id: str) -> OverseerrConfigEntry: """Get the Overseerr config entry.""" if not (entry := hass.config_entries.async_get_entry(config_entry_id)): raise ServiceValidationError( @@ -56,7 +57,7 @@ def async_get_entry(hass: HomeAssistant, config_entry_id: str) -> OverseerrConfi return cast(OverseerrConfigEntry, entry) -async def get_media( +async def _get_media( client: OverseerrClient, media_type: str, identifier: int ) -> dict[str, Any]: """Get media details.""" @@ -73,43 +74,45 @@ async def get_media( return media -def setup_services(hass: HomeAssistant) -> None: +async def _async_get_requests(call: ServiceCall) -> ServiceResponse: + """Get requests made to Overseerr.""" + entry = _async_get_entry(call.hass, call.data[ATTR_CONFIG_ENTRY_ID]) + client = entry.runtime_data.client + kwargs: dict[str, Any] = {} + if status := call.data.get(ATTR_STATUS): + kwargs["status"] = status + if sort_order := call.data.get(ATTR_SORT_ORDER): + kwargs["sort"] = sort_order + if requested_by := call.data.get(ATTR_REQUESTED_BY): + kwargs["requested_by"] = requested_by + try: + requests = await client.get_requests(**kwargs) + except OverseerrConnectionError as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="connection_error", + translation_placeholders={"error": str(err)}, + ) from err + result: list[dict[str, Any]] = [] + for request in requests: + req = asdict(request) + assert request.media.tmdb_id + req["media"] = await _get_media( + client, request.media.media_type, request.media.tmdb_id + ) + result.append(req) + + return {"requests": cast(list[JsonValueType], result)} + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: """Set up the services for the Overseerr integration.""" - async def async_get_requests(call: ServiceCall) -> ServiceResponse: - """Get requests made to Overseerr.""" - entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID]) - client = entry.runtime_data.client - kwargs: dict[str, Any] = {} - if status := call.data.get(ATTR_STATUS): - kwargs["status"] = status - if sort_order := call.data.get(ATTR_SORT_ORDER): - kwargs["sort"] = sort_order - if requested_by := call.data.get(ATTR_REQUESTED_BY): - kwargs["requested_by"] = requested_by - try: - requests = await client.get_requests(**kwargs) - except OverseerrConnectionError as err: - raise HomeAssistantError( - translation_domain=DOMAIN, - translation_key="connection_error", - translation_placeholders={"error": str(err)}, - ) from err - result: list[dict[str, Any]] = [] - for request in requests: - req = asdict(request) - assert request.media.tmdb_id - req["media"] = await get_media( - client, request.media.media_type, request.media.tmdb_id - ) - result.append(req) - - return {"requests": cast(list[JsonValueType], result)} - hass.services.async_register( DOMAIN, SERVICE_GET_REQUESTS, - async_get_requests, + _async_get_requests, schema=SERVICE_GET_REQUESTS_SCHEMA, supports_response=SupportsResponse.ONLY, )