Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DPT232 #5

Merged
merged 12 commits into from
Dec 31, 2024
9 changes: 9 additions & 0 deletions knxdclient/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
DATE_TIME = 19
ENUM8 = 20
VARSTRING = 24
COLOUR_RGB = 232


class KNXTime(NamedTuple):
Expand Down Expand Up @@ -81,6 +82,7 @@
KNXDPT.DATE_TIME: bytes,
KNXDPT.ENUM8: bytes,
KNXDPT.VARSTRING: bytes,
KNXDPT.COLOUR_RGB: bytes,
}

DPT_PYTHON_REPRESENTATION: Dict[KNXDPT, Union[type, Tuple[type, ...]]] = {
Expand All @@ -104,6 +106,7 @@
KNXDPT.DATE_TIME: (datetime.datetime, datetime.date, datetime.time),
KNXDPT.ENUM8: (int, enum.Enum),
KNXDPT.VARSTRING: str,
KNXDPT.COLOUR_RGB: tuple,
}


Expand Down Expand Up @@ -209,6 +212,10 @@
return bytes([val])
elif t is KNXDPT.VARSTRING:
return val.encode('iso-8859-1') + b'\0'
elif t is KNXDPT.COLOUR_RGB:
if len(val) != 3:
raise ValueError(f"KNX DPT 232 requires a three-byte tuple. Received {len(val)}")

Check warning on line 217 in knxdclient/encoding.py

View check run for this annotation

Codecov / codecov/patch

knxdclient/encoding.py#L217

Added line #L217 was not covered by tests
return bytes(val)
else:
raise NotImplementedError()

Expand Down Expand Up @@ -273,5 +280,7 @@
elif t is KNXDPT.ENUM8:
# The raw int val is returned. The User code must construct the correct Enum type if required.
return val[0]
elif t is KNXDPT.COLOUR_RGB:
return tuple(val)
else:
raise NotImplementedError()
10 changes: 10 additions & 0 deletions test/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,13 @@ class HAVCMode(enum.Enum):
self.assertEqual(bytes([2]), knxdclient.encode_value(HAVCMode.Standby, knxdclient.KNXDPT.ENUM8))
self.assertEqual(bytes([2]), knxdclient.encode_value(2, knxdclient.KNXDPT.ENUM8))
self.assertEqual(2, knxdclient.decode_value(bytes([2]), knxdclient.KNXDPT.ENUM8))

def test_dpt232_conversion(self) -> None:
self.assertEqual((0x12, 0x34, 0x56),
knxdclient.decode_value(
knxdclient.encode_value((0x12, 0x34, 0x56), knxdclient.KNXDPT.COLOUR_RGB),
knxdclient.KNXDPT.COLOUR_RGB))
self.assertEqual(bytes([0x7f, 0x00, 0x20]),
knxdclient.encode_value((0x7f, 0x00, 0x20), knxdclient.KNXDPT.COLOUR_RGB))
self.assertEqual((0x10, 0xcc, 0x0a),
knxdclient.decode_value(bytes([0x10, 0xcc, 0x0a]), knxdclient.KNXDPT.COLOUR_RGB))
Loading