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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
7 changes: 7 additions & 0 deletions knxdclient/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class KNXDPT(enum.Enum):
DATE_TIME = 19
ENUM8 = 20
VARSTRING = 24
COLOUR_RGB = 232


class KNXTime(NamedTuple):
Expand Down Expand Up @@ -81,6 +82,7 @@ def from_datetime(cls, value: datetime.datetime):
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 @@ def from_datetime(cls, value: datetime.datetime):
KNXDPT.DATE_TIME: (datetime.datetime, datetime.date, datetime.time),
KNXDPT.ENUM8: (int, enum.Enum),
KNXDPT.VARSTRING: str,
KNXDPT.COLOUR_RGB: bytes,
greiginsydney marked this conversation as resolved.
Show resolved Hide resolved
}


Expand Down Expand Up @@ -209,6 +212,8 @@ def encode_value(value: Any, t: KNXDPT) -> EncodedData:
return bytes([val])
elif t is KNXDPT.VARSTRING:
return val.encode('iso-8859-1') + b'\0'
elif t is KNXDPT.COLOUR_RGB:
return bytes([val[0] , val[1] , val[2]])
else:
raise NotImplementedError()

Expand Down Expand Up @@ -273,5 +278,7 @@ def decode_value(value: EncodedData, t: KNXDPT) -> Any:
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 f'{(val[2] + (val[1] << 8) + (val[0] << 16)):X}'
greiginsydney marked this conversation as resolved.
Show resolved Hide resolved
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(bytes([0x10, 0xCC, 0x0A]),
knxdclient.encode_value(bytes([0x10, 0xCC, 0x0A]), knxdclient.KNXDPT.COLOUR_RGB))
self.assertEqual(bytes([127, 00, 32]),
knxdclient.encode_value(bytes([0x7F, 0x00, 0x20]), knxdclient.KNXDPT.COLOUR_RGB))
self.assertEqual(f'{0x801B10:X}',
greiginsydney marked this conversation as resolved.
Show resolved Hide resolved
knxdclient.decode_value(bytes([0x80, 0x1B, 0x10]), knxdclient.KNXDPT.COLOUR_RGB))
self.assertEqual(f'{0x123456:X}',
knxdclient.decode_value(bytes([0x12, 0x34, 0x56]), knxdclient.KNXDPT.COLOUR_RGB))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add a "roundtrip" test here that checks that decode_value(encode_value(some_value, ...), ...) == some_value.

This should also reveal the issue with the deviating datatypes, discussed in my other comments.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ta. I shall tinker. (This first one might take us a little while, but it should speed any future ones. I appreciate the hand-holding).