Skip to content

Commit 54ec10f

Browse files
authored
Utilize zigpy energy scanning API (#31)
* Implement energy scanning using the new zigpy API * Disable bellows UART thread * Bump minimum package versions
1 parent d380e1f commit 54ec10f

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

pyproject.toml

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ dependencies = [
1717
"click",
1818
"coloredlogs",
1919
"scapy",
20-
"zigpy>=0.53.0",
21-
"bellows>=0.34.3",
22-
"zigpy-deconz>=0.18.0",
23-
"zigpy-znp>=0.8.0",
20+
"zigpy>=0.54.0",
21+
"bellows>=0.35.0",
22+
"zigpy-deconz>=0.12.0",
23+
"zigpy-znp>=0.10.0",
24+
"zigpy-xbee>=0.17.0",
2425
]
2526

2627
[tool.setuptools.packages.find]

zigpy_cli/radio.py

+8-19
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import zigpy.zdo.types
1616

1717
from zigpy_cli.cli import cli, click_coroutine
18-
from zigpy_cli.common import HEX_OR_DEC_INT
1918
from zigpy_cli.const import RADIO_LOGGING_CONFIGS, RADIO_TO_PACKAGE, RADIO_TO_PYPI
2019

2120
LOGGER = logging.getLogger(__name__)
@@ -55,7 +54,9 @@ async def radio(ctx, radio, port, baudrate=None, database=None):
5554
{
5655
"device": {"path": port},
5756
"backup_enabled": False,
57+
"startup_energy_scan": False,
5858
"database_path": database,
59+
"use_thread": False,
5960
}
6061
)
6162

@@ -171,45 +172,33 @@ async def permit(app, join_time):
171172

172173
@radio.command()
173174
@click.pass_obj
174-
@click.option("--nwk", type=HEX_OR_DEC_INT, default=0x0000)
175175
@click.option("-n", "--num-scans", type=int, default=-1)
176176
@click_coroutine
177-
async def energy_scan(app, nwk, num_scans):
177+
async def energy_scan(app, num_scans):
178178
await app.startup()
179179
LOGGER.info("Running scan...")
180180

181-
# Temporarily create a zigpy device for scans not using the coordinator itself
182-
if nwk != 0x0000:
183-
app.add_device(
184-
nwk=nwk,
185-
ieee=zigpy.types.EUI64.convert("AA:AA:AA:AA:AA:AA:AA:AA"),
186-
)
187-
188181
# We compute an average over the last 5 scans
189182
channel_energies = collections.defaultdict(lambda: collections.deque([], maxlen=5))
190183

191184
for scan in itertools.count():
192185
if num_scans != -1 and scan > num_scans:
193186
break
194187

195-
rsp = await app.get_device(nwk=nwk).zdo.Mgmt_NWK_Update_req(
196-
zigpy.zdo.types.NwkUpdate(
197-
ScanChannels=zigpy.types.Channels.ALL_CHANNELS,
198-
ScanDuration=0x02,
199-
ScanCount=1,
200-
)
188+
results = await app.energy_scan(
189+
channels=zigpy.types.Channels.ALL_CHANNELS, duration_exp=2, count=1
201190
)
202191

203-
_, scanned_channels, _, _, energy_values = rsp
204-
205-
for channel, energy in zip(scanned_channels, energy_values):
192+
for channel, energy in results.items():
206193
energies = channel_energies[channel]
207194
energies.append(energy)
208195

209196
total = 0xFF * len(energies)
210197

211198
print(f"Channel energy (mean of {len(energies)} / {energies.maxlen}):")
212199
print("------------------------------------------------")
200+
print(" ! Different radios compute channel energy differently")
201+
print()
213202
print(" + Lower energy is better")
214203
print(" + Active Zigbee networks on a channel may still cause congestion")
215204
print(" + TX on 26 in North America may be with lower power due to regulations")

0 commit comments

Comments
 (0)