def chromecast_agent():
    global gv_discovered_devices
    global gv_zconf

    def chromecast_add_callback(uuid, name):
        # Add discovered device to global dict
        # keyed on friendly name and stores the full 
        # service record
        friendly_name = cast_listener.services[uuid][3]
        gv_discovered_devices[friendly_name] = cast_listener.services[uuid]

    def chromecast_remove_callback(uuid, name, service):
        # purge removed devices from the global dict
        friendly_name = cast_listener.services[uuid][3]
        if friendly_name in gv_discovered_devices:
            del gv_discovered_devices[friendly_name]

    cast_listener = pychromecast.CastListener(
            chromecast_add_callback,
            chromecast_remove_callback)

    gv_zconf = zeroconf.Zeroconf()
    cast_browser = pychromecast.discovery.start_discovery(
            cast_listener, 
            gv_zconf)

    while (1):
        time.sleep(30)
        log_message('Discovered cast devices: %s' % (
            list(gv_discovered_devices.keys())))

    return 
Beispiel #2
0
 def scanChromecast(self):
     self.listener = pychromecast.CastListener(self.cc_added_callback,
                                               self.cc_removed_callback,
                                               self.cc_updated_callback)
     zconf = zeroconf.Zeroconf()
     self.browser = pychromecast.discovery.start_discovery(
         self.listener, zconf)
Beispiel #3
0
def cast_device_discovery_agent():
    global gv_cast_devices_dict
    global gv_zconf

    def cast_device_add_callback(uuid, name):
        # Add discovered device to global dict
        # keyed on friendly name and stores the full
        # service record
        friendly_name = cast_listener.services[uuid][3]
        gv_cast_devices_dict[friendly_name] = cast_listener.services[uuid]

    def cast_device_remove_callback(uuid, name, service):
        # purge removed devices from the global dict
        friendly_name = cast_listener.services[uuid][3]
        if friendly_name in gv_cast_devices_dict:
            del gv_cast_devices_dict[friendly_name]

    # cast listener (add, remove, update)
    # treat update as add
    cast_listener = pychromecast.CastListener(cast_device_add_callback,
                                              cast_device_remove_callback,
                                              cast_device_add_callback)

    gv_zconf = zeroconf.Zeroconf()
    cast_browser = pychromecast.discovery.start_discovery(
        cast_listener, gv_zconf)

    while (True):
        time.sleep(30)
        log_message(
            0, 'All discovered cast devices: %s' %
            (list(gv_cast_devices_dict.keys())))

    return
Beispiel #4
0
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)
Beispiel #5
0
	def _run(self):
		''' starts the server
		'''
		self.listener = pychromecast.CastListener(
			self.add_service, self.remove_service, self.update_callback)
		self.browser = pychromecast.discovery.start_discovery(
			self.listener, self.zconf)
		while self.runFlag:
			time.sleep(2)
			for device_friendly_name in self.devices:
				self.send_device_play_status(device_friendly_name, False)
Beispiel #6
0
    async def on_start(self) -> bool:
        """Handle initialization of the provider based on config."""
        self._listener = pychromecast.CastListener(
            self.__chromecast_add_update_callback,
            self.__chromecast_remove_callback,
            self.__chromecast_add_update_callback,
        )

        def start_discovery():
            self._browser = pychromecast.discovery.start_discovery(
                self._listener, self.mass.zeroconf)

        self.mass.add_job(start_discovery)
        return True
        print("  {} {}".format(uuid, service))


def add_callback(uuid, name):
    print("Found mDNS service for cast device {}".format(uuid))
    list_devices()


def remove_callback(uuid, name, service):
    print("Lost mDNS service for cast device {} {}".format(uuid, service))
    list_devices()


def update_callback(uuid, name):
    print("Updated mDNS service for cast device {}".format(uuid))
    list_devices()


listener = pychromecast.CastListener(add_callback, remove_callback,
                                     update_callback)
zconf = zeroconf.Zeroconf()
browser = pychromecast.discovery.start_discovery(listener, zconf)

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    pass

pychromecast.stop_discovery(browser)
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)