Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network scanning interface #55

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies = [
"click",
"coloredlogs",
"scapy",
"zigpy>=0.55.0",
"zigpy>=0.75.0",
"bellows>=0.43.0",
"zigpy-deconz>=0.21.0",
"zigpy-xbee>=0.18.0",
Expand Down
63 changes: 63 additions & 0 deletions zigpy_cli/radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,66 @@
+ b"\n"
)
output.flush()


@radio.command()
@click.pass_obj
@click.option("-r", "--randomize", is_flag=True, type=bool, default=False)
@click.option("-u", "--unfiltered", is_flag=True, type=bool, default=False)
@click.option("-n", "--num-scans", type=int, default=-1)
@click.option("-d", "--duration-exponent", type=int, default=4)
@click.option(

Check warning on line 327 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L321-L327

Added lines #L321 - L327 were not covered by tests
"-c",
"--channels",
type=CHANNELS_LIST,
default=zigpy.types.Channels.ALL_CHANNELS,
)
@click_coroutine
async def network_scan(

Check warning on line 334 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L333-L334

Added lines #L333 - L334 were not covered by tests
app, randomize, unfiltered, num_scans, duration_exponent, channels
):
if app._network_scan is ControllerApplication._network_scan:
raise click.ClickException("Network scan is not supported by this radio")

Check warning on line 338 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L337-L338

Added lines #L337 - L338 were not covered by tests

await app.startup()
LOGGER.info("Running scan...")

Check warning on line 341 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L340-L341

Added lines #L340 - L341 were not covered by tests

seen_beacons = set()

Check warning on line 343 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L343

Added line #L343 was not covered by tests

if randomize:

Check warning on line 345 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L345

Added line #L345 was not covered by tests

def channels_iter_func():

Check warning on line 347 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L347

Added line #L347 was not covered by tests
while True:
yield random.choice(tuple(channels))

Check warning on line 349 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L349

Added line #L349 was not covered by tests

channels_iter = channels_iter_func()

Check warning on line 351 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L351

Added line #L351 was not covered by tests
else:
channels_iter = itertools.cycle(channels)

Check warning on line 353 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L353

Added line #L353 was not covered by tests

for scan in itertools.count():
channel = next(channels_iter)

Check warning on line 356 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L355-L356

Added lines #L355 - L356 were not covered by tests

if num_scans != -1 and scan > num_scans:
break

Check warning on line 359 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L358-L359

Added lines #L358 - L359 were not covered by tests

print(f"Scanning channel {channel}\r", end="", flush=True)

Check warning on line 361 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L361

Added line #L361 was not covered by tests

async for network in app.network_scan(

Check warning on line 363 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L363

Added line #L363 was not covered by tests
channels=zigpy.types.Channels.from_channel_list([channel]),
duration_exp=duration_exponent,
):
key = network.replace(lqi=None, rssi=None)

Check warning on line 367 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L367

Added line #L367 was not covered by tests

if key in seen_beacons:
continue

Check warning on line 370 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L369-L370

Added lines #L369 - L370 were not covered by tests

seen_beacons.add(key)

Check warning on line 372 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L372

Added line #L372 was not covered by tests

print(

Check warning on line 374 in zigpy_cli/radio.py

View check run for this annotation

Codecov / codecov/patch

zigpy_cli/radio.py#L374

Added line #L374 was not covered by tests
f"channel: {network.channel}, "
f"network: {network.pan_id} ({network.extended_pan_id}), "
f"permitting joins: {int(network.permit_joining)}, "
f"nwk update id: {network.nwk_update_id}, "
f"lqi: {network.lqi:>4}, "
f"rssi: {network.rssi:>3}"
)
Loading