Skip to content

Commit

Permalink
Added DPT 29 (INT64) (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
greiginsydney authored Jan 19, 2025
1 parent b6fe17e commit 5bd026b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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
INT64 = 29
COLOUR_RGB = 232


Expand Down Expand Up @@ -82,6 +83,7 @@ def from_datetime(cls, value: datetime.datetime):
KNXDPT.DATE_TIME: bytes,
KNXDPT.ENUM8: bytes,
KNXDPT.VARSTRING: bytes,
KNXDPT.INT64: bytes,
KNXDPT.COLOUR_RGB: bytes,
}

Expand All @@ -106,6 +108,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.INT64: int,
KNXDPT.COLOUR_RGB: tuple,
}

Expand Down Expand Up @@ -212,6 +215,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.INT64:
return struct.pack('>q', val)
elif t is KNXDPT.COLOUR_RGB:
if len(val) != 3:
raise ValueError(f"KNX DPT 232 requires a three-byte tuple. Received {len(val)}")
Expand Down Expand Up @@ -280,6 +285,8 @@ 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.INT64:
return struct.unpack('>q', val)[0]
elif t is KNXDPT.COLOUR_RGB:
return tuple(val)
else:
Expand Down
12 changes: 12 additions & 0 deletions test/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ class HAVCMode(enum.Enum):
self.assertEqual(bytes([2]), knxdclient.encode_value(2, knxdclient.KNXDPT.ENUM8))
self.assertEqual(2, knxdclient.decode_value(bytes([2]), knxdclient.KNXDPT.ENUM8))

def test_dpt29_conversion(self) -> None:
self.assertEqual(bytes([0xff, 0xff, 0xff, 0xff, 0xf8, 0xa4, 0x32, 0xeb]),
knxdclient.encode_value(-123456789, knxdclient.KNXDPT.INT64))
self.assertEqual(bytes([0x00, 0x00, 0x00, 0x0b, 0xad, 0xc0, 0xff, 0xee]),
knxdclient.encode_value(0xbadc0ffee, knxdclient.KNXDPT.INT64))
self.assertEqual(-123456789,
knxdclient.decode_value(bytes([0xff, 0xff, 0xff, 0xff, 0xf8, 0xa4, 0x32, 0xeb]),
knxdclient.KNXDPT.INT64))
self.assertEqual(0xbadc0ffee,
knxdclient.decode_value(bytes([0x00, 0x00, 0x00, 0x0b, 0xad, 0xc0, 0xff, 0xee]),
knxdclient.KNXDPT.INT64))

def test_dpt232_conversion(self) -> None:
self.assertEqual((0x12, 0x34, 0x56),
knxdclient.decode_value(
Expand Down

0 comments on commit 5bd026b

Please sign in to comment.