diff --git a/custom_components/plex_assistant/__init__.py b/custom_components/plex_assistant/__init__.py index df99f58..33aba6b 100644 --- a/custom_components/plex_assistant/__init__.py +++ b/custom_components/plex_assistant/__init__.py @@ -13,6 +13,7 @@ from homeassistant.components.plex.services import get_plex_server from homeassistant.components.zeroconf import async_get_instance from pychromecast.controllers.plex import PlexController +from datetime import timedelta import os import json @@ -107,14 +108,10 @@ def pa_executor(_server, start_script_keys): entry.add_update_listener(async_reload_entry) - ha_plex_server = get_plex_server(hass, server_name) - def handle_input(call): offset = None media = None - ha_plex_server._async_update_platforms() - command = call.data.get("command").strip() _LOGGER.debug("Command: %s", command) @@ -145,24 +142,43 @@ def handle_input(call): pa.update_libraries() device = fuzzy(command["device"] or default_device, pa.device_names) - responding = device_responding(hass, pa, device[0]) if device[0] in start_script_keys else True + responding = True if device[0] in start_script_keys: - if device[0] not in pa.device_names or not responding: - timeout = 0 - hass.services.call("script", start_script[device[0]].replace("script.", "")) + timeout = 0 + started = False + responding = False + woken = False + start_time = time.time() + while timeout < 30 and device[0] not in pa.devices: + started = True + if timeout == 0: + hass.services.call("script", start_script[device[0]].replace("script.", "")) + time.sleep(5) + hass.services.call("plex", "scan_for_clients") + else: + time.sleep(1) + if (timeout % 2) == 0 or timeout == 0: + get_devices(hass, pa) + timeout += 1 + + if started: + hass.services.async_call("plex", "scan_for_clients") + get_devices(hass, pa) - while timeout < 40 and not responding: - if (timeout % 10) == 0: - hass.services.call("plex", "scan_for_clients") + if device[0] in pa.devices: + stop = False + while not responding and not stop: + if not started: + hass.services.call("script", start_script[device[0]].replace("script.", "")) responding = device_responding(hass, pa, device[0]) - time.sleep(1) - timeout += 1 + stop = True + total_time = timedelta(seconds=time.time()) - timedelta(seconds=start_time) - if responding: - time.sleep(3) + if responding and not started and total_time > timedelta(seconds=1): + time.sleep(5) - device = fuzzy(command["device"] or default_device, list(pa.devices.keys())) + device = fuzzy(command["device"] or default_device, list(pa.devices.keys())) _LOGGER.debug("PA Devices: %s", pa.devices) diff --git a/custom_components/plex_assistant/helpers.py b/custom_components/plex_assistant/helpers.py index dc617f0..9ac00b4 100644 --- a/custom_components/plex_assistant/helpers.py +++ b/custom_components/plex_assistant/helpers.py @@ -41,17 +41,15 @@ def get_devices(hass, pa): def device_responding(hass, pa, device): - get_devices(hass, pa) - if device in pa.devices: - entity_device = hass.data["media_player"].get_entity(pa.devices[device]["entity_id"]).device - try: - entity_device.query("/resources", timeout=5) - return True - except plexapi.exceptions.BadRequest: - return True - except: - return False - return False + responding = hass.services.call( + "media_player", + "media_play", + {"entity_id": pa.devices[device]["entity_id"]}, + blocking = True, + limit=30 + ) + return responding + async def listeners(hass):