Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions homeassistant/components/plex/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from .errors import MediaNotFound
from .models import PlexMediaSearchResult
from .server import PlexServer

REFRESH_LIBRARY_SCHEMA = vol.Schema(
{vol.Optional("server_name"): str, vol.Required("library_name"): str}
Expand All @@ -28,7 +29,7 @@
_LOGGER = logging.getLogger(__package__)


async def async_setup_services(hass):
async def async_setup_services(hass: HomeAssistant) -> None:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value was always True, and never checked.
I think it is cleaner to drop the return value.

"""Set up services for the Plex component."""

async def async_refresh_library_service(service_call: ServiceCall) -> None:
Expand All @@ -53,8 +54,6 @@ async def async_scan_clients_service(_: ServiceCall) -> None:
DOMAIN, SERVICE_SCAN_CLIENTS, async_scan_clients_service
)

return True


def refresh_library(hass: HomeAssistant, service_call: ServiceCall) -> None:
"""Scan a Plex library for new and updated media."""
Expand All @@ -77,17 +76,22 @@ def refresh_library(hass: HomeAssistant, service_call: ServiceCall) -> None:
library.update()


def get_plex_server(hass, plex_server_name=None, plex_server_id=None):
def get_plex_server(
hass: HomeAssistant,
plex_server_name: str | None = None,
plex_server_id: str | None = None,
) -> PlexServer:
"""Retrieve a configured Plex server by name."""
if DOMAIN not in hass.data:
raise HomeAssistantError("Plex integration not configured")
plex_servers = hass.data[DOMAIN][SERVERS].values()
if not plex_servers:
servers: dict[str, PlexServer] = hass.data[DOMAIN][SERVERS]

@epenet epenet Feb 1, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small change here to first get the full dict, and only get the values() on line 94.
This means we don't need to get the dict again on line 92, and it helps with typing.

if not servers:
raise HomeAssistantError("No Plex servers available")

if plex_server_id:
return hass.data[DOMAIN][SERVERS][plex_server_id]
return servers[plex_server_id]

plex_servers = servers.values()
if plex_server_name:
plex_server = next(
(x for x in plex_servers if x.friendly_name == plex_server_name), None
Expand All @@ -110,7 +114,11 @@ def get_plex_server(hass, plex_server_name=None, plex_server_id=None):


def process_plex_payload(
hass, content_type, content_id, default_plex_server=None, supports_playqueues=True
hass: HomeAssistant,
content_type: str,
content_id: str,
default_plex_server: PlexServer | None = None,
supports_playqueues: bool = True,
) -> PlexMediaSearchResult:
"""Look up Plex media using media_player.play_media service payloads."""
plex_server = default_plex_server
Expand Down