def get_chromecast(name): global gv_discovered_devices global gv_zconf if (not name or name == 'Disabled'): return None log_message("Looking up Chromecast %s" % (name)) if not gv_zconf: log_message("No zconf service active") return None if not name in gv_discovered_devices: log_message("chromecast not found in discovered device services") return None try: log_message("Getting chromecast device object") # Get the device handle # FIXME this call has issues device = pychromecast.get_chromecast_from_service( gv_discovered_devices[name], gv_zconf) except: log_message("Failed to chromecast device object") device = None return device
def getChromecastDevice(self, device_name): import pychromecast # Get cast from discovered devices of cast platform known_devices = self.hass.data.get(KNOWN_CHROMECAST_INFO_KEY, []) _LOGGER.debug("Chromecast devices: %s", known_devices) try: # HA below 0.113 cast_info = next((x for x in known_devices if x.friendly_name == device_name), None) except: cast_info = next( ( known_devices[x] for x in known_devices if known_devices[x].friendly_name == device_name ), None, ) _LOGGER.debug("cast info: %s", cast_info) if cast_info: return pychromecast.get_chromecast_from_service( ( cast_info.services, cast_info.uuid, cast_info.model_name, cast_info.friendly_name, None, None, ), 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_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: services, browser = pychromecast.discovery.discover_listed_chromecasts( friendly_names=names) else: services, browser = pychromecast.discovery.discover_chromecasts() pychromecast.stop_discovery(browser) devices = [ CastDevice(pychromecast.get_chromecast_from_service(s, browser.zc), s[4], s[5]) for s in services ] devices.sort(key=lambda d: d.cast.name) return devices