Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

New radio api #7

Merged
merged 22 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]
python-version: ["3.7", "3.8", "3.9", "3.10"]

steps:
- uses: actions/checkout@v2
Expand All @@ -34,7 +34,7 @@ jobs:
flake8 . --count --exit-zero --max-complexity=16 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
pytest --asyncio-mode=auto

markdownlint:

Expand Down
29 changes: 22 additions & 7 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
import zigpy.types as zigpy_types
import zigpy.exceptions

import zigpy_zigate.config as config
import zigpy_zigate.types as t
Expand Down Expand Up @@ -31,7 +32,7 @@ def test_zigpy_ieee(app):
data = b"\x01\x02\x03\x04\x05\x06\x07\x08"

zigate_ieee, _ = t.EUI64.deserialize(data)
app._ieee = zigpy_types.EUI64(zigate_ieee)
app.state.node_info.ieee = zigpy_types.EUI64(zigate_ieee)

dst_addr = app.get_dst_address(cluster)
assert dst_addr.serialize() == b"\x03" + data[::-1] + b"\x01"
Expand All @@ -45,27 +46,41 @@ def test_model_detection(app):
@pytest.mark.asyncio
async def test_form_network_success(app):
app._api.set_channel = AsyncMock()
app._api.set_extended_panid = AsyncMock()
app._api.reset = AsyncMock()

async def mock_start_network():
return [[0x00, 0x1234, 0x0123456789abcdef], 0]
app._api.start_network = mock_start_network

async def mock_get_network_state():
return [[0x0000, 0x0123456789abcdef, 0x1234, 0x1234abcdef012345, 0x11], 0]
app._api.get_network_state = mock_get_network_state

await app.form_network()
assert app._nwk == 0x1234
assert app._ieee == 0x0123456789abcdef
await app.load_network_info()
assert app.state.node_info.nwk == 0x0000
assert app.state.node_info.ieee == zigpy.types.EUI64.convert(
"01:23:45:67:89:ab:cd:ef"
)
assert app.state.network_info.pan_id == 0x1234
assert app.state.network_info.extended_pan_id == zigpy.types.ExtendedPanId.convert(
"12:34:ab:cd:ef:01:23:45"
)
assert app._api.reset.call_count == 0


@pytest.mark.asyncio
async def test_form_network_failed(app):
app._api.set_channel = AsyncMock()
app._api.set_extended_panid = AsyncMock()
app._api.reset = AsyncMock()
async def mock_start_network():
return [[0x06], 0]
app._api.start_network = mock_start_network
async def mock_get_network_state():
return [[0xffff, 0x0123456789abcdef, 0x1234, 0, 0x11], 0]
app._api.get_network_state = mock_get_network_state
await app.form_network()
assert app._nwk == 0
assert app._ieee == 0
assert app._api.reset.call_count == 1

with pytest.raises(zigpy.exceptions.FormationFailure):
await app.form_network()
24 changes: 24 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import binascii
from zigpy_zigate import types as t
from zigpy_zigate.api import RESPONSES, COMMANDS

Expand Down Expand Up @@ -63,6 +64,29 @@ def test_deserialize():
assert result[3] is None
assert len(result) == 4

# Frame received: 8012000a2800010102bc8c73000100
# data received 0x8012 b'00010102bc8c730001'

data = binascii.unhexlify(b'00010102bc8c730001')
schema = RESPONSES[0x8012]
result, rest = t.deserialize(data, schema)
assert result[0] == 0x00
assert result[1] == 0x01
assert result[2] == 0x01
assert result[3] == t.Address(address_mode=t.ADDRESS_MODE.NWK,
address=t.NWK.deserialize(b'\xbc\x8c')[0])
assert result[4] == 0x73
assert len(result) == 5

# Frame received: 99990002828000
# data received 0x9999 b'80' LQI:0

data = binascii.unhexlify(b'80')
schema = RESPONSES[0x9999]
result, rest = t.deserialize(data, schema)
assert result[0] == 0x80
assert len(result) == 1

def test_serialize():
data = [True]
schema = COMMANDS[0x0002]
Expand Down
2 changes: 1 addition & 1 deletion zigpy_zigate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MAJOR_VERSION = 0
MINOR_VERSION = 8
PATCH_VERSION = '0'
PATCH_VERSION = '1'
__short_version__ = '{}.{}'.format(MAJOR_VERSION, MINOR_VERSION)
__version__ = '{}.{}'.format(__short_version__, PATCH_VERSION)
83 changes: 50 additions & 33 deletions zigpy_zigate/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ResponseId(enum.IntEnum):
DEVICE_ANNOUNCE = 0x004D
CONTROLLER_HEARTBEAT = 0x8008
STATUS = 0x8000
CONTROLLER_LOG = 0x8001
LOG_MESSAGE = 0x8001
DATA_INDICATION = 0x8002
PDM_LOADED = 0x0302
NODE_NON_FACTORY_NEW_RESTART = 0x8006
Expand All @@ -58,53 +58,53 @@ class ResponseId(enum.IntEnum):
ROUTE_DISCOVERY_CONFIRM = 0x8701
APS_DATA_CONFIRM_FAILED = 0x8702
AHI_SET_TX_POWER_RSP = 0x8806
CONTROLLER_EXTENDED_ERROR = 0x9999
ZCL_EVENT = 0x9999


RESPONSES = {
# 0x004D: (t.NWK, t.EUI64, t.uint8_t, t.uint8_t),
ResponseId.DEVICE_ANNOUNCE: (t.NWK, t.EUI64, t.uint8_t, t.uint8_t),
# 0x8008: (t.uint32_t,), # Hearbeat : every minute sent by Zigate
ResponseId.CONTROLLER_HEARTBEAT: ( t.uint32_t, ),
# 0x8000: (t.uint8_t, t.uint8_t, t.uint16_t, t.uint8_t, t.uint8_t, t.Bytes),
ResponseId.STATUS: (t.uint8_t, t.uint8_t, t.uint16_t, t.uint8_t, t.uint8_t, t.Bytes),
# 0x8002: (t.uint8_t, t.uint16_t, t.uint16_t, t.uint8_t, t.uint8_t, t.Address, t.Address, t.Bytes),
ResponseId.DATA_INDICATION: ( t.uint8_t, t.uint16_t, t.uint16_t, t.uint8_t, t.uint8_t, t.Address, t.Address, t.Bytes, ),
# 0x8001: (t.Bytes,),
ResponseId.CONTROLLER_LOG: (t.Bytes, ),
# 0x0302: (t.uint8_t,),
ResponseId.DATA_INDICATION: (
t.uint8_t,
t.uint16_t,
t.uint16_t,
t.uint8_t,
t.uint8_t,
t.Address,
t.Address,
t.Bytes,
),
ResponseId.LOG_MESSAGE: (t.Bytes,),
ResponseId.PDM_LOADED: (t.uint8_t,),
ResponseId.NODE_NON_FACTORY_NEW_RESTART: (t.uint8_t,),
ResponseId.NODE_FACTORY_NEW_RESTART: (t.uint8_t,),
ResponseId.NETWORK_STATE_RSP: (t.NWK, t.EUI64, t.uint16_t, t.uint64_t, t.uint8_t),
ResponseId.NETWORK_STATE_RSP: (t.NWK, t.EUI64, t.uint16_t, t.EUI64, t.uint8_t),
ResponseId.VERSION_LIST: (t.uint16_t, t.uint16_t),
# 0x8011: (t.uint8_t, t.NWK, t.uint8_t, t.uint16_t, t.uint8_t),
ResponseId.ACK_DATA: (t.uint8_t, t.NWK, t.uint8_t, t.uint16_t, t.uint8_t),
# 0x8012: (t.uint8_t, t.uint8_t, t.uint8_t, t.Address, t.uint8_t, t.uint8_t), # DATA confirmed
ResponseId.APS_DATA_CONFIRM: ( t.uint8_t, t.uint8_t, t.uint8_t, t.Address, t.uint8_t, t.uint8_t ),
ResponseId.APS_DATA_CONFIRM: (
t.uint8_t,
t.uint8_t,
t.uint8_t,
t.Address,
t.uint8_t,
t.uint8_t,
),
ResponseId.GET_TIMESERVER_LIST: (t.uint32_t,),
ResponseId.NETWORK_JOINED_FORMED: (t.uint8_t, t.NWK, t.EUI64, t.uint8_t),
ResponseId.PDM_EVENT: (t.uint8_t, t.uint32_t),
ResponseId.LEAVE_INDICATION: (t.EUI64, t.uint8_t),
# 0x8701: (t.uint8_t, t.uint8_t),
ResponseId.ROUTE_DISCOVERY_CONFIRM: (t.uint8_t, t.uint8_t),
# 0x8702: (t.uint8_t, t.uint8_t, t.uint8_t, t.Address, t.uint8_t, t.uint8_t), # Data not confirmed
ResponseId.APS_DATA_CONFIRM_FAILED: (t.uint8_t, t.uint8_t, t.uint8_t, t.Address, t.uint8_t, t.uint8_t ),
# 0x8806: (t.uint8_t,),
ResponseId.APS_DATA_CONFIRM_FAILED: (
t.uint8_t,
t.uint8_t,
t.uint8_t,
t.Address,
t.uint8_t,
t.uint8_t,
),
ResponseId.AHI_SET_TX_POWER_RSP: (t.uint8_t,),
# 0x9999: (t.uint8_t,),
ResponseId.CONTROLLER_EXTENDED_ERROR: (t.uint8_t,),

# 0x8006: (t.uint8_t,),
# 0x8007: (t.uint8_t,),
# 0x8009: (t.NWK, t.EUI64, t.uint16_t, t.EUI64, t.uint8_t),
# 0x8010: (t.uint16_t, t.uint16_t),
# 0x8017: (t.uint32_t,),
# 0x8024: (t.uint8_t, t.NWK, t.EUI64, t.uint8_t),
# 0x8035: (t.uint8_t, t.uint32_t),
# 0x8048: (t.EUI64, t.uint8_t),


ResponseId.ZCL_EVENT: (t.uint8_t,),
}

COMMANDS = {
Expand All @@ -116,8 +116,25 @@ class ResponseId(enum.IntEnum):
CommandId.SET_CHANNELMASK: (t.uint32_t,),
CommandId.NETWORK_REMOVE_DEVICE: (t.EUI64, t.EUI64),
CommandId.PERMIT_JOINING_REQUEST: (t.NWK, t.uint8_t, t.uint8_t),
CommandId.MANAGEMENT_NETWORK_UPDATE_REQUEST: ( t.NWK, t.uint32_t, t.uint8_t, t.uint8_t, t.uint8_t, t.uint16_t, ),
CommandId.SEND_RAW_APS_DATA_PACKET: ( t.uint8_t, t.NWK, t.uint8_t, t.uint8_t, t.uint16_t, t.uint16_t, t.uint8_t, t.uint8_t, t.LBytes, ),
CommandId.MANAGEMENT_NETWORK_UPDATE_REQUEST: (
t.NWK,
t.uint32_t,
t.uint8_t,
t.uint8_t,
t.uint8_t,
t.uint16_t,
),
CommandId.SEND_RAW_APS_DATA_PACKET: (
t.uint8_t,
t.NWK,
t.uint8_t,
t.uint8_t,
t.uint16_t,
t.uint16_t,
t.uint8_t,
t.uint8_t,
t.LBytes,
),
CommandId.AHI_SET_TX_POWER: (t.uint8_t,),
}

Expand Down
Loading