|
9 | 9 | import bellows.ezsp |
10 | 10 | import bellows.types |
11 | 11 | from zigpy.serial import SerialProtocol |
| 12 | +import zigpy.types |
12 | 13 |
|
13 | 14 | from .common import ( |
14 | 15 | PROBE_TIMEOUT, |
@@ -162,10 +163,18 @@ async def probe_spinel(self, baudrate: int) -> ProbeResult: |
162 | 163 | async def probe_app_type( |
163 | 164 | self, |
164 | 165 | types: typing.Iterable[ApplicationType] | None = None, |
| 166 | + try_first: tuple[ApplicationType, ...] = (), |
165 | 167 | ) -> None: |
166 | 168 | if types is None: |
167 | 169 | types = self._probe_methods |
168 | 170 |
|
| 171 | + # fmt: off |
| 172 | + types = ( |
| 173 | + [m for m in types if m in try_first] |
| 174 | + + [m for m in types if m not in try_first] |
| 175 | + ) |
| 176 | + # fmt: on |
| 177 | + |
169 | 178 | # Reset into bootloader |
170 | 179 | if self._reset_target: |
171 | 180 | await self.enter_bootloader_reset(self._reset_target) |
@@ -205,6 +214,8 @@ async def probe_app_type( |
205 | 214 | except asyncio.TimeoutError: |
206 | 215 | continue |
207 | 216 |
|
| 217 | + _LOGGER.debug("Probe result: %s", result) |
| 218 | + |
208 | 219 | # Keep track of the bootloader version for later |
209 | 220 | if probe_method == ApplicationType.GECKO_BOOTLOADER: |
210 | 221 | _LOGGER.info("Detected bootloader version %s", result.version) |
@@ -307,30 +318,25 @@ async def dump_emberznet_config(self) -> None: |
307 | 318 | continue |
308 | 319 | print(f"{config.name}={v[1]}") |
309 | 320 |
|
310 | | - async def write_emberznet_eui64(self, new_eui64: bellows.types.EUI64) -> bool: |
311 | | - await self.probe_app_type() |
| 321 | + async def write_emberznet_eui64( |
| 322 | + self, new_ieee: zigpy.types.EUI64, force: bool = False |
| 323 | + ) -> bool: |
| 324 | + await self.probe_app_type( |
| 325 | + try_first=[ApplicationType.GECKO_BOOTLOADER, ApplicationType.EZSP] |
| 326 | + ) |
312 | 327 |
|
313 | 328 | if self.app_type != ApplicationType.EZSP: |
314 | 329 | raise RuntimeError(f"Device is not running EmberZNet: {self.app_type}") |
315 | 330 |
|
316 | 331 | async with self._connect_ezsp(self.app_baudrate) as ezsp: |
317 | | - (current_eui64,) = await ezsp.getEui64() |
318 | | - _LOGGER.info("Current device IEEE: %s", current_eui64) |
| 332 | + (current_ieee,) = await ezsp.getEui64() |
| 333 | + _LOGGER.info("Current device IEEE: %s", current_ieee) |
319 | 334 |
|
320 | | - if current_eui64 == new_eui64: |
| 335 | + if current_ieee == new_ieee: |
321 | 336 | _LOGGER.info("Device IEEE address already matches, not overwriting") |
322 | 337 | return False |
323 | 338 |
|
324 | | - if not await ezsp.can_write_custom_eui64(): |
325 | | - raise ValueError( |
326 | | - "IEEE address has already been written, it cannot be written again" |
327 | | - ) |
328 | | - |
329 | | - (status,) = await ezsp.setMfgToken( |
330 | | - bellows.types.EzspMfgTokenId.MFG_CUSTOM_EUI_64, new_eui64.serialize() |
331 | | - ) |
332 | | - |
333 | | - if status != bellows.types.EmberStatus.SUCCESS: |
334 | | - raise RuntimeError(f"Failed to write IEEE address: {status}") |
| 339 | + await ezsp.write_custom_eui64(ieee=new_ieee, burn_into_userdata=force) |
| 340 | + _LOGGER.info("Wrote new device IEEE: %s", new_ieee) |
335 | 341 |
|
336 | 342 | return True |
0 commit comments