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
Browse files Browse the repository at this point in the history
  • Loading branch information
maykar committed Jul 9, 2020
1 parent 6f070d3 commit 247c907
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ plex_assistant:

## Companion Sensor

Plex Assistant includes a sensor to display the names of currently connected devices as well as the machine ID of Plex clients (updates once a minute). This is to help with config and troubleshooting. It is advisable to remove the sensor when not needed to cut down on API calls. To use the sensor add the code below to configuration.yaml:
Plex Assistant includes a sensor to display the names of currently connected devices as well as the machine ID of Plex clients. This is to help with config and troubleshooting. To update the sensor send the command "update sensor" to Plex Assistant either through Google Assistant or as a HA service call.

```
sensor:
Expand Down
84 changes: 54 additions & 30 deletions custom_components/plex_assistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class PA:
client_sensor = []
alias_names = []
attr_update = True
running = False


def setup(hass, config):
Expand Down Expand Up @@ -86,25 +85,46 @@ def setup(hass, config):
PA.lib = get_libraries(PA.plex)
PA.alias_names = list(aliases.keys()) if aliases else []

def update_sensor():
clients = [{client.title: {"ID": client.machineIdentifier,
"type": client.product}} for client in PA.clients]
devicelist = list(PA.devices.keys())
state = str(len(devicelist + clients)) + ' connected devices.'
attributes = {
"Connected Devices": {
'Cast Devices': devicelist or 'None',
'Plex Clients': clients or 'None'
},
"friendly_name": "Plex Assistant Devices",
}
sensor = "sensor.plex_assistant_devices"
hass.states.async_set(sensor, state, attributes)

def handle_input(call):
if not call.data.get("command").strip():
command_string = call.data.get("command").strip().lower()

if not command_string:
_LOGGER.warning(localize["no_call"])
return

PA.running = True
PA.attr_update = True
get_chromecasts(blocking=True, callback=cc_callback)
chromecasts = get_chromecasts()
for chromecast in chromecasts:
PA.devices[chromecast.device.friendly_name] = chromecast

PA.clients = PA.server.clients()
PA.client_names = [client.title for client in PA.clients]
PA.client_ids = [client.machineIdentifier for client in PA.clients]

if localize["controls"]["update_sensor"] in command_string:
update_sensor()
return

cast = None
alias = ["", 0]
client = False
speech_error = False

command = process_speech(
call.data.get("command").lower(),
localize,
default_cast,
PA
)
command = process_speech(command_string, localize, default_cast, PA)

if not command["control"]:
_LOGGER.debug({i: command[i] for i in command if i != 'library'})
Expand All @@ -113,34 +133,38 @@ def handle_input(call):
PA.lib = get_libraries(PA.plex)

PA.device_names = list(PA.devices.keys())

try:
devices = PA.device_names + PA.client_names + PA.client_ids
device = fuzzy(command["device"] or default_cast, devices)
devices = PA.device_names + PA.client_names + PA.client_ids
device = fuzzy(command["device"] or default_cast, devices)
if aliases:
alias = fuzzy(command["device"] or default_cast, PA.alias_names)
if alias[1] < 75 and device[1] < 75:
raise Exception()
name = aliases[alias[0]] if alias[1] > device[1] else device[0]
cast = PA.devices[name] if name in PA.device_names else name
client = isinstance(cast, str)
if client:
client_device = next(
c for c in PA.clients if c.title == cast or c.machineIdentifier == cast)
cast = client_device
except Exception:
error = "{0} {1}: \"{2}\"".format(

if alias[1] < 60 and device[1] < 60:
_LOGGER.warning("{0} {1}: \"{2}\"".format(
localize["cast_device"].capitalize(),
localize["not_found"],
command["device"].title()
)
_LOGGER.warning(error)
))
_LOGGER.debug("Device Score: %s", device[1])
_LOGGER.debug("Devices: %s", str(devices))

if aliases:
_LOGGER.debug("Alias Score: %s", alias[1])
_LOGGER.debug("Aliases: %s", str(PA.alias_names))
return

name = aliases[alias[0]] if alias[1] > device[1] else device[0]
cast = PA.devices[name] if name in PA.device_names else name
client = isinstance(cast, str)
if client:
client_device = next(
c for c in PA.clients if c.title == cast or c.machineIdentifier == cast)
cast = client_device

if command["control"]:
control = command["control"]
if client:
cast.proxyThroughServer()
plex_c = PA.server.client(cast)
plex_c = PA.server.client(cast.title)
else:
plex_c = PlexController()
cast.wait()
Expand Down Expand Up @@ -197,7 +221,7 @@ def handle_input(call):
cast.wait()
play_media(float(delay), cast, plex_c, media)

PA.running = False
update_sensor()

hass.services.register(DOMAIN, "command", handle_input)
return True
6 changes: 6 additions & 0 deletions custom_components/plex_assistant/localize.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"stop": "stop",
"jump_forward": "jump forward",
"jump_back": "jump back",
"update_sensor": "update sensor",
},

# Text for errors
Expand Down Expand Up @@ -183,6 +184,7 @@
"stop": "sluta",
"jump_forward": "hoppa framåt",
"jump_back": "hoppa tillbaka",
"update_sensor": "update sensor",
},
"no_call": "Inget kommando mottogs.",

Expand Down Expand Up @@ -297,6 +299,7 @@
"stop": "stop",
"jump_forward": "spring naar voren",
"jump_back": "spring naar achter",
"update_sensor": "update sensor",
},

"not_found": "niet gevonden",
Expand Down Expand Up @@ -426,6 +429,7 @@
"stop": "interrompi",
"jump_forward": "vai avanti",
"jump_back": "vai indietro",
"update_sensor": "update sensor",
},

"not_found": "non trovato",
Expand Down Expand Up @@ -566,6 +570,7 @@
"stop": "arrête",
"jump_forward": "avance",
"jump_back": "recule",
"update_sensor": "update sensor",
},
"not_found": "je n'ai pas trouvé",
"cast_device": "sur",
Expand Down Expand Up @@ -781,6 +786,7 @@
"stop": "stop",
"jump_forward": "para a frente",
"jump_back": "para trás",
"update_sensor": "update sensor",
},

"not_found": "não encontrado",
Expand Down
8 changes: 1 addition & 7 deletions custom_components/plex_assistant/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import time
from .helpers import cc_callback

SCAN_INTERVAL = timedelta(minutes=1)


def setup_platform(hass, config, add_devices, discovery_info=None):
name = "Plex Assistant Devices"
Expand All @@ -42,11 +40,7 @@ def state(self):
def device_state_attributes(self):
return self._attributes

async def async_update(self):
if not PA.running:
PA.attr_update = True
get_chromecasts(blocking=False, callback=cc_callback)
time.sleep(5)
def update(self):
clients = [{client.title: {"ID": client.machineIdentifier,
"type": client.product}} for client in PA.clients]
devicelist = list(PA.devices.keys())
Expand Down

0 comments on commit 247c907

Please sign in to comment.