Skip to content

Commit

Permalink
Uppercase SYS commands
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed May 16, 2020
1 parent 7916e25 commit 87dd436
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 145 deletions.
154 changes: 77 additions & 77 deletions tests/test_api.py

Large diffs are not rendered by default.

52 changes: 26 additions & 26 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# We just respond to pings, nothing more
self.callback_for_response(c.Sys.Ping.Req(), lambda r: self.ping_replier(r))
self.callback_for_response(c.SYS.Ping.Req(), lambda r: self.ping_replier(r))

def reply_once_to(self, request, responses):
called_future = asyncio.get_running_loop().create_future()
Expand Down Expand Up @@ -101,7 +101,7 @@ async def callback(request):
return callback

def ping_replier(self, request):
self.send(c.Sys.Ping.Rsp(Capabilities=t.MTCapabilities(1625)))
self.send(c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities(1625)))

def send(self, response):
self._uart.send(response.to_frame())
Expand Down Expand Up @@ -146,9 +146,9 @@ def application(znp_server):

# Handle the entire startup sequence
znp_server.reply_to(
request=c.Sys.ResetReq.Req(Type=t.ResetType.Soft),
request=c.SYS.ResetReq.Req(Type=t.ResetType.Soft),
responses=[
c.Sys.ResetInd.Callback(
c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=2,
ProductId=1,
Expand Down Expand Up @@ -233,13 +233,13 @@ def on_endpoint_deletion(req):
NwkNvIds.NWK_CHILD_AGE_ENABLE,
]:
znp_server.reply_to(
request=c.Sys.OSALNVWrite.Req(Id=nvid, Offset=0, partial=True),
responses=[c.Sys.OSALNVWrite.Rsp(Status=t.Status.SUCCESS)],
request=c.SYS.OSALNVWrite.Req(Id=nvid, Offset=0, partial=True),
responses=[c.SYS.OSALNVWrite.Rsp(Status=t.Status.SUCCESS)],
)

znp_server.reply_to(
request=c.Sys.OSALNVRead.Req(Id=NwkNvIds.HAS_CONFIGURED_ZSTACK3, Offset=0),
responses=[c.Sys.OSALNVRead.Rsp(Status=t.Status.SUCCESS, Value=b"\x55")],
request=c.SYS.OSALNVRead.Req(Id=NwkNvIds.HAS_CONFIGURED_ZSTACK3, Offset=0),
responses=[c.SYS.OSALNVRead.Rsp(Status=t.Status.SUCCESS, Value=b"\x55")],
)

znp_server.reply_to(
Expand Down Expand Up @@ -284,20 +284,20 @@ async def test_application_startup_failure(application):
app, znp_server = application

# Prevent the fixture's default response
znp_server._response_listeners[c.Sys.OSALNVRead.Req.header].clear()
znp_server._response_listeners[c.SYS.OSALNVRead.Req.header].clear()

znp_server.reply_once_to(
request=c.Sys.OSALNVRead.Req(Id=NwkNvIds.HAS_CONFIGURED_ZSTACK3, Offset=0),
responses=[c.Sys.OSALNVRead.Rsp(Status=t.Status.INVALID_PARAMETER, Value=b"")],
request=c.SYS.OSALNVRead.Req(Id=NwkNvIds.HAS_CONFIGURED_ZSTACK3, Offset=0),
responses=[c.SYS.OSALNVRead.Rsp(Status=t.Status.INVALID_PARAMETER, Value=b"")],
)

# We cannot start the application if Z-Stack is not configured and without auto_form
with pytest.raises(RuntimeError):
await app.startup(auto_form=False)

znp_server.reply_once_to(
request=c.Sys.OSALNVRead.Req(Id=NwkNvIds.HAS_CONFIGURED_ZSTACK3, Offset=0),
responses=[c.Sys.OSALNVRead.Rsp(Status=t.Status.SUCCESS, Value=b"\x00")],
request=c.SYS.OSALNVRead.Req(Id=NwkNvIds.HAS_CONFIGURED_ZSTACK3, Offset=0),
responses=[c.SYS.OSALNVRead.Rsp(Status=t.Status.SUCCESS, Value=b"\x00")],
)

with pytest.raises(RuntimeError):
Expand All @@ -309,8 +309,8 @@ async def test_application_startup_tx_power(application):
app, znp_server = application

set_tx_power = znp_server.reply_once_to(
request=c.Sys.SetTxPower.Req(TXPower=19),
responses=[c.Sys.SetTxPower.Rsp(Status=t.Status.SUCCESS)],
request=c.SYS.SetTxPower.Req(TXPower=19),
responses=[c.SYS.SetTxPower.Rsp(Status=t.Status.SUCCESS)],
)

app.update_config({conf.CONF_ZNP_CONFIG: {conf.CONF_TX_POWER: 19}})
Expand Down Expand Up @@ -818,10 +818,10 @@ async def test_update_network(mocker, caplog, application):
)

set_extended_pan_id = znp_server.reply_once_to(
request=c.Sys.OSALNVWrite.Req(
request=c.SYS.OSALNVWrite.Req(
Id=NwkNvIds.EXTENDED_PAN_ID, Offset=0, Value=extended_pan_id.serialize()
),
responses=[c.Sys.OSALNVWrite.Rsp(Status=t.Status.SUCCESS)],
responses=[c.SYS.OSALNVWrite.Rsp(Status=t.Status.SUCCESS)],
)

set_network_key_util = znp_server.reply_once_to(
Expand All @@ -830,10 +830,10 @@ async def test_update_network(mocker, caplog, application):
)

set_network_key_nvram = znp_server.reply_once_to(
request=c.Sys.OSALNVWrite.Req(
request=c.SYS.OSALNVWrite.Req(
Id=NwkNvIds.PRECFGKEYS_ENABLE, Offset=0, Value=t.Bool(True).serialize()
),
responses=[c.Sys.OSALNVWrite.Rsp(Status=t.Status.SUCCESS)],
responses=[c.SYS.OSALNVWrite.Rsp(Status=t.Status.SUCCESS)],
)

# But it does succeed with a warning if you explicitly allow it
Expand Down Expand Up @@ -944,27 +944,27 @@ async def test_auto_form_necessary(application, mocker):
def nvram_writer(req):
nvram[req.Id] = req.Value

return c.Sys.OSALNVWrite.Rsp(Status=t.Status.SUCCESS)
return c.SYS.OSALNVWrite.Rsp(Status=t.Status.SUCCESS)

def nvram_init(req):
nvram[req.Id] = req.Value

return c.Sys.OSALNVItemInit.Rsp(Status=t.Status.SUCCESS)
return c.SYS.OSALNVItemInit.Rsp(Status=t.Status.SUCCESS)

# Prevent the fixture's default response
znp_server._response_listeners[c.Sys.OSALNVRead.Req.header].clear()
znp_server._response_listeners[c.SYS.OSALNVRead.Req.header].clear()

read_zstack_configured = znp_server.reply_once_to(
request=c.Sys.OSALNVRead.Req(Id=NwkNvIds.HAS_CONFIGURED_ZSTACK3, Offset=0),
responses=[c.Sys.OSALNVRead.Rsp(Status=t.Status.INVALID_PARAMETER, Value=b"")],
request=c.SYS.OSALNVRead.Req(Id=NwkNvIds.HAS_CONFIGURED_ZSTACK3, Offset=0),
responses=[c.SYS.OSALNVRead.Rsp(Status=t.Status.INVALID_PARAMETER, Value=b"")],
)

znp_server.reply_to(
request=c.Sys.OSALNVWrite.Req(Offset=0, partial=True), responses=[nvram_writer]
request=c.SYS.OSALNVWrite.Req(Offset=0, partial=True), responses=[nvram_writer]
)

znp_server.reply_to(
request=c.Sys.OSALNVItemInit.Req(partial=True), responses=[nvram_init]
request=c.SYS.OSALNVItemInit.Req(partial=True), responses=[nvram_init]
)

znp_server.reply_to(
Expand Down
50 changes: 25 additions & 25 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,22 @@ def test_commands_schema():

def test_command_param_binding():
# No params
c.Sys.Ping.Req()
c.SYS.Ping.Req()

# Invalid param name
with pytest.raises(KeyError):
c.Sys.Ping.Rsp(asd=123)
c.SYS.Ping.Rsp(asd=123)

# Valid param name
c.Sys.Ping.Rsp(Capabilities=t.MTCapabilities.CAP_SYS)
c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.CAP_SYS)

# Too many params, one valid
with pytest.raises(KeyError):
c.Sys.Ping.Rsp(foo="asd", Capabilities=t.MTCapabilities.CAP_SYS)
c.SYS.Ping.Rsp(foo="asd", Capabilities=t.MTCapabilities.CAP_SYS)

# Not enough params
with pytest.raises(KeyError):
c.Sys.Ping.Rsp()
c.SYS.Ping.Rsp()

# Invalid type
with pytest.raises(ValueError):
Expand All @@ -198,18 +198,18 @@ def test_command_param_binding():
assert t.MTCapabilities.CAP_SYS == 0x0001

with pytest.raises(ValueError):
c.Sys.Ping.Rsp(Capabilities=0x0001)
c.SYS.Ping.Rsp(Capabilities=0x0001)

# Parameters can be looked up by name
ping_rsp = c.Sys.Ping.Rsp(Capabilities=t.MTCapabilities.CAP_SYS)
ping_rsp = c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.CAP_SYS)
assert ping_rsp.Capabilities == t.MTCapabilities.CAP_SYS

# Invalid ones cannot
with pytest.raises(AttributeError):
ping_rsp.Oops

# bytes are converted into t.ShortBytes
cmd = c.Sys.NVWrite.Req(
cmd = c.SYS.NVWrite.Req(
SysId=0x12, ItemId=0x3456, SubId=0x7890, Offset=0x0000, Value=b"asdfoo"
)
assert isinstance(cmd.Value, t.ShortBytes)
Expand Down Expand Up @@ -281,11 +281,11 @@ def test_command_str_repr():


def test_command_immutability():
command1 = c.Sys.NVWrite.Req(
command1 = c.SYS.NVWrite.Req(
partial=True, SysId=None, ItemId=0x1234, SubId=None, Offset=None, Value=None
)

command2 = c.Sys.NVWrite.Req(
command2 = c.SYS.NVWrite.Req(
partial=True, SysId=None, ItemId=0x1234, SubId=None, Offset=None, Value=None
)

Expand All @@ -311,7 +311,7 @@ def test_command_immutability():


def test_command_serialization():
command = c.Sys.NVWrite.Req(
command = c.SYS.NVWrite.Req(
SysId=0x12, ItemId=0x3456, SubId=0x7890, Offset=0x0000, Value=b"asdfoo"
)
frame = command.to_frame()
Expand All @@ -320,27 +320,27 @@ def test_command_serialization():

# Partial frames cannot be serialized
with pytest.raises(ValueError):
partial1 = c.Sys.NVWrite.Req(partial=True, SysId=0x12)
partial1 = c.SYS.NVWrite.Req(partial=True, SysId=0x12)
partial1.to_frame()

# Partial frames cannot be serialized, even if all params are filled out
with pytest.raises(ValueError):
partial2 = c.Sys.NVWrite.Req(
partial2 = c.SYS.NVWrite.Req(
partial=True, SysId=None, ItemId=0x1234, SubId=None, Offset=None, Value=None
)
partial2.to_frame()


def test_command_equality():
command1 = c.Sys.NVWrite.Req(
command1 = c.SYS.NVWrite.Req(
SysId=0x12, ItemId=0x3456, SubId=0x7890, Offset=0x00, Value=b"asdfoo"
)

command2 = c.Sys.NVWrite.Req(
command2 = c.SYS.NVWrite.Req(
SysId=0x12, ItemId=0x3456, SubId=0x7890, Offset=0x00, Value=b"asdfoo"
)

command3 = c.Sys.NVWrite.Req(
command3 = c.SYS.NVWrite.Req(
SysId=0xFF, ItemId=0x3456, SubId=0x7890, Offset=0x00, Value=b"asdfoo"
)

Expand All @@ -357,22 +357,22 @@ def test_command_equality():
assert not command1.matches(command3)
assert not command3.matches(command1)

assert not command1.matches(c.Sys.NVWrite.Req(partial=True))
assert c.Sys.NVWrite.Req(partial=True).matches(command1)
assert not command1.matches(c.SYS.NVWrite.Req(partial=True))
assert c.SYS.NVWrite.Req(partial=True).matches(command1)

# parameters can be specified explicitly as None
assert c.Sys.NVWrite.Req(partial=True, SubId=None).matches(command1)
assert c.Sys.NVWrite.Req(partial=True, SubId=0x7890).matches(command1)
assert not c.Sys.NVWrite.Req(partial=True, SubId=123).matches(command1)
assert c.SYS.NVWrite.Req(partial=True, SubId=None).matches(command1)
assert c.SYS.NVWrite.Req(partial=True, SubId=0x7890).matches(command1)
assert not c.SYS.NVWrite.Req(partial=True, SubId=123).matches(command1)

# Different frame types do not match, even if they have the same structure
assert not c.Sys.NVWrite.Rsp(Status=t.Status.SUCCESS).matches(
c.Sys.NVDelete.Rsp(partial=True)
assert not c.SYS.NVWrite.Rsp(Status=t.Status.SUCCESS).matches(
c.SYS.NVDelete.Rsp(partial=True)
)


def test_command_deserialization(caplog):
command = c.Sys.NVWrite.Req(
command = c.SYS.NVWrite.Req(
SysId=0x12, ItemId=0x3456, SubId=0x7890, Offset=0x00, Value=b"asdfoo"
)

Expand All @@ -394,7 +394,7 @@ def test_command_deserialization(caplog):

# Deserialization fails if you attempt to deserialize the wrong frame
with pytest.raises(ValueError):
c.Sys.NVWrite.Req.from_frame(c.Sys.Ping.Req().to_frame())
c.SYS.NVWrite.Req.from_frame(c.SYS.Ping.Req().to_frame())


def test_command_not_recognized():
Expand Down
14 changes: 7 additions & 7 deletions tests/test_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_uart_rx_basic():
uart = znp_uart.ZnpMtProtocol(api)
uart.connection_made(transport)

test_command = c.Sys.ResetInd.Callback(
test_command = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x45,
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_uart_rx_byte_by_byte():
uart = znp_uart.ZnpMtProtocol(api)
uart.connection_made(transport)

test_command = c.Sys.ResetInd.Callback(
test_command = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x45,
Expand All @@ -75,7 +75,7 @@ def test_uart_rx_byte_by_byte_garbage():
uart = znp_uart.ZnpMtProtocol(api)
uart.connection_made(transport)

test_command = c.Sys.ResetInd.Callback(
test_command = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x45,
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_uart_rx_big_garbage():
uart = znp_uart.ZnpMtProtocol(api)
uart.connection_made(transport)

test_command = c.Sys.ResetInd.Callback(
test_command = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x45,
Expand Down Expand Up @@ -142,7 +142,7 @@ def test_uart_rx_corrupted_fcs():
uart = znp_uart.ZnpMtProtocol(api)
uart.connection_made(transport)

test_command = c.Sys.ResetInd.Callback(
test_command = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x45,
Expand All @@ -167,7 +167,7 @@ def test_uart_rx_sof_stress():
uart = znp_uart.ZnpMtProtocol(api)
uart.connection_made(transport)

test_command = c.Sys.ResetInd.Callback(
test_command = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x45,
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_uart_frame_received_error():
uart = znp_uart.ZnpMtProtocol(api)
uart.connection_made(transport)

test_command = c.Sys.ResetInd.Callback(
test_command = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x45,
Expand Down
6 changes: 3 additions & 3 deletions zigpy_znp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async def connect(self) -> None:

try:
# Make sure that our port works
await self.request(c.Sys.Ping.Req())
await self.request(c.SYS.Ping.Req())
except Exception:
self._uart = None
raise
Expand Down Expand Up @@ -347,15 +347,15 @@ async def nvram_write(
value = value.serialize()

return await self.request(
c.Sys.OSALNVWrite.Req(Id=nv_id, Offset=offset, Value=t.ShortBytes(value)),
c.SYS.OSALNVWrite.Req(Id=nv_id, Offset=offset, Value=t.ShortBytes(value)),
RspStatus=t.Status.SUCCESS,
)

async def nvram_read(
self, nv_id: nvids.BaseNvIds, *, offset: t.uint8_t = 0
) -> bytes:
response = await self.request(
c.Sys.OSALNVRead.Req(Id=nv_id, Offset=offset), RspStatus=t.Status.SUCCESS,
c.SYS.OSALNVRead.Req(Id=nv_id, Offset=offset), RspStatus=t.Status.SUCCESS,
)

return response.Value
4 changes: 2 additions & 2 deletions zigpy_znp/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .app_config import AppConfig
from .mac import MAC
from .sapi import SAPI
from .sys import Sys
from .sys import SYS
from .util import Util
from .zdo import ZDO
from .zgp import ZGP
Expand All @@ -16,7 +16,7 @@
AppConfig,
MAC,
SAPI,
Sys,
SYS,
Util,
ZDO,
ZGP,
Expand Down
Loading

0 comments on commit 87dd436

Please sign in to comment.