Skip to content

Commit

Permalink
Add system command
Browse files Browse the repository at this point in the history
  • Loading branch information
lhespress committed May 10, 2024
1 parent b6e9dd5 commit 4e7ebd3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
31 changes: 30 additions & 1 deletion zigpy_espzb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ async def connect(self) -> None:
self._uart = await zigpy_espzb.uart.connect(self._config, self)

# TODO: implement a firmware version command
self._firmware_version = await self.system_firmware()
self._network_state = await self.get_network_state()

def connection_lost(self, exc: Exception) -> None:
Expand Down Expand Up @@ -166,6 +167,9 @@ def data_received(self, data: bytes) -> None:
# We won't implement requests for now
assert command.frame_type != FrameType.Request

if schema is None:
return

fut = None

if command.frame_type == FrameType.Response:
Expand Down Expand Up @@ -515,7 +519,7 @@ async def reset(self) -> None:

for attempt in range(5):
try:
await self.form_network()
await self.send_command(commands.SystemResetReq())
except asyncio.TimeoutError:
break
else:
Expand All @@ -524,3 +528,28 @@ async def reset(self) -> None:
await asyncio.sleep(2)

LOGGER.debug("Reset complete")

async def system_factory(self):

LOGGER.debug("Factory...")

await self.send_command(commands.SystemFactoryReq())

await asyncio.sleep(2)

LOGGER.debug("Factory complete")

async def system_firmware(self):
rsp = await self.send_command(commands.SystemFirmwareReq())

return rsp.firmware_version

async def system_model(self):
rsp = await self.send_command(commands.SystemModelReq())

return rsp.payload

async def system_manufacturer(self):
rsp = await self.send_command(commands.SystemManufacturerReq())

return rsp.payload
60 changes: 60 additions & 0 deletions zigpy_espzb/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Bytes,
DeviceType,
ExtendedAddrMode,
FirmwareVersion,
NetworkState,
SecurityMode,
ShiftedChannels,
Expand Down Expand Up @@ -77,6 +78,11 @@ class CommandId(t.enum16):
aps_data_request = 0x0300
aps_data_indication = 0x0301
aps_data_confirm = 0x0302
system_reset = 0x0400
system_factory = 0x0401
system_firmware = 0x0402
system_model = 0x0403
system_manufacturer = 0x0404


class FrameType(t.enum4):
Expand Down Expand Up @@ -506,6 +512,35 @@ class SecurityModeSetReq(BaseCommand):
class SecurityModeSetRsp(BaseCommand):
status: Status

class SystemResetReq(BaseCommand):
pass

class SystemResetRsp(BaseCommand):
status: Status

class SystemFactoryReq(BaseCommand):
pass

class SystemFactoryRsp(BaseCommand):
status: Status

class SystemFirmwareReq(BaseCommand):
pass

class SystemFirmwareRsp(BaseCommand):
firmware_version: FirmwareVersion

class SystemModelReq(BaseCommand):
pass

class SystemModelRsp(BaseCommand):
payload: t.CharacterString

class SystemManufacturerReq(BaseCommand):
pass

class SystemManufacturerRsp(BaseCommand):
payload: t.CharacterString

COMMAND_SCHEMAS = {
CommandId.network_init: (
Expand Down Expand Up @@ -698,6 +733,31 @@ class SecurityModeSetRsp(BaseCommand):
SecurityModeSetRsp,
None,
),
CommandId.system_reset: (
SystemResetReq,
SystemResetRsp,
None,
),
CommandId.system_factory: (
SystemFactoryReq,
SystemFactoryRsp,
None,
),
CommandId.system_firmware: (
SystemFirmwareReq,
SystemFirmwareRsp,
None,
),
CommandId.system_model: (
SystemModelReq,
SystemModelRsp,
None,
),
CommandId.system_manufacturer: (
SystemManufacturerReq,
SystemManufacturerRsp,
None,
),
}

COMMAND_SCHEMA_TO_COMMAND_ID = {
Expand Down
4 changes: 2 additions & 2 deletions zigpy_espzb/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ async def load_network_info(self, *, load_devices=False):
node_info.ieee = await self._api.get_mac_address()

# TODO: implement firmware commands to read the board name, manufacturer
node_info.manufacturer = "Espressif Systems"
node_info.model = "ESP32H2"
node_info.manufacturer = await self._api.system_manufacturer()
node_info.model = await self._api.system_model()

# TODO: implement firmware command to read out the firmware version and build ID
node_info.version = f"{int(self._api.firmware_version):#010x}"
Expand Down

0 comments on commit 4e7ebd3

Please sign in to comment.