Skip to content

Commit f775fb5

Browse files
committed
Switch to JSON and dump network information
1 parent db2e486 commit f775fb5

File tree

1 file changed

+64
-23
lines changed

1 file changed

+64
-23
lines changed

zigpy_cli/radio.py

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,34 @@ async def energy_scan(app, num_scans):
228228

229229
@radio.command()
230230
@click.pass_obj
231-
@click.option("-n", "--num-scans", type=int, default=10 * 2**8)
231+
@click.option("-e", "--num-energy-scans", type=int, default=10 * 2**8)
232+
@click.option("-n", "--num-network-scans", type=int, default=5)
232233
@click.option("-r", "--randomize", type=bool, default=True)
233234
@click.argument("output", type=click.File("w"), default="-")
234235
@click_coroutine
235-
async def advanced_energy_scan(app, output, num_scans, randomize):
236+
async def advanced_energy_scan(
237+
app,
238+
output,
239+
num_energy_scans,
240+
num_network_scans,
241+
randomize,
242+
):
243+
import bellows.types
244+
from bellows.zigbee.application import (
245+
ControllerApplication as EzspControllerApplication,
246+
)
247+
from bellows.zigbee.util import map_energy_to_rssi as ezsp_map_energy_to_rssi
248+
236249
await app.startup()
237250
LOGGER.info("Running scan...")
238251

239252
channels = zigpy.types.Channels.ALL_CHANNELS
240-
scan_counts = {channel: num_scans for channel in channels}
253+
scan_counts = {channel: num_energy_scans for channel in channels}
254+
255+
scan_data = {
256+
"energy_scan": [],
257+
"network_scan": [],
258+
}
241259

242260
if randomize:
243261

@@ -260,40 +278,63 @@ def iter_channels():
260278

261279
with click.progressbar(
262280
iterable=iter_channels(),
263-
length=len(list(channels)) * num_scans,
281+
length=len(list(channels)) * num_energy_scans,
264282
item_show_func=lambda item: None if item is None else f"Channel {item}",
265283
) as bar:
266-
output.write("Timestamp,Channel,Energy\n")
267-
268284
for channel in bar:
269285
results = await app.energy_scan(
270286
channels=zigpy.types.Channels.from_channel_list([channel]),
271287
duration_exp=0,
272288
count=1,
273289
)
274290

275-
energy = results[channel]
276-
timestamp = time.time()
277-
output.write(f"{timestamp:0.4f},{channel},{energy:0.4f}\n")
291+
rssi = None
278292

279-
import bellows.types
280-
from bellows.zigbee.application import (
281-
ControllerApplication as EzspControllerApplication,
282-
)
293+
if isinstance(app, EzspControllerApplication):
294+
rssi = ezsp_map_energy_to_rssi(results[channel])
283295

284-
if not isinstance(app, EzspControllerApplication):
285-
return
296+
scan_data["energy_scan"].append(
297+
{
298+
"timestamp": time.time(),
299+
"channel": channel,
300+
"energy": results[channel],
301+
"rssi": rssi,
302+
}
303+
)
286304

287305
await asyncio.sleep(1)
288306
for channel in channels:
289-
networks = await app._ezsp.startScan(
290-
scanType=bellows.types.EzspNetworkScanType.ACTIVE_SCAN,
291-
channelMask=zigpy.types.Channels.from_channel_list([channel]),
292-
duration=6,
293-
)
294-
295-
for network, lqi, rssi in networks:
296-
print(f"Found network {network}: LQI={lqi}, RSSI={rssi}")
307+
print(f"Scanning for networks on channel {channel}")
308+
networks = set()
309+
310+
for attempt in range(num_network_scans):
311+
networks_scan = await app._ezsp.startScan(
312+
scanType=bellows.types.EzspNetworkScanType.ACTIVE_SCAN,
313+
channelMask=zigpy.types.Channels.from_channel_list([channel]),
314+
duration=6,
315+
)
316+
networks_scan = tuple(
317+
[(network.freeze(), lqi, rssi) for network, lqi, rssi in networks_scan]
318+
)
319+
new_networks = set(networks_scan) - networks
320+
321+
for network, lqi, rssi in new_networks:
322+
print(f"Found network {network}: LQI={lqi}, RSSI={rssi}")
323+
scan_data["network_scan"].append(
324+
{
325+
"channel": channel,
326+
"lqi": lqi,
327+
"rssi": rssi,
328+
"network": {
329+
**network.as_dict(),
330+
"extendedPanId": str(network.extendedPanId),
331+
},
332+
}
333+
)
334+
335+
networks.update(new_networks)
336+
337+
json.dump(scan_data, output, separators=(",", ":"))
297338

298339

299340
@radio.command()

0 commit comments

Comments
 (0)