mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add service to scan for new Plex clients (#39074)
This commit is contained in:
parent
226406b853
commit
aecd74c6af
@ -46,3 +46,4 @@ MANUAL_SETUP_STRING = "Configure Plex server manually"
|
|||||||
|
|
||||||
SERVICE_PLAY_ON_SONOS = "play_on_sonos"
|
SERVICE_PLAY_ON_SONOS = "play_on_sonos"
|
||||||
SERVICE_REFRESH_LIBRARY = "refresh_library"
|
SERVICE_REFRESH_LIBRARY = "refresh_library"
|
||||||
|
SERVICE_SCAN_CLIENTS = "scan_for_clients"
|
||||||
|
@ -4,7 +4,15 @@ import logging
|
|||||||
from plexapi.exceptions import NotFound
|
from plexapi.exceptions import NotFound
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from .const import DOMAIN, SERVERS, SERVICE_REFRESH_LIBRARY
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
|
|
||||||
|
from .const import (
|
||||||
|
DOMAIN,
|
||||||
|
PLEX_UPDATE_PLATFORMS_SIGNAL,
|
||||||
|
SERVERS,
|
||||||
|
SERVICE_REFRESH_LIBRARY,
|
||||||
|
SERVICE_SCAN_CLIENTS,
|
||||||
|
)
|
||||||
|
|
||||||
REFRESH_LIBRARY_SCHEMA = vol.Schema(
|
REFRESH_LIBRARY_SCHEMA = vol.Schema(
|
||||||
{vol.Optional("server_name"): str, vol.Required("library_name"): str}
|
{vol.Optional("server_name"): str, vol.Required("library_name"): str}
|
||||||
@ -19,12 +27,20 @@ async def async_setup_services(hass):
|
|||||||
async def async_refresh_library_service(service_call):
|
async def async_refresh_library_service(service_call):
|
||||||
await hass.async_add_executor_job(refresh_library, hass, service_call)
|
await hass.async_add_executor_job(refresh_library, hass, service_call)
|
||||||
|
|
||||||
|
async def async_scan_clients_service(_):
|
||||||
|
_LOGGER.info("Scanning for new Plex clients")
|
||||||
|
for server_id in hass.data[DOMAIN][SERVERS]:
|
||||||
|
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||||
|
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_REFRESH_LIBRARY,
|
SERVICE_REFRESH_LIBRARY,
|
||||||
async_refresh_library_service,
|
async_refresh_library_service,
|
||||||
schema=REFRESH_LIBRARY_SCHEMA,
|
schema=REFRESH_LIBRARY_SCHEMA,
|
||||||
)
|
)
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN, SERVICE_SCAN_CLIENTS, async_scan_clients_service
|
||||||
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -21,3 +21,6 @@ refresh_library:
|
|||||||
library_name:
|
library_name:
|
||||||
description: Name of the Plex library to refresh.
|
description: Name of the Plex library to refresh.
|
||||||
example: "TV Shows"
|
example: "TV Shows"
|
||||||
|
|
||||||
|
scan_for_clients:
|
||||||
|
description: Scan for available clients from the Plex server(s), local network, and plex.tv.
|
||||||
|
@ -5,6 +5,7 @@ from homeassistant.components.plex.const import (
|
|||||||
DOMAIN,
|
DOMAIN,
|
||||||
PLEX_SERVER_CONFIG,
|
PLEX_SERVER_CONFIG,
|
||||||
SERVICE_REFRESH_LIBRARY,
|
SERVICE_REFRESH_LIBRARY,
|
||||||
|
SERVICE_SCAN_CLIENTS,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
@ -100,3 +101,28 @@ async def test_refresh_library(hass):
|
|||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
assert not mock_update.called
|
assert not mock_update.called
|
||||||
|
|
||||||
|
|
||||||
|
async def test_scan_clients(hass):
|
||||||
|
"""Test scan_for_clients service call."""
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
data=DEFAULT_DATA,
|
||||||
|
options=DEFAULT_OPTIONS,
|
||||||
|
unique_id=DEFAULT_DATA["server_id"],
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_plex_server = MockPlexServer(config_entry=entry)
|
||||||
|
|
||||||
|
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||||
|
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
||||||
|
), patch("homeassistant.components.plex.PlexWebsocket", autospec=True):
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert await hass.services.async_call(
|
||||||
|
DOMAIN,
|
||||||
|
SERVICE_SCAN_CLIENTS,
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user