diff --git a/homeassistant/components/plex/const.py b/homeassistant/components/plex/const.py index b914a89f744..9823443b897 100644 --- a/homeassistant/components/plex/const.py +++ b/homeassistant/components/plex/const.py @@ -46,3 +46,4 @@ MANUAL_SETUP_STRING = "Configure Plex server manually" SERVICE_PLAY_ON_SONOS = "play_on_sonos" SERVICE_REFRESH_LIBRARY = "refresh_library" +SERVICE_SCAN_CLIENTS = "scan_for_clients" diff --git a/homeassistant/components/plex/services.py b/homeassistant/components/plex/services.py index cd127c1465e..23dea809a0d 100644 --- a/homeassistant/components/plex/services.py +++ b/homeassistant/components/plex/services.py @@ -4,7 +4,15 @@ import logging from plexapi.exceptions import NotFound 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( {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): 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( DOMAIN, SERVICE_REFRESH_LIBRARY, async_refresh_library_service, schema=REFRESH_LIBRARY_SCHEMA, ) + hass.services.async_register( + DOMAIN, SERVICE_SCAN_CLIENTS, async_scan_clients_service + ) return True diff --git a/homeassistant/components/plex/services.yaml b/homeassistant/components/plex/services.yaml index e9fb40939b3..366acb43a5b 100644 --- a/homeassistant/components/plex/services.yaml +++ b/homeassistant/components/plex/services.yaml @@ -21,3 +21,6 @@ refresh_library: library_name: description: Name of the Plex library to refresh. example: "TV Shows" + +scan_for_clients: + description: Scan for available clients from the Plex server(s), local network, and plex.tv. diff --git a/tests/components/plex/test_services.py b/tests/components/plex/test_services.py index b40c9f6d4d3..078ba3b97e9 100644 --- a/tests/components/plex/test_services.py +++ b/tests/components/plex/test_services.py @@ -5,6 +5,7 @@ from homeassistant.components.plex.const import ( DOMAIN, PLEX_SERVER_CONFIG, SERVICE_REFRESH_LIBRARY, + SERVICE_SCAN_CLIENTS, ) from homeassistant.const import ( CONF_HOST, @@ -100,3 +101,28 @@ async def test_refresh_library(hass): True, ) 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, + )