Skip to content

Commit d6c39ea

Browse files
authored
Use zigpy integer, enum, and bitmap types (#225)
* Use zigpy integer, enum, and bitmap types * Re-add exported objects removed during pre-commit * Create typing-compatible stubs
1 parent e60c556 commit d6c39ea

19 files changed

+113
-340
lines changed

tests/test_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_error_code():
6868

6969
r, rest = t.ErrorCode.deserialize(b"\xaa" + extra)
7070
assert rest == extra
71-
assert r.name == "unknown_0xAA"
71+
assert r.name == "undefined_0xaa"
7272

7373

7474
def _validate_schema(schema):

tests/test_types_basic.py

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class TestList(t.LVList, item_type=t.uint8_t, length_type=t.uint8_t):
1717
assert t.serialize_list([]) == b""
1818

1919

20-
def test_enum_uint():
21-
class TestEnum(t.enum_flag_uint16):
20+
def test_enum():
21+
class TestEnum(t.bitmap16):
2222
ALL = 0xFFFF
2323
CH_1 = 0x0001
2424
CH_2 = 0x0002
@@ -39,19 +39,6 @@ class TestEnum(t.enum_flag_uint16):
3939
assert TestEnum(0x8012).serialize() == data
4040

4141

42-
def test_abstract_ints():
43-
assert issubclass(t.uint8_t, t.uint_t)
44-
assert not issubclass(t.uint8_t, t.int_t)
45-
assert t.int_t._signed is True
46-
assert t.uint_t._signed is False
47-
48-
with pytest.raises(TypeError):
49-
t.int_t(0)
50-
51-
with pytest.raises(TypeError):
52-
t.FixedIntType(0)
53-
54-
5542
def test_int_too_short():
5643
with pytest.raises(ValueError):
5744
t.uint8_t.deserialize(b"")
@@ -132,29 +119,6 @@ class TestList(t.LVList, item_type=t.uint8_t, length_type=t.uint8_t):
132119
TestList.deserialize(b"\x04123")
133120

134121

135-
def test_hex_repr():
136-
class NwkAsHex(t.uint16_t, hex_repr=True):
137-
pass
138-
139-
nwk = NwkAsHex(0x123A)
140-
assert str(nwk) == "0x123A"
141-
assert repr(nwk) == "0x123A"
142-
143-
assert str([nwk]) == "[0x123A]"
144-
assert repr([nwk]) == "[0x123A]"
145-
146-
# You can turn it off as well
147-
class NwkWithoutHex(NwkAsHex, hex_repr=False):
148-
pass
149-
150-
nwk = NwkWithoutHex(1234)
151-
assert str(nwk) == "1234"
152-
assert repr(nwk) == "1234"
153-
154-
assert str([nwk]) == "[1234]"
155-
assert repr([nwk]) == "[1234]"
156-
157-
158122
def test_fixed_list():
159123
class TestList(t.FixedList, item_type=t.uint16_t, length=3):
160124
pass
@@ -187,7 +151,7 @@ class TestList(t.FixedList, length=3, item_type=t.uint16_t):
187151

188152

189153
def test_enum_instance_types():
190-
class TestEnum(t.enum_uint8):
154+
class TestEnum(t.enum8):
191155
Member = 0x00
192156

193157
assert TestEnum._member_type_ is t.uint8_t

tests/test_types_cstruct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class TestStruct(t.CStruct):
273273
def test_old_nib_deserialize():
274274
PaddingByte: typing_extensions.TypeAlias = t.uint8_t
275275

276-
class NwkState16(t.enum_uint16):
276+
class NwkState16(t.enum16):
277277
NWK_INIT = 0
278278
NWK_JOINING_ORPHAN = 1
279279
NWK_DISC = 2

tests/test_types_named.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_status():
2222
assert rest == extra
2323
assert r == 0x33
2424
assert r.value == 0x33
25-
assert r.name == "unknown_0x33"
25+
assert r.name == "undefined_0x33"
2626

2727

2828
def test_addr_mode_address():
@@ -99,24 +99,6 @@ def test_addr_mode_address():
9999
assert r3 != r4
100100

101101

102-
def test_missing_status_enum():
103-
class TestEnum(t.MissingEnumMixin, t.enum_uint8):
104-
Member = 0x00
105-
106-
assert 0xFF not in list(TestEnum)
107-
assert isinstance(TestEnum(0xFF), TestEnum)
108-
assert TestEnum(0xFF).value == 0xFF
109-
assert type(TestEnum(0xFF).value) is t.uint8_t
110-
111-
# Missing members that don't fit can't be created
112-
with pytest.raises(ValueError):
113-
TestEnum(0xFF + 1)
114-
115-
# Missing members that aren't integers can't be created
116-
with pytest.raises(ValueError):
117-
TestEnum("0xFF")
118-
119-
120102
def test_zdo_nullable_node_descriptor():
121103
desc1, data = c.zdo.NullableNodeDescriptor.deserialize(b"\x00")
122104

@@ -131,12 +113,3 @@ def test_zdo_nullable_node_descriptor():
131113

132114
assert not data
133115
assert desc2.serialize() == desc3.serialize()
134-
135-
136-
def test_missing_enum_mixin():
137-
class TestEnum(t.MissingEnumMixin, t.enum_uint8):
138-
FOO = 0x01
139-
140-
assert TestEnum(0x01) == 0x01 == TestEnum.FOO
141-
assert TestEnum(0x02) == 0x02
142-
assert 0x02 not in TestEnum._value2member_map_

zigpy_znp/commands/af.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import zigpy_znp.types as t
22

33

4-
class TransmitOptions(t.enum_flag_uint8):
4+
class TransmitOptions(t.bitmap8):
55
NONE = 0
66

77
# Will force the message to use Wildcard ProfileID
@@ -19,7 +19,7 @@ class TransmitOptions(t.enum_flag_uint8):
1919
SKIP_ROUTING = 0x80
2020

2121

22-
class LatencyReq(t.enum_uint8):
22+
class LatencyReq(t.enum8):
2323
NoLatencyReqs = 0x00
2424
FastBeacons = 0x01
2525
SlowBeacons = 0x02

zigpy_znp/commands/app_config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import zigpy_znp.types as t
44

55

6-
class TimeoutIndex(t.enum_uint8):
6+
class TimeoutIndex(t.enum8):
77
Seconds_10 = 0x00
88

99
Minutes_2 = 0x01
@@ -22,15 +22,15 @@ class TimeoutIndex(t.enum_uint8):
2222
Minutes_16384 = 0x0E
2323

2424

25-
class CentralizedLinkKeyMode(t.enum_uint8):
25+
class CentralizedLinkKeyMode(t.enum8):
2626
UseDefault = 0x00
2727
UseProvidedInstallCode = 0x01
2828
UseProvidedInstallCodeAndFallbackToDefault = 0x02
2929
UseProvidedAPSLinkKey = 0x03
3030
UseProvidedAPSLinkKeyAndFallbackToDefault = 0x04
3131

3232

33-
class BDBCommissioningStatus(t.enum_uint8):
33+
class BDBCommissioningStatus(t.enum8):
3434
Success = 0x00
3535
InProgress = 0x01
3636
NoNetwork = 0x02
@@ -48,7 +48,7 @@ class BDBCommissioningStatus(t.enum_uint8):
4848
Failure = 0x0E
4949

5050

51-
class BDBCommissioningMode(t.enum_flag_uint8):
51+
class BDBCommissioningMode(t.bitmap8):
5252
NONE = 0
5353

5454
InitiatorTouchLink = 1 << 0
@@ -59,7 +59,7 @@ class BDBCommissioningMode(t.enum_flag_uint8):
5959
ParentLost = 1 << 5
6060

6161

62-
class InstallCodeFormat(t.enum_uint8):
62+
class InstallCodeFormat(t.enum8):
6363
InstallCodeAndCRC = 0x01
6464
KeyDerivedFromInstallCode = 0x02
6565

zigpy_znp/commands/rpc_error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import zigpy_znp.types as t
22

33

4-
class ErrorCode(t.enum_uint8):
4+
class ErrorCode(t.enum8):
55
InvalidSubsystem = 0x01
66
InvalidCommandId = 0x02
77
InvalidParameter = 0x03

zigpy_znp/commands/sys.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import zigpy_znp.types as t
66

77

8-
class BootloaderBuildType(t.enum_uint8):
8+
class BootloaderBuildType(t.enum8):
99
NON_BOOTLOADER_BUILD = 0
1010
BUILT_AS_BIN = 1
1111
BUILT_AS_HEX = 2
1212

1313

14-
class ADCChannel(t.enum_uint8):
14+
class ADCChannel(t.enum8):
1515
"""The ADC channel."""
1616

1717
AIN0 = 0x00
@@ -26,7 +26,7 @@ class ADCChannel(t.enum_uint8):
2626
Voltage = 0x0F
2727

2828

29-
class ADCResolution(t.enum_uint8):
29+
class ADCResolution(t.enum8):
3030
"""Resolution of the ADC channel."""
3131

3232
bits_8 = 0x00
@@ -35,7 +35,7 @@ class ADCResolution(t.enum_uint8):
3535
bits_14 = 0x03
3636

3737

38-
class GPIOPinMode(t.enum_flag_uint8):
38+
class GPIOPinMode(t.bitmap8):
3939
"""Pin state. Any pin with an unspecified state bit is pulled up."""
4040

4141
Tristate0 = 0b0000_0001
@@ -49,7 +49,7 @@ class GPIOPinMode(t.enum_flag_uint8):
4949
PullDown3 = 0b1000_0000
5050

5151

52-
class GPIOPinDirection(t.enum_flag_uint8):
52+
class GPIOPinDirection(t.bitmap8):
5353
"""Pin direction. Any pin with an unspecified direction bit is an input pin."""
5454

5555
Output0 = 0b0000_0001
@@ -58,7 +58,7 @@ class GPIOPinDirection(t.enum_flag_uint8):
5858
Output3 = 0b0000_1000
5959

6060

61-
class GPIOOperation(t.enum_uint8):
61+
class GPIOOperation(t.enum8):
6262
"""Specifies the type of operation to perform on the GPIO pins."""
6363

6464
SetDirection = 0x00
@@ -70,7 +70,7 @@ class GPIOOperation(t.enum_uint8):
7070
HiD = 0x12 # ???
7171

7272

73-
class StackTuneOperation(t.enum_uint8):
73+
class StackTuneOperation(t.enum8):
7474
"""The tuning operation to be executed."""
7575

7676
# XXX: [Value] should correspond to the valid values specified by the

zigpy_znp/commands/ubl.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
FLASH_WORD_SIZE = 4
1212

1313

14-
class BootloaderStatus(t.enum_uint8):
14+
class BootloaderStatus(t.enum8):
1515
SUCCESS = 0
1616
FAILURE = 1
1717
INVALID_FCS = 2
@@ -23,12 +23,12 @@ class BootloaderStatus(t.enum_uint8):
2323
CANCELED = 8
2424

2525

26-
class BootloaderDeviceType(t.enum_uint8):
26+
class BootloaderDeviceType(t.enum8):
2727
CC2538 = 1
2828
CC2530 = 2
2929

3030

31-
class BootloaderRunMode(t.enum_uint8):
31+
class BootloaderRunMode(t.enum8):
3232
# Read the code, not the spec
3333
FORCE_BOOT = 0x10
3434
FORCE_RUN = FORCE_BOOT ^ 0xFF

zigpy_znp/commands/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import zigpy_znp.types as t
55

66

7-
class NodeRelation(t.enum_uint8):
7+
class NodeRelation(t.enum8):
88
PARENT = 0
99
CHILD_RFD = 1
1010
CHILD_RFD_RX_IDLE = 2
@@ -62,7 +62,7 @@ class RandomNumbers(t.FixedList, item_type=t.uint8_t, length=100):
6262
pass
6363

6464

65-
class LEDMode(t.enum_uint8):
65+
class LEDMode(t.enum8):
6666
OFF = 0
6767
ON = 1
6868
BLINK = 2

zigpy_znp/commands/zdo.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ class SecurityEntry(t.FixedList, item_type=t.uint8_t, length=5):
1717
pass
1818

1919

20-
class StartupState(t.enum_uint8):
20+
class StartupState(t.enum8):
2121
RestoredNetworkState = 0x00
2222
NewNetworkState = 0x01
2323
NotStarted = 0x02
2424

2525

26-
class RouteDiscoveryOptions(t.enum_flag_uint8):
26+
class RouteDiscoveryOptions(t.bitmap8):
2727
UNICAST = 0x00
2828
MTO_WITH_ROUTE_CACHE = 0x01
2929
MTO_WITHOUT_ROUTE_CACHE = 0x03
3030

3131

32-
class RouteStatus(t.enum_uint8):
32+
class RouteStatus(t.enum8):
3333
INIT = 0
3434
ACTIVE = 1
3535
DISC = 2
3636
LINK_FAIL = 3
3737
REPAIR = 4
3838

3939

40-
class RouteOptions(t.enum_flag_uint8):
40+
class RouteOptions(t.bitmap8):
4141
# Used in option of NLME_RouteDiscoveryRequest() and rtgTable[]
4242
MTO_ROUTE = 0x01
4343

@@ -60,7 +60,7 @@ class RouteOptions(t.enum_flag_uint8):
6060
MULTICAST_ROUTE = 0x40
6161

6262

63-
class RoutingStatus(t.enum_uint8):
63+
class RoutingStatus(t.enum8):
6464
SUCCESS = 0
6565
FAIL = 1
6666
TBL_FULL = 2
@@ -71,7 +71,7 @@ class RoutingStatus(t.enum_uint8):
7171
SRC_TBL_FULL = 7
7272

7373

74-
class MACCapabilities(t.enum_flag_uint8):
74+
class MACCapabilities(t.bitmap8):
7575
PANCoordinator = 1 << 0
7676
Router = 1 << 1
7777
MainsPowered = 1 << 2
@@ -82,7 +82,7 @@ class MACCapabilities(t.enum_flag_uint8):
8282
AllocateShortAddrDuringAssocNeeded = 1 << 7
8383

8484

85-
class LeaveOptions(t.enum_flag_uint8):
85+
class LeaveOptions(t.bitmap8):
8686
NONE = 0
8787
Rejoin = 1 << 0
8888
RemoveChildren = 1 << 1
@@ -136,7 +136,7 @@ def serialize(self) -> bytes:
136136
return super().serialize()
137137

138138

139-
class AddrRequestType(t.enum_uint8):
139+
class AddrRequestType(t.enum8):
140140
SINGLE = 0x00
141141
EXTENDED = 0x01
142142

zigpy_znp/commands/znp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import zigpy_znp.types as t
22

33

4-
class DiscreteCommand(t.enum_flag_uint8):
4+
class DiscreteCommand(t.bitmap8):
55
ZDOStart = 0x40
66
ResetNwk = 0x80
77

0 commit comments

Comments
 (0)