-
-
Notifications
You must be signed in to change notification settings - Fork 32k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Overseerr service to get requests #134229
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dc2a9c2
Add service to get requests
joostlek a03a6d3
Add service to get requests
joostlek 752677c
Add service to get requests
joostlek d8663cc
fix
joostlek 0505dd8
Fix
joostlek d692397
Merge branch 'dev' into get-requests
joostlek bed9902
Merge branch 'dev' into get-requests
joostlek f537279
Add tests
joostlek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,10 @@ | |
"default": "mdi:message-bulleted" | ||
} | ||
} | ||
}, | ||
"services": { | ||
"get_requests": { | ||
"service": "mdi:multimedia" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
"""Define services for the Overseerr integration.""" | ||
|
||
from dataclasses import asdict | ||
from typing import Any, cast | ||
|
||
from python_overseerr import OverseerrClient, OverseerrConnectionError | ||
import voluptuous as vol | ||
|
||
from homeassistant.config_entries import ConfigEntryState | ||
from homeassistant.core import ( | ||
HomeAssistant, | ||
ServiceCall, | ||
ServiceResponse, | ||
SupportsResponse, | ||
) | ||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError | ||
from homeassistant.util.json import JsonValueType | ||
|
||
from .const import ( | ||
ATTR_CONFIG_ENTRY_ID, | ||
ATTR_REQUESTED_BY, | ||
ATTR_SORT_ORDER, | ||
ATTR_STATUS, | ||
DOMAIN, | ||
LOGGER, | ||
) | ||
from .coordinator import OverseerrConfigEntry | ||
|
||
SERVICE_GET_REQUESTS = "get_requests" | ||
SERVICE_GET_REQUESTS_SCHEMA = vol.Schema( | ||
{ | ||
vol.Required(ATTR_CONFIG_ENTRY_ID): str, | ||
vol.Optional(ATTR_STATUS): vol.In( | ||
["approved", "pending", "available", "processing", "unavailable", "failed"] | ||
), | ||
vol.Optional(ATTR_SORT_ORDER): vol.In(["added", "modified"]), | ||
vol.Optional(ATTR_REQUESTED_BY): int, | ||
} | ||
) | ||
|
||
|
||
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( | ||
translation_domain=DOMAIN, | ||
translation_key="integration_not_found", | ||
translation_placeholders={"target": DOMAIN}, | ||
) | ||
if entry.state is not ConfigEntryState.LOADED: | ||
raise ServiceValidationError( | ||
translation_domain=DOMAIN, | ||
translation_key="not_loaded", | ||
translation_placeholders={"target": entry.title}, | ||
) | ||
return cast(OverseerrConfigEntry, entry) | ||
|
||
|
||
async def get_media( | ||
client: OverseerrClient, media_type: str, identifier: int | ||
) -> dict[str, Any]: | ||
"""Get media details.""" | ||
media = {} | ||
try: | ||
if media_type == "movie": | ||
media = asdict(await client.get_movie_details(identifier)) | ||
if media_type == "tv": | ||
media = asdict(await client.get_tv_details(identifier)) | ||
except OverseerrConnectionError: | ||
LOGGER.error("Could not find data for %s %s", media_type, identifier) | ||
return {} | ||
media["media_info"].pop("requests") | ||
return media | ||
|
||
|
||
def 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, | ||
schema=SERVICE_GET_REQUESTS_SCHEMA, | ||
supports_response=SupportsResponse.ONLY, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
get_requests: | ||
fields: | ||
config_entry_id: | ||
required: true | ||
selector: | ||
config_entry: | ||
integration: overseerr | ||
status: | ||
selector: | ||
select: | ||
options: | ||
- approved | ||
- pending | ||
- available | ||
- processing | ||
- unavailable | ||
- failed | ||
translation_key: request_status | ||
sort_order: | ||
selector: | ||
select: | ||
options: | ||
- added | ||
- modified | ||
translation_key: request_sort_order | ||
requested_by: | ||
selector: | ||
number: | ||
min: 0 | ||
mode: box |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add tests with more of the args?