Skip to content

Commit 93cd2b8

Browse files
committed
Rename and improve discovery function
1 parent c09dc79 commit 93cd2b8

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

airos/discovery.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ def parse_airos_packet(self, data: bytes, host_ip: str) -> dict[str, Any] | None
276276
return parsed_info
277277

278278

279-
async def async_discover_devices(timeout: int) -> dict[str, dict[str, Any]]:
279+
async def airos_discover_devices(
280+
timeout: int = 30, listen_ip: str = "0.0.0.0", port: int = DISCOVERY_PORT
281+
) -> dict[str, dict[str, Any]]:
280282
"""Discover unconfigured airOS devices on the network for a given timeout.
281283
282284
This function sets up a listener, waits for a period, and returns
@@ -301,7 +303,7 @@ async def _async_airos_device_found(device_info: dict[str, Any]) -> None:
301303
protocol,
302304
) = await asyncio.get_running_loop().create_datagram_endpoint(
303305
lambda: AirOSDiscoveryProtocol(_async_airos_device_found),
304-
local_addr=("0.0.0.0", DISCOVERY_PORT),
306+
local_addr=(listen_ip, port),
305307
)
306308
try:
307309
await asyncio.sleep(timeout)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "airos"
7-
version = "0.2.5"
7+
version = "0.2.6a0"
88
license = "MIT"
99
description = "Ubiquity airOS module(s) for Python 3."
1010
readme = "README.md"

tests/test_discovery.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from airos.discovery import (
99
DISCOVERY_PORT,
1010
AirOSDiscoveryProtocol,
11-
async_discover_devices,
11+
airos_discover_devices,
1212
)
1313
from airos.exceptions import AirOSDiscoveryError, AirOSEndpointError, AirOSListenerError
1414
import pytest
@@ -247,7 +247,7 @@ async def _simulate_discovery():
247247
mock_protocol_factory(MagicMock()).callback(parsed_data)
248248

249249
with patch("asyncio.sleep", new=AsyncMock()):
250-
discovery_task = asyncio.create_task(async_discover_devices(timeout=1))
250+
discovery_task = asyncio.create_task(airos_discover_devices(timeout=1))
251251

252252
await _simulate_discovery()
253253

@@ -264,7 +264,7 @@ async def test_async_discover_devices_no_devices(mock_datagram_endpoint):
264264
mock_transport, _ = mock_datagram_endpoint
265265

266266
with patch("asyncio.sleep", new=AsyncMock()):
267-
result = await async_discover_devices(timeout=1)
267+
result = await airos_discover_devices(timeout=1)
268268

269269
assert result == {}
270270
mock_transport.close.assert_called_once()
@@ -284,7 +284,7 @@ async def test_async_discover_devices_oserror(mock_datagram_endpoint):
284284
side_effect=OSError(98, "Address in use")
285285
)
286286

287-
await async_discover_devices(timeout=1)
287+
await airos_discover_devices(timeout=1)
288288

289289
assert "address_in_use" in str(excinfo.value)
290290
mock_transport.close.assert_not_called()
@@ -300,7 +300,7 @@ async def test_async_discover_devices_cancelled(mock_datagram_endpoint):
300300
patch("asyncio.sleep", new=AsyncMock(side_effect=asyncio.CancelledError)),
301301
pytest.raises(AirOSListenerError) as excinfo,
302302
):
303-
await async_discover_devices(timeout=1)
303+
await airos_discover_devices(timeout=1)
304304

305305
assert "cannot_connect" in str(excinfo.value)
306306
mock_transport.close.assert_called_once()
@@ -373,7 +373,7 @@ async def test_async_discover_devices_generic_oserror(mock_datagram_endpoint):
373373
mock_loop.create_datagram_endpoint = AsyncMock(
374374
side_effect=OSError(13, "Permission denied")
375375
)
376-
await async_discover_devices(timeout=1)
376+
await airos_discover_devices(timeout=1)
377377

378378
assert "cannot_connect" in str(excinfo.value)
379379
mock_transport.close.assert_not_called()

0 commit comments

Comments
 (0)