async def async_discover_devices( hass: HomeAssistant, timeout: int, address: str | None = None ) -> list[FluxLEDDiscovery]: """Discover flux led devices.""" if address: targets = [address] else: targets = [ str(address) for address in await network.async_get_ipv4_broadcast_addresses(hass) ] scanner = AIOBulbScanner() for idx, discovered in enumerate( await asyncio.gather( *[ scanner.async_scan(timeout=timeout, address=address) for address in targets ], return_exceptions=True, ) ): if isinstance(discovered, Exception): _LOGGER.debug("Scanning %s failed with error: %s", targets[idx], discovered) continue if not address: return scanner.getBulbInfo() return [ device for device in scanner.getBulbInfo() if device[ATTR_IPADDR] == address ]
async def test_async_scanner_times_out_with_nothing( mock_discovery_aio_protocol): """Test scanner.""" scanner = AIOBulbScanner() task = asyncio.ensure_future(scanner.async_scan(timeout=0.05)) transport, protocol = await mock_discovery_aio_protocol() data = await task assert data == []
async def test_async_scanner_times_out_with_nothing_specific_address( mock_discovery_aio_protocol, ): """Test scanner.""" scanner = AIOBulbScanner() task = asyncio.ensure_future( scanner.async_scan(timeout=0.05, address="192.168.213.252")) transport, protocol = await mock_discovery_aio_protocol() data = await task assert data == []
async def test_async_scanner_specific_address(mock_discovery_aio_protocol): """Test scanner with a specific address.""" scanner = AIOBulbScanner() task = asyncio.ensure_future( scanner.async_scan(timeout=10, address="192.168.213.252")) transport, protocol = await mock_discovery_aio_protocol() protocol.datagram_received(b"192.168.213.252,B4E842E10588,AK001-ZJ2145", ("192.168.213.252", 48899)) protocol.datagram_received(b"+ok=08_15_20210204_ZG-BL\r", ("192.168.213.252", 48899)) data = await task assert data == [{ "firmware_date": datetime.date(2021, 2, 4), "id": "B4E842E10588", "ipaddr": "192.168.213.252", "model": "AK001-ZJ2145", "model_description": "RGB Controller with MIC", "model_info": "ZG-BL", "model_num": 8, "version_num": 21, }] assert scanner.getBulbInfoByID("B4E842E10588") == { "firmware_date": datetime.date(2021, 2, 4), "id": "B4E842E10588", "ipaddr": "192.168.213.252", "model": "AK001-ZJ2145", "model_description": "RGB Controller with MIC", "model_info": "ZG-BL", "model_num": 8, "version_num": 21, } assert scanner.getBulbInfo() == [{ "firmware_date": datetime.date(2021, 2, 4), "id": "B4E842E10588", "ipaddr": "192.168.213.252", "model": "AK001-ZJ2145", "model_description": "RGB Controller with MIC", "model_info": "ZG-BL", "model_num": 8, "version_num": 21, }]
async def test_async_scanner(mock_discovery_aio_protocol): """Test scanner.""" scanner = AIOBulbScanner() task = asyncio.ensure_future( scanner.async_scan(timeout=0.1, address="192.168.213.252")) transport, protocol = await mock_discovery_aio_protocol() protocol.datagram_received(b"HF-A11ASSISTHREAD", ("127.0.0.1", 48899)) protocol.datagram_received(b"192.168.213.252,B4E842E10588,AK001-ZJ2145", ("192.168.213.252", 48899)) protocol.datagram_received(b"AT+LVER\r", ("127.0.0.1", 48899)) protocol.datagram_received(b"+ok=08_15_20210204_ZG-BL\r", ("192.168.213.252", 48899)) protocol.datagram_received(b"192.168.213.65,F4CFA23E1AAF,AK001-ZJ2104", ("192.168.213.65", 48899)) protocol.datagram_received(b"+ok=A2_33_20200428_ZG-LX\r", ("192.168.213.65", 48899)) data = await task assert data == [ { "firmware_date": datetime.date(2021, 2, 4), "id": "B4E842E10588", "ipaddr": "192.168.213.252", "model": "AK001-ZJ2145", "model_description": "RGB Controller with MIC", "model_info": "ZG-BL", "model_num": 8, "version_num": 21, }, { "firmware_date": datetime.date(2020, 4, 28), "id": "F4CFA23E1AAF", "ipaddr": "192.168.213.65", "model": "AK001-ZJ2104", "model_description": "RGB Symphony v2", "model_info": "ZG-LX", "model_num": 162, "version_num": 51, }, ]
async def async_discover_devices( hass: HomeAssistant, timeout: int, address: str | None = None) -> list[dict[str, str]]: """Discover flux led devices.""" scanner = AIOBulbScanner() try: discovered: list[dict[str, str]] = await scanner.async_scan(timeout=timeout, address=address) except OSError as ex: _LOGGER.debug("Scanning failed with error: %s", ex) return [] else: return discovered
async def async_discover_devices( hass: HomeAssistant, timeout: int, address: str | None = None) -> list[FluxLEDDiscovery]: """Discover flux led devices.""" domain_data = hass.data.setdefault(DOMAIN, {}) if FLUX_LED_DISCOVERY_LOCK not in domain_data: domain_data[FLUX_LED_DISCOVERY_LOCK] = asyncio.Lock() async with domain_data[FLUX_LED_DISCOVERY_LOCK]: scanner = AIOBulbScanner() try: discovered = await scanner.async_scan(timeout=timeout, address=address) except OSError as ex: _LOGGER.debug("Scanning failed with error: %s", ex) return [] else: return discovered
async def go(): scanner = AIOBulbScanner() pprint.pprint(await scanner.async_scan())