Esempio n. 1
0
def _patched_ssdp_listener(info: CaseInsensitiveDict, *args, **kwargs):
    listener = SsdpSearchListener(*args, **kwargs)

    async def _async_callback(*_):
        if kwargs["source"][0] == FAIL_TO_BIND_IP:
            raise OSError
        await listener.async_connect_callback()

    @callback
    def _async_search(*_):
        if info:
            asyncio.create_task(listener.async_callback(info))

    listener.async_start = _async_callback
    listener.async_search = _async_search
    return listener
Esempio n. 2
0
def _patched_ssdp_listener(info: ssdp.SsdpHeaders, *args, **kwargs):
    listener = SsdpSearchListener(*args, **kwargs)

    async def _async_callback(*_):
        if kwargs["source_ip"] == IPv4Address(FAIL_TO_BIND_IP):
            raise OSError
        await listener.async_connect_callback()

    @callback
    def _async_search(*_):
        if info:
            asyncio.create_task(listener.async_callback(info))

    listener.async_start = _async_callback
    listener.async_search = _async_search
    return listener
Esempio n. 3
0
    async def async_setup(self) -> None:
        """Set up the scanner."""
        if self._connected_events:
            await self._async_wait_connected()
            return

        for idx, source_ip in enumerate(await self._async_build_source_set()):
            self._connected_events.append(asyncio.Event())

            def _wrap_async_connected_idx(idx) -> Callable[[], Awaitable[None]]:
                """Create a function to capture the idx cell variable."""

                async def _async_connected() -> None:
                    self._connected_events[idx].set()

                return _async_connected

            source = (str(source_ip), 0)
            self._listeners.append(
                SsdpSearchListener(
                    async_callback=self._async_process_entry,
                    service_type=SSDP_ST,
                    target=SSDP_TARGET,
                    source=source,
                    async_connect_callback=_wrap_async_connected_idx(idx),
                )
            )

        results = await asyncio.gather(
            *(listener.async_start() for listener in self._listeners),
            return_exceptions=True,
        )
        failed_listeners = []
        for idx, result in enumerate(results):
            if not isinstance(result, Exception):
                continue
            _LOGGER.warning(
                "Failed to setup listener for %s: %s",
                self._listeners[idx].source,
                result,
            )
            failed_listeners.append(self._listeners[idx])
            self._connected_events[idx].set()

        for listener in failed_listeners:
            self._listeners.remove(listener)

        await self._async_wait_connected()
        self._track_interval = async_track_time_interval(
            self._hass, self.async_scan, DISCOVERY_INTERVAL
        )
        self.async_scan()
Esempio n. 4
0
    async def async_setup(self):
        """Set up the scanner."""
        if self._connected_events:
            await asyncio.gather(*(event.wait() for event in self._connected_events))
            return

        for idx, source_ip in enumerate(await self._async_build_source_set()):
            self._connected_events.append(asyncio.Event())

            def _wrap_async_connected_idx(idx):
                """Create a function to capture the idx cell variable."""

                async def _async_connected():
                    self._connected_events[idx].set()

                return _async_connected

            self._listeners.append(
                SsdpSearchListener(
                    async_callback=self._async_process_entry,
                    service_type=SSDP_ST,
                    target=SSDP_TARGET,
                    source_ip=source_ip,
                    async_connect_callback=_wrap_async_connected_idx(idx),
                )
            )

        results = await asyncio.gather(
            *(listener.async_start() for listener in self._listeners),
            return_exceptions=True,
        )
        failed_listeners = []
        for idx, result in enumerate(results):
            if not isinstance(result, Exception):
                continue
            _LOGGER.warning(
                "Failed to setup listener for %s: %s",
                self._listeners[idx].source_ip,
                result,
            )
            failed_listeners.append(self._listeners[idx])
            self._connected_events[idx].set()

        for listener in failed_listeners:
            self._listeners.remove(listener)

        await asyncio.gather(*(event.wait() for event in self._connected_events))
        self.async_scan()