mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Simplify overseerr service actions (#146607)
This commit is contained in:
parent
14c30ef2df
commit
4160521349
@ -25,7 +25,7 @@ from homeassistant.helpers.typing import ConfigType
|
|||||||
|
|
||||||
from .const import DOMAIN, EVENT_KEY, JSON_PAYLOAD, LOGGER, REGISTERED_NOTIFICATIONS
|
from .const import DOMAIN, EVENT_KEY, JSON_PAYLOAD, LOGGER, REGISTERED_NOTIFICATIONS
|
||||||
from .coordinator import OverseerrConfigEntry, OverseerrCoordinator
|
from .coordinator import OverseerrConfigEntry, OverseerrCoordinator
|
||||||
from .services import setup_services
|
from .services import async_setup_services
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.EVENT, Platform.SENSOR]
|
PLATFORMS: list[Platform] = [Platform.EVENT, Platform.SENSOR]
|
||||||
CONF_CLOUDHOOK_URL = "cloudhook_url"
|
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:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up the Overseerr component."""
|
"""Set up the Overseerr component."""
|
||||||
setup_services(hass)
|
async_setup_services(hass)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ from homeassistant.core import (
|
|||||||
ServiceCall,
|
ServiceCall,
|
||||||
ServiceResponse,
|
ServiceResponse,
|
||||||
SupportsResponse,
|
SupportsResponse,
|
||||||
|
callback,
|
||||||
)
|
)
|
||||||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
||||||
from homeassistant.util.json import JsonValueType
|
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."""
|
"""Get the Overseerr config entry."""
|
||||||
if not (entry := hass.config_entries.async_get_entry(config_entry_id)):
|
if not (entry := hass.config_entries.async_get_entry(config_entry_id)):
|
||||||
raise ServiceValidationError(
|
raise ServiceValidationError(
|
||||||
@ -56,7 +57,7 @@ def async_get_entry(hass: HomeAssistant, config_entry_id: str) -> OverseerrConfi
|
|||||||
return cast(OverseerrConfigEntry, entry)
|
return cast(OverseerrConfigEntry, entry)
|
||||||
|
|
||||||
|
|
||||||
async def get_media(
|
async def _get_media(
|
||||||
client: OverseerrClient, media_type: str, identifier: int
|
client: OverseerrClient, media_type: str, identifier: int
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Get media details."""
|
"""Get media details."""
|
||||||
@ -73,12 +74,9 @@ async def get_media(
|
|||||||
return media
|
return media
|
||||||
|
|
||||||
|
|
||||||
def setup_services(hass: HomeAssistant) -> None:
|
async def _async_get_requests(call: ServiceCall) -> ServiceResponse:
|
||||||
"""Set up the services for the Overseerr integration."""
|
|
||||||
|
|
||||||
async def async_get_requests(call: ServiceCall) -> ServiceResponse:
|
|
||||||
"""Get requests made to Overseerr."""
|
"""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
|
client = entry.runtime_data.client
|
||||||
kwargs: dict[str, Any] = {}
|
kwargs: dict[str, Any] = {}
|
||||||
if status := call.data.get(ATTR_STATUS):
|
if status := call.data.get(ATTR_STATUS):
|
||||||
@ -99,17 +97,22 @@ def setup_services(hass: HomeAssistant) -> None:
|
|||||||
for request in requests:
|
for request in requests:
|
||||||
req = asdict(request)
|
req = asdict(request)
|
||||||
assert request.media.tmdb_id
|
assert request.media.tmdb_id
|
||||||
req["media"] = await get_media(
|
req["media"] = await _get_media(
|
||||||
client, request.media.media_type, request.media.tmdb_id
|
client, request.media.media_type, request.media.tmdb_id
|
||||||
)
|
)
|
||||||
result.append(req)
|
result.append(req)
|
||||||
|
|
||||||
return {"requests": cast(list[JsonValueType], result)}
|
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(
|
hass.services.async_register(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_GET_REQUESTS,
|
SERVICE_GET_REQUESTS,
|
||||||
async_get_requests,
|
_async_get_requests,
|
||||||
schema=SERVICE_GET_REQUESTS_SCHEMA,
|
schema=SERVICE_GET_REQUESTS_SCHEMA,
|
||||||
supports_response=SupportsResponse.ONLY,
|
supports_response=SupportsResponse.ONLY,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user