Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Dev to Master (#97)
Browse files Browse the repository at this point in the history
* start script improvements

* responding fix

* increase sleep for start script

* increase wait

* Only wait if Plex wasn't already open
  • Loading branch information
maykar authored Feb 16, 2021
1 parent f1fbc40 commit 4d3cc1e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
48 changes: 32 additions & 16 deletions custom_components/plex_assistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
20 changes: 9 additions & 11 deletions custom_components/plex_assistant/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 4d3cc1e

Please sign in to comment.