def _setup_internal_discovery(hass: HomeAssistantType) -> None: """Set up the pychromecast internal discovery.""" if INTERNAL_DISCOVERY_RUNNING_KEY not in hass.data: hass.data[INTERNAL_DISCOVERY_RUNNING_KEY] = threading.Lock() if not hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].acquire(blocking=False): # Internal discovery is already running return import pychromecast def internal_callback(name): """Called when zeroconf has discovered a new chromecast.""" mdns = listener.services[name] _discover_chromecast(hass, ChromecastInfo(*mdns)) _LOGGER.debug("Starting internal pychromecast discovery.") listener, browser = pychromecast.start_discovery(internal_callback) def stop_discovery(event): """Stop discovery of new chromecasts.""" _LOGGER.debug("Stopping internal pychromecast discovery.") pychromecast.stop_discovery(browser) hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].release() hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_discovery)
def _setup_internal_discovery(hass: HomeAssistantType) -> None: """Set up the pychromecast internal discovery.""" if INTERNAL_DISCOVERY_RUNNING_KEY not in hass.data: hass.data[INTERNAL_DISCOVERY_RUNNING_KEY] = threading.Lock() if not hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].acquire(blocking=False): # Internal discovery is already running return import pychromecast def internal_callback(name): """Called when zeroconf has discovered a new chromecast.""" mdns = listener.services[name] _discover_chromecast(hass, ChromecastInfo(*mdns)) _LOGGER.debug("Starting internal pychromecast discovery.") listener, browser = pychromecast.start_discovery(internal_callback) def stop_discovery(event): """Stop discovery of new chromecasts.""" _LOGGER.debug("Stopping internal pychromecast discovery.") pychromecast.stop_discovery(browser) hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].release() hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_discovery)
def setup_internal_discovery(hass: HomeAssistant) -> None: """Set up the pychromecast internal discovery.""" if INTERNAL_DISCOVERY_RUNNING_KEY not in hass.data: hass.data[INTERNAL_DISCOVERY_RUNNING_KEY] = threading.Lock() if not hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].acquire(blocking=False): # Internal discovery is already running return def internal_add_callback(name): """Handle zeroconf discovery of a new chromecast.""" mdns = listener.services[name] discover_chromecast( hass, ChromecastInfo( service=name, host=mdns[0], port=mdns[1], uuid=mdns[2], model_name=mdns[3], friendly_name=mdns[4], ), ) def internal_remove_callback(name, mdns): """Handle zeroconf discovery of a removed chromecast.""" _remove_chromecast( hass, ChromecastInfo( service=name, host=mdns[0], port=mdns[1], uuid=mdns[2], model_name=mdns[3], friendly_name=mdns[4], ), ) _LOGGER.debug("Starting internal pychromecast discovery.") listener = pychromecast.CastListener( internal_add_callback, internal_remove_callback, internal_add_callback, # Use internal_add_callback also for updates ) browser = pychromecast.start_discovery(listener, ChromeCastZeroconf.get_zeroconf()) def stop_discovery(event): """Stop discovery of new chromecasts.""" _LOGGER.debug("Stopping internal pychromecast discovery.") pychromecast.stop_discovery(browser) hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].release() hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_discovery)
def _setup_internal_discovery(hass: HomeAssistantType) -> None: """Set up the pychromecast internal discovery.""" if INTERNAL_DISCOVERY_RUNNING_KEY not in hass.data: hass.data[INTERNAL_DISCOVERY_RUNNING_KEY] = threading.Lock() if not hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].acquire(blocking=False): # Internal discovery is already running return import pychromecast def internal_add_callback(name): """Handle zeroconf discovery of a new chromecast.""" mdns = listener.services[name] _discover_chromecast(hass, ChromecastInfo( service=name, host=mdns[0], port=mdns[1], uuid=mdns[2], model_name=mdns[3], friendly_name=mdns[4], )) def internal_remove_callback(name, mdns): """Handle zeroconf discovery of a removed chromecast.""" _remove_chromecast(hass, ChromecastInfo( service=name, host=mdns[0], port=mdns[1], uuid=mdns[2], model_name=mdns[3], friendly_name=mdns[4], )) _LOGGER.debug("Starting internal pychromecast discovery.") listener, browser = pychromecast.start_discovery(internal_add_callback, internal_remove_callback) ChromeCastZeroconf.set_zeroconf(browser.zc) def stop_discovery(event): """Stop discovery of new chromecasts.""" _LOGGER.debug("Stopping internal pychromecast discovery.") pychromecast.stop_discovery(browser) hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].release() hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_discovery)
def _setup_internal_discovery(hass: HomeAssistantType) -> None: """Set up the pychromecast internal discovery.""" hass.data.setdefault(INTERNAL_DISCOVERY_RUNNING_KEY, threading.Lock()) if not hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].acquire(blocking=False): # Internal discovery is already running return import pychromecast def internal_callback(name): """Called when zeroconf has discovered a new chromecast.""" mdns = listener.services[name] ip_address, port, uuid, _, _ = mdns key = (ip_address, port, uuid) if key in hass.data[KNOWN_CHROMECASTS_KEY]: _LOGGER.debug("Discovered previous chromecast %s", mdns) return _LOGGER.debug("Discovered new chromecast %s", mdns) try: # pylint: disable=protected-access chromecast = pychromecast._get_chromecast_from_host(mdns, blocking=True) except pychromecast.ChromecastConnectionError: _LOGGER.debug( "Can't set up cast with mDNS info %s. " "Assuming it's not a Chromecast", mdns) return hass.data[KNOWN_CHROMECASTS_KEY][key] = chromecast dispatcher_send(hass, SIGNAL_CAST_DISCOVERED, chromecast) _LOGGER.debug("Starting internal pychromecast discovery.") listener, browser = pychromecast.start_discovery(internal_callback) def stop_discovery(event): """Stop discovery of new chromecasts.""" pychromecast.stop_discovery(browser) hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].release() hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_discovery)
def _setup_internal_discovery(hass: HomeAssistantType) -> None: """Set up the pychromecast internal discovery.""" hass.data.setdefault(INTERNAL_DISCOVERY_RUNNING_KEY, threading.Lock()) if not hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].acquire(blocking=False): # Internal discovery is already running return import pychromecast def internal_callback(name): """Called when zeroconf has discovered a new chromecast.""" mdns = listener.services[name] ip_address, port, uuid, _, _ = mdns key = (ip_address, port, uuid) if key in hass.data[KNOWN_CHROMECASTS_KEY]: _LOGGER.debug("Discovered previous chromecast %s", mdns) return _LOGGER.debug("Discovered new chromecast %s", mdns) try: # pylint: disable=protected-access chromecast = pychromecast._get_chromecast_from_host( mdns, blocking=True) except pychromecast.ChromecastConnectionError: _LOGGER.debug("Can't set up cast with mDNS info %s. " "Assuming it's not a Chromecast", mdns) return hass.data[KNOWN_CHROMECASTS_KEY][key] = chromecast dispatcher_send(hass, SIGNAL_CAST_DISCOVERED, chromecast) _LOGGER.debug("Starting internal pychromecast discovery.") listener, browser = pychromecast.start_discovery(internal_callback) def stop_discovery(event): """Stop discovery of new chromecasts.""" pychromecast.stop_discovery(browser) hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].release() hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_discovery)
def setup_internal_discovery(hass: HomeAssistant) -> None: """Set up the pychromecast internal discovery.""" if INTERNAL_DISCOVERY_RUNNING_KEY not in hass.data: hass.data[INTERNAL_DISCOVERY_RUNNING_KEY] = threading.Lock() if not hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].acquire(blocking=False): # Internal discovery is already running return def internal_add_update_callback(uuid, service_name): """Handle zeroconf discovery of a new or updated chromecast.""" service = listener.services[uuid] # For support of deprecated IP based white listing zconf = ChromeCastZeroconf.get_zeroconf() service_info = None tries = 0 while service_info is None and tries < 4: try: service_info = zconf.get_service_info( "_googlecast._tcp.local.", service_name ) except OSError: # If the zeroconf fails to receive the necessary data we abort # adding the service break tries += 1 if not service_info: _LOGGER.warning( "setup_internal_discovery failed to get info for %s, %s", uuid, service_name, ) return addresses = service_info.parsed_addresses() host = addresses[0] if addresses else service_info.server discover_chromecast( hass, ChromecastInfo( services=service[0], uuid=service[1], model_name=service[2], friendly_name=service[3], host=host, port=service_info.port, ), ) def internal_remove_callback(uuid, service_name, service): """Handle zeroconf discovery of a removed chromecast.""" _remove_chromecast( hass, ChromecastInfo( services=service[0], uuid=service[1], model_name=service[2], friendly_name=service[3], ), ) _LOGGER.debug("Starting internal pychromecast discovery") listener = pychromecast.CastListener( internal_add_update_callback, internal_remove_callback, internal_add_update_callback, ) browser = pychromecast.start_discovery(listener, ChromeCastZeroconf.get_zeroconf()) def stop_discovery(event): """Stop discovery of new chromecasts.""" _LOGGER.debug("Stopping internal pychromecast discovery") pychromecast.discovery.stop_discovery(browser) hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].release() hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_discovery)