def handle_input(call): if not call.data.get("command").strip(): _LOGGER.warning(localize["no_call"]) return command_string = call.data.get("command").strip().lower() _LOGGER.debug("Command: %s", command_string) PA.client_update = True get_chromecasts(blocking=False, callback=cc_callback, zeroconf_instance=zc) if localize["controls"]["update_sensor"] in command_string: update_sensor(hass) return cast = None alias = ["", 0] client = False speech_error = False command = process_speech(command_string, localize, default_cast, PA) PA.device_names = list(PA.devices.keys()) if not command["control"]: _LOGGER.debug({i: command[i] for i in command if i != "library"}) if PA.lib["updated"] < PA.plex.search(sort="addedAt:desc", limit=1)[0].addedAt: PA.lib = get_libraries(PA.plex) 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] < 60 and device[1] < 60: _LOGGER.warning('{0} {1}: "{2}"'.format( localize["cast_device"].capitalize(), localize["not_found"], command["device"].title(), )) _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 = cast else: plex_c = PlexController() cast.wait() cast.register_handler(plex_c) if control == "play": plex_c.play() elif control == "pause": plex_c.pause() elif control == "stop": plex_c.stop() elif control == "jump_forward": plex_c.stepForward() elif control == "jump_back": plex_c.stepBack() return try: result = find_media(command, command["media"], PA.lib) media = media_selection(command, result["media"], result["library"]) except Exception: error = media_error(command, localize) if tts_error: tts = gTTS(error, lang=lang) tts.save(directory + "error.mp3") speech_error = True if speech_error and not client: cast.wait() med_con = cast.media_controller mp3 = get_url(hass) + "/local/plex_assist_tts/error.mp3" med_con.play_media(mp3, "audio/mpeg") med_con.block_until_active() return _LOGGER.debug("Media: %s", str(media)) if client: _LOGGER.debug("Client: %s", cast) cast.proxyThroughServer() plex_c = cast plex_c.playMedia(media) else: _LOGGER.debug("Cast: %s", cast.name) plex_c = PlexController() cast.register_handler(plex_c) cast.wait() plex_c.block_until_playing(media) update_sensor(hass)
media = plex_server.createPlayQueue(libraryItems) # Set starting position to the 3rd item if startItem demo. startItem = libraryItems[2] if args.startitem else None # Print info media_info(media, media.items) start_item_info(libraryItems[2]) elif args.demo == "playlist": # Convert list into a playlist for media. media = plex_server.createPlaylist("pychromecast test playlist", libraryItems) # Set starting position to the 4th item if startItem demo. startItem = libraryItems[3] if args.startitem else None # Print info media_info(media, media.items()) start_item_info(libraryItems[2]) plex_c = PlexController() cast.register_handler(plex_c) cast.wait() # Plays the media item, list, playlist, or playqueue. # If startItem = None it is ignored and playback starts at first item, # otherwise playback starts at the position of the media item given. plex_c.block_until_playing(media, startItem=startItem) if getattr(media, "TYPE", None) == "playlist": media.delete() # Shut down discovery browser.stop_discovery()