Skip to content

Commit

Permalink
Handle getTokenData command missing (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly authored Jun 30, 2023
1 parent cb3d4f8 commit 7f26d82
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
4 changes: 4 additions & 0 deletions bellows/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ class EzspError(APIException):
pass


class InvalidCommandError(EzspError):
pass


class ControllerError(ControllerException):
pass
15 changes: 7 additions & 8 deletions bellows/ezsp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import zigpy.config

import bellows.config as conf
from bellows.exception import EzspError
from bellows.exception import EzspError, InvalidCommandError
import bellows.types as t
import bellows.uart

Expand Down Expand Up @@ -374,17 +374,16 @@ async def get_board_info(self) -> tuple[str, str, str]:

async def _get_nv3_restored_eui64_key(self) -> t.NV3KeyId | None:
"""Get the NV3 key for the device's restored EUI64, if one exists."""
try:
# If the EZSP version doesn't have `getTokenData`, it doesn't implement NV3
self.getTokenData
except AttributeError:
return None

for key in (
t.NV3KeyId.CREATOR_STACK_RESTORED_EUI64, # NCP firmware
t.NV3KeyId.NVM3KEY_STACK_RESTORED_EUI64, # RCP firmware
):
status, data = await self.getTokenData(key, 0)
try:
status, data = await self.getTokenData(key, 0)
except (InvalidCommandError, AttributeError):
# Either the command doesn't exist in the EZSP version, or the command
# is not implemented in the firmware
return None

if status == t.EmberStatus.SUCCESS:
nv3_restored_eui64, _ = t.EmberEUI64.deserialize(data)
Expand Down
4 changes: 2 additions & 2 deletions bellows/ezsp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from asyncio import timeout as asyncio_timeout # pragma: no cover

from bellows.config import CONF_EZSP_CONFIG, CONF_EZSP_POLICIES
from bellows.exception import EzspError
from bellows.exception import InvalidCommandError
from bellows.typing import GatewayType

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -192,7 +192,7 @@ def __call__(self, data: bytes) -> None:
if frame_name == "invalidCommand":
sent_cmd_name = self.COMMANDS_BY_ID[expected_id][0]
future.set_exception(
EzspError(
InvalidCommandError(
f"{sent_cmd_name} command is an {frame_name}, was sent "
f"under {sequence} sequence number: {result[0].name}"
)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_ezsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,11 @@ async def _mock_cmd(*args, **kwargs):

@pytest.mark.parametrize(
"value, expected_result",
[(b"\xFF" * 8, True), (bytes.fromhex("0846b8a11c004b1200"), False), (b"", False)],
[
(b"\xFF" * 8, True),
(bytes.fromhex("0846b8a11c004b1200"), False),
(b"", False),
],
)
async def test_can_burn_userdata_custom_eui64(ezsp_f, value, expected_result):
"""Test detecting if a custom EUI64 has been written."""
Expand Down

0 comments on commit 7f26d82

Please sign in to comment.