/
dpt_1byte_signed.py
62 lines (45 loc) · 1.43 KB
/
dpt_1byte_signed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Implementation of Basic KNX 1-Byte signed integer values."""
from xknx.exceptions import ConversionError
from .dpt import DPTBase
class DPTSignedRelativeValue(DPTBase):
"""
Abstraction for KNX 1 Byte "1-octet Signed Relative Value".
DPT 6.***
"""
value_min = -128
value_max = 127
unit = ""
payload_length = 1
@classmethod
def from_knx(cls, raw):
"""Parse/deserialize from KNX/IP raw data."""
cls.test_bytesarray(raw, 1)
if raw[0] > cls.value_max:
return raw[0] - 0x100
return raw[0]
@classmethod
def to_knx(cls, value):
"""Serialize to KNX/IP raw data."""
try:
knx_value = int(value)
if not cls._test_boundaries(knx_value):
raise ValueError
if knx_value < 0:
knx_value += 0x100
return (knx_value & 0xff,)
except ValueError:
raise ConversionError("Cant serialize %s" % cls.__name__, value=value)
@classmethod
def _test_boundaries(cls, value):
"""Test if value is within defined range for this object."""
return cls.value_min <= value <= cls.value_max
class DPTPercentV8(DPTSignedRelativeValue):
"""Abstraction for KNX DPT_Percent_V8.
DPT 6.001
"""
unit = "%"
class DPTValue1Count(DPTSignedRelativeValue):
"""Abstraction for KNX DPT_Value_1_Count.
DPT 6.010
"""
unit = "counter pulses"