Simplify overseerr service actions (#146607)

This commit is contained in:
epenet 2025-06-12 12:17:00 +02:00 committed by GitHub
parent 14c30ef2df
commit 4160521349
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 36 deletions

View File

@ -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

View File

@ -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,12 +74,9 @@ async def get_media(
return media
def setup_services(hass: HomeAssistant) -> None:
"""Set up the services for the Overseerr integration."""
async def async_get_requests(call: ServiceCall) -> ServiceResponse:
async def _async_get_requests(call: ServiceCall) -> ServiceResponse:
"""Get requests made to Overseerr."""
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID])
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):
@ -99,17 +97,22 @@ def setup_services(hass: HomeAssistant) -> None:
for request in requests:
req = asdict(request)
assert request.media.tmdb_id
req["media"] = await get_media(
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."""
hass.services.async_register(
DOMAIN,
SERVICE_GET_REQUESTS,
async_get_requests,
_async_get_requests,
schema=SERVICE_GET_REQUESTS_SCHEMA,
supports_response=SupportsResponse.ONLY,
)