-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
remote_value_sensor.py
95 lines (81 loc) · 3.37 KB
/
remote_value_sensor.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Module for managing a remote value typically used within a sensor.
The module maps a given value_type to a DPT class and uses this class
for serialization and deserialization of the KNX value.
"""
from xknx.exceptions import ConversionError
from xknx.knx import (
DPT2ByteFloat, DPT2ByteUnsigned, DPT4ByteFloat, DPT4ByteSigned,
DPT4ByteUnsigned, DPTArray, DPTBrightness, DPTColorTemperature,
DPTElectricCurrent, DPTElectricPotential, DPTEnergy, DPTEnthalpy,
DPTFrequency, DPTHeatFlowRate, DPTHumidity, DPTLux, DPTPartsPerMillion,
DPTPhaseAngleDeg, DPTPhaseAngleRad, DPTPower, DPTPowerFactor, DPTSpeed,
DPTTemperature, DPTUElCurrentmA, DPTValue1Ucount, DPTVoltage, DPTWsp)
from .remote_value import RemoteValue
class RemoteValueSensor(RemoteValue):
"""Abstraction for many different sensor DPT types."""
DPTMAP = {
'current': DPTUElCurrentmA,
'brightness': DPTBrightness,
'color_temperature': DPTColorTemperature,
'temperature': DPTTemperature,
'illuminance': DPTLux,
'speed_ms': DPTWsp,
'humidity': DPTHumidity,
'voltage': DPTVoltage,
'power': DPTPower,
'electric_current': DPTElectricCurrent,
'electric_potential': DPTElectricPotential,
'energy': DPTEnergy,
'frequency': DPTFrequency,
'heatflowrate': DPTHeatFlowRate,
'phaseanglerad': DPTPhaseAngleRad,
'phaseangledeg': DPTPhaseAngleDeg,
'pulse': DPTValue1Ucount,
'powerfactor': DPTPowerFactor,
'speed': DPTSpeed,
'enthalpy': DPTEnthalpy,
'ppm': DPTPartsPerMillion,
# Generic DPT Without Min/Max and Unit.
'DPT-5': DPTValue1Ucount,
'1byte_unsigned': DPTValue1Ucount,
'DPT-7': DPT2ByteUnsigned,
'2byte_unsigned': DPT2ByteUnsigned,
'DPT-9': DPT2ByteFloat,
'DPT-12': DPT4ByteUnsigned,
'4byte_unsigned': DPT4ByteUnsigned,
'DPT-13': DPT4ByteSigned,
'4byte_signed': DPT4ByteSigned,
'DPT-14': DPT4ByteFloat,
'4byte_float': DPT4ByteFloat
}
def __init__(self,
xknx,
group_address=None,
group_address_state=None,
value_type=None,
device_name=None,
after_update_cb=None):
"""Initialize RemoteValueSensor class."""
# pylint: disable=too-many-arguments
super(RemoteValueSensor, self).__init__(
xknx, group_address, group_address_state,
device_name=device_name, after_update_cb=after_update_cb)
if value_type not in self.DPTMAP:
raise ConversionError("invalid value type", value_type=value_type, device_name=device_name)
self.value_type = value_type
def payload_valid(self, payload):
"""Test if telegram payload may be parsed."""
return (
isinstance(payload, DPTArray) and
len(payload.value) == self.DPTMAP[self.value_type].payload_length)
def to_knx(self, value):
"""Convert value to payload."""
return DPTArray(self.DPTMAP[self.value_type].to_knx(value))
def from_knx(self, payload):
"""Convert current payload to value."""
return self.DPTMAP[self.value_type].from_knx(payload.value)
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return self.DPTMAP[self.value_type].unit