def get_chromecast(name): global gv_discovered_devices global gv_zconf if (not name or name == 'Disabled'): return None log_message(1, "Looking up Chromecast %s" % (name)) if not gv_zconf: log_message(1, "No zconf service active") return None if not name in gv_discovered_devices: log_message(1, "chromecast not found in discovered device services") return None try: log_message(1, "Getting chromecast device object") # Get the device handle device = pychromecast.get_chromecast_from_cast_info( gv_discovered_devices[name], gv_zconf) except: log_message(1, "Failed to chromecast device object") device = None return device
def getChromecastDevice(self, device_name: str) -> None: # Get cast from discovered devices of cast platform known_devices = get_cast_devices(self.hass) _LOGGER.debug("Chromecast devices: %s", known_devices) cast_info = next( ( castinfo for castinfo in known_devices if castinfo.friendly_name == device_name ), None, ) _LOGGER.debug("cast info: %s", cast_info) if cast_info: return pychromecast.get_chromecast_from_cast_info( cast_info.cast_info, ChromeCastZeroconf.get_zeroconf() ) _LOGGER.error( "Could not find device %s from hass.data", device_name, ) raise HomeAssistantError( "Could not find device with name {}".format(device_name) )
def get_cast_device(name): global gv_cast_devices_dict global gv_zconf if (not name or name == 'Disabled'): return None log_message(1, 'Looking up Cast Device [%s]' % (name)) if not gv_zconf: log_message(1, 'No zconf service active') return None if not name in gv_cast_devices_dict: log_message(1, 'cast device not found in discovered device services') return None try: log_message(1, 'Getting cast device object') # Get the device handle # FIXME this call has issues device = pychromecast.get_chromecast_from_cast_info( gv_cast_devices_dict[name], gv_zconf) except: traceback.print_exc() log_message(1, 'Failed to get cast device object') device = None return device
def changeTarget(self, target): device = next( filter(lambda d: d.friendly_name == target, self.browser.devices.values())) if self.cast: self.cast.disconnect() self.cast = pychromecast.get_chromecast_from_cast_info( device, self.browser.zc) self.cast.wait() self.volume.configure(state="normal") self.volumeval.set(self.cast.status.volume_level * 100) self.cast.register_status_listener(self) if self.config.get("RememberLastTarget"): self.config["LastTarget"] = target
def update_cast(self, uuid, service): with sentry_sdk.start_transaction( op="cc_update", name="Chromecast update recieved"): svcs = parent.browser.services cast = pychromecast.get_chromecast_from_cast_info( svcs[uuid], parent.zconf) cast.wait(timeout=2) # We only care about devices that we can cast to (i.e., not # audio devices) if cast.cast_info.cast_type != 'cast': return if uuid not in parent.devices: parent.devices[uuid] = {"cast": cast, "enabled": False} else: parent.devices[uuid]["cast"] = cast if parent.callback_fn is not None: parent.callback_fn()
def getChromecastDevice(self, device_name): import pychromecast # Get cast from discovered devices of cast platform known_devices = list(self.discovered_casts.values()) _LOGGER.debug("Chromecast devices: %s", known_devices) cast_info = next( (x for x in known_devices if x.friendly_name == device_name), None) _LOGGER.debug("cast info: %s", cast_info) if cast_info: zconf = ChromeCastZeroconf.get_zeroconf() return pychromecast.get_chromecast_from_cast_info(cast_info, zconf) _LOGGER.error( "Could not find device %s from hass.data", device_name, ) raise HomeAssistantError( "Could not find device with name {}".format(device_name))
def get_cast_devices(names: Optional[List[str]] = None) -> List[CastDevice]: """ Discover all available devices, optionally filtering them with list of specific device names (which will speedup discovery, as pychromecast does this in a non-blocking manner). :param names: Optional list of device names. :type names: List[str] :returns: List of CastDevice wrapper objects containing cast object and additional ip/port info. :rtype: List[CastDevice] """ if names: cast_infos, browser = pychromecast.discovery.discover_listed_chromecasts( friendly_names=names) else: cast_infos, browser = pychromecast.discovery.discover_chromecasts() browser.stop_discovery() devices = [ CastDevice(pychromecast.get_chromecast_from_cast_info(c, browser.zc), c.host, c.port) for c in cast_infos ] devices.sort(key=lambda d: d.cast.name) return devices