Skip to content

Commit 413f98d

Browse files
committed
Replace port_path with znp in tools that can run independently
1 parent 8defad3 commit 413f98d

File tree

6 files changed

+46
-47
lines changed

6 files changed

+46
-47
lines changed

zigpy_znp/tools/flash_read.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@
1313
LOGGER = logging.getLogger(__name__)
1414

1515

16-
async def read_firmware(radio_path: str) -> bytearray:
17-
znp = ZNP(
18-
CONFIG_SCHEMA(
19-
{"znp_config": {"skip_bootloader": False}, "device": {"path": radio_path}}
20-
)
21-
)
22-
23-
# The bootloader handshake must be the very first command
24-
await znp.connect(test_port=False)
25-
16+
async def read_firmware(znp: ZNP) -> bytearray:
2617
try:
2718
async with async_timeout.timeout(5):
2819
handshake_rsp = await znp.request_callback_rsp(
@@ -60,8 +51,6 @@ async def read_firmware(radio_path: str) -> bytearray:
6051

6152
data.extend(read_rsp.Data)
6253

63-
znp.close()
64-
6554
return data
6655

6756

@@ -76,7 +65,19 @@ async def main(argv):
7665
)
7766

7867
args = parser.parse_args(argv)
79-
data = await read_firmware(args.serial)
68+
69+
znp = ZNP(
70+
CONFIG_SCHEMA(
71+
{"znp_config": {"skip_bootloader": False}, "device": {"path": args.serial}}
72+
)
73+
)
74+
75+
# The bootloader handshake must be the very first command
76+
await znp.connect(test_port=False)
77+
78+
data = await read_firmware(znp)
79+
znp.close()
80+
8081
args.output.write(data)
8182

8283
LOGGER.info("Unplug your adapter to leave bootloader mode!")

zigpy_znp/tools/flash_write.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def get_firmware_crcs(firmware: bytes) -> tuple[int, int]:
5555
return real_crc, compute_crc16(firmware_without_crcs)
5656

5757

58-
async def write_firmware(firmware: bytes, radio_path: str, reset_nvram: bool):
58+
async def write_firmware(znp: ZNP, firmware: bytes, reset_nvram: bool):
5959
if len(firmware) != c.ubl.IMAGE_SIZE:
6060
raise ValueError(
6161
f"Firmware is the wrong size."
@@ -70,15 +70,6 @@ async def write_firmware(firmware: bytes, radio_path: str, reset_nvram: bool):
7070
f" Expected 0x{expected_crc:04X}, got 0x{computed_crc:04X}"
7171
)
7272

73-
znp = ZNP(
74-
CONFIG_SCHEMA(
75-
{"znp_config": {"skip_bootloader": False}, "device": {"path": radio_path}}
76-
)
77-
)
78-
79-
# The bootloader handshake must be the very first command
80-
await znp.connect(test_port=False)
81-
8273
try:
8374
async with async_timeout.timeout(5):
8475
handshake_rsp = await znp.request_callback_rsp(
@@ -146,8 +137,6 @@ async def write_firmware(firmware: bytes, radio_path: str, reset_nvram: bool):
146137
else:
147138
LOGGER.info("Unplug your adapter to leave bootloader mode!")
148139

149-
znp.close()
150-
151140

152141
async def main(argv):
153142
parser = setup_parser("Write firmware to a radio")
@@ -168,7 +157,18 @@ async def main(argv):
168157

169158
args = parser.parse_args(argv)
170159

171-
await write_firmware(args.input.read(), args.serial, args.reset)
160+
znp = ZNP(
161+
CONFIG_SCHEMA(
162+
{"znp_config": {"skip_bootloader": False}, "device": {"path": args.serial}}
163+
)
164+
)
165+
166+
# The bootloader handshake must be the very first command
167+
await znp.connect(test_port=False)
168+
169+
await write_firmware(znp=znp, firmware=args.input.read(), reset_nvram=args.reset)
170+
171+
znp.close()
172172

173173

174174
if __name__ == "__main__":

zigpy_znp/tools/form_network.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async def form_network(radio_path):
1414
config = ControllerApplication.SCHEMA({"device": {"path": radio_path}})
1515
app = ControllerApplication(config)
1616

17-
await app.startup(auto_form=True)
17+
await app.startup(force_form=True)
1818
await app.shutdown()
1919

2020

zigpy_znp/tools/network_backup.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
LOGGER = logging.getLogger(__name__)
1919

2020

21-
async def backup_network(radio_path: str) -> t.JSONType:
22-
znp = ZNP(ControllerApplication.SCHEMA({"device": {"path": radio_path}}))
23-
await znp.connect()
24-
21+
async def backup_network(znp: ZNP) -> t.JSONType:
2522
try:
2623
await znp.load_network_info()
2724
except ValueError as e:
@@ -81,8 +78,6 @@ async def backup_network(radio_path: str) -> t.JSONType:
8178

8279
obj["stack_specific"] = {"zstack": {"tclk_seed": tclk_seed.hex()}}
8380

84-
znp.close()
85-
8681
# Ensure our generated backup is valid
8782
validate_backup_json(obj)
8883

@@ -96,9 +91,11 @@ async def main(argv: list[str]) -> None:
9691
)
9792
args = parser.parse_args(argv)
9893

99-
backup_obj = await backup_network(
100-
radio_path=args.serial,
101-
)
94+
znp = ZNP(ControllerApplication.SCHEMA({"device": {"path": args.serial}}))
95+
await znp.connect()
96+
97+
backup_obj = await backup_network(znp)
98+
znp.close()
10299

103100
args.output.write(json.dumps(backup_obj, indent=4))
104101

zigpy_znp/tools/nvram_read.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
LOGGER = logging.getLogger(__name__)
1515

1616

17-
async def nvram_read(radio_path: str):
18-
znp = ZNP(CONFIG_SCHEMA({"device": {"path": radio_path}}))
19-
await znp.connect()
20-
17+
async def nvram_read(znp: ZNP):
2118
data = {}
2219
data["LEGACY"] = {}
2320

@@ -89,8 +86,12 @@ async def main(argv):
8986

9087
args = parser.parse_args(argv)
9188

92-
obj = await nvram_read(args.serial)
89+
znp = ZNP(CONFIG_SCHEMA({"device": {"path": args.serial}}))
90+
await znp.connect()
91+
92+
obj = await nvram_read(znp)
9393
args.output.write(json.dumps(obj, indent=4) + "\n")
94+
znp.close()
9495

9596

9697
if __name__ == "__main__":

zigpy_znp/tools/nvram_write.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
LOGGER = logging.getLogger(__name__)
1313

1414

15-
async def nvram_write(radio_path, backup):
16-
znp = ZNP(CONFIG_SCHEMA({"device": {"path": radio_path}}))
17-
18-
await znp.connect()
19-
15+
async def nvram_write(znp: ZNP, backup):
2016
# First write the NVRAM items common to all radios
2117
for nwk_nvid, value in backup["LEGACY"].items():
2218
if "+" in nwk_nvid:
@@ -58,7 +54,11 @@ async def main(argv):
5854

5955
args = parser.parse_args(argv)
6056
backup = json.load(args.input)
61-
await nvram_write(args.serial, backup)
57+
58+
znp = ZNP(CONFIG_SCHEMA({"device": {"path": args.serial}}))
59+
await znp.connect()
60+
61+
await nvram_write(znp, backup)
6262

6363

6464
if __name__ == "__main__":

0 commit comments

Comments
 (0)