-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpzem004t.py
296 lines (243 loc) · 10.6 KB
/
pzem004t.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""Device handler for CSRLabs PZEM-004T Ver 3 based on PTVO.info firmware"""
from zigpy.profiles import zha
from zigpy.quirks import CustomCluster, CustomDevice
from zhaquirks import Bus, LocalDataCluster
from zigpy.zcl.clusters.homeautomation import Diagnostic, ElectricalMeasurement
from zigpy.zcl.clusters.general import Basic, OnOffConfiguration, AnalogInput, MultistateValue, MultistateInput
from zigpy.zcl.clusters.measurement import TemperatureMeasurement
from zigpy.zcl.clusters.smartenergy import Metering
from homeassistant.components.zha.sensor import ElectricalMeasurement as EM
from zhaquirks.const import (
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
SKIP_CONFIGURATION,
)
TEMPERATURE_REPORTED = "temperature_reported"
VOLTAGE_REPORTED = "voltage_reported"
CURRENT_REPORTED = "current_reported"
POWER_REPORTED = "power_reported"
FREQUENCY_REPORTED = "frequency_reported"
POWER_FACTOR_REPORTED = "power_factor_reported"
APPARENT_POWER_REPORTED = "apparent_power_reported"
CONSUMPTION_REPORTED = "consumption_reported"
INSTANTANEOUS_DEMAND = "instantaneous_demand"
PTVO_DEVICE = 0xfffe
def myformatter(self, value: int) -> int | float:
"""Return 'normalized' value."""
multiplier = getattr(self._channel, f"{self._div_mul_prefix}_multiplier")
divisor = getattr(self._channel, f"{self._div_mul_prefix}_divisor")
value = float(value * multiplier) / divisor
return round(value, self._decimals)
EM.formatter = myformatter
class PtvoAnalogInputCluster(CustomCluster, AnalogInput):
cluster_id = AnalogInput.cluster_id
def __init__(self, *args, **kwargs):
"""Init."""
self._current_state = {}
self._current_value = 0
self._v_value = 0
self._c_value = 0
super().__init__(*args, **kwargs)
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if value is not None:
if attrid == 85:
self._current_value = value
if attrid == 28:
if value == "C":
"""Chip temperature value."""
t_value = self._current_value * 100
self.endpoint.device.temperature_bus.listener_event(TEMPERATURE_REPORTED, t_value)
if value == "V":
"""Voltage value."""
self._v_value = self._current_value
self.endpoint.device.electrical_bus.listener_event(VOLTAGE_REPORTED, self._v_value)
if value == "A":
"""Current value."""
self._c_value = self._current_value
self.endpoint.device.electrical_bus.listener_event(CURRENT_REPORTED, self._c_value)
"""Apparent Power value"""
a_p_value = self._v_value * self._c_value
self.endpoint.device.electrical_bus.listener_event(APPARENT_POWER_REPORTED, a_p_value)
if value == "W":
"""Power value."""
p_value = self._current_value
p_value1 = self._current_value / 1000
self.endpoint.device.electrical_bus.listener_event(POWER_REPORTED, p_value)
self.endpoint.device.consumption_bus.listener_event(INSTANTANEOUS_DEMAND, p_value1)
if value == "Hz":
"""Frequency value."""
f_value = self._current_value
self.endpoint.device.electrical_bus.listener_event(FREQUENCY_REPORTED, f_value)
if value == "pf":
"""Power Factor value."""
pf_value = self._current_value
self.endpoint.device.electrical_bus.listener_event(POWER_FACTOR_REPORTED, pf_value)
if value == "Wh":
"""Energy value."""
e_value = self._current_value / 1000
self.endpoint.device.consumption_bus.listener_event(CONSUMPTION_REPORTED, e_value)
class TemperatureMeasurementCluster(LocalDataCluster, TemperatureMeasurement):
cluster_id = TemperatureMeasurement.cluster_id
MEASURED_VALUE_ID = 0x0000
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.temperature_bus.add_listener(self)
def temperature_reported(self, value):
"""Temperature reported."""
self._update_attribute(self.MEASURED_VALUE_ID, value)
class ElectricalMeasurementCluster(LocalDataCluster, ElectricalMeasurement):
"""Electrical measurement cluster."""
cluster_id = ElectricalMeasurement.cluster_id
POWER_ID = 0x050B
VOLTAGE_ID = 0x0505
CURRENT_ID = 0x0508
FREQUENCY_ID = 0x0300
POWER_FACTOR_ID = 0x0510
APPARENT_POWER_ID = 0x050F
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.electrical_bus.add_listener(self)
def power_reported(self, value):
"""Power reported."""
self._update_attribute(self.POWER_ID, value)
def voltage_reported(self, value):
"""Voltage reported."""
self._update_attribute(self.VOLTAGE_ID, value)
def current_reported(self, value):
"""Current reported."""
self._update_attribute(self.CURRENT_ID, value)
def frequency_reported(self, value):
"""Frequency reported."""
self._update_attribute(self.FREQUENCY_ID, value)
def power_factor_reported(self, value):
"""Power Factor reported."""
self._update_attribute(self.POWER_FACTOR_ID, value)
def apparent_power_reported(self, value):
"""Apparent Power reported"""
self._update_attribute(self.APPARENT_POWER_ID, value)
class MeteringCluster(LocalDataCluster, Metering):
"""Metering cluster to receive reports that are sent to the basic cluster."""
cluster_id = Metering.cluster_id
CURRENT_SUMM_DELIVERED_ID = 0x0000
INSTANTANEOUS_DEMAND_ID = 0x0400
UNIT_OF_MEASUREMENT = 0x0300
MULTIPLIER = 0x0301
DIVISOR = 0x0302
SUMMATION_FORMATTING = 0x0303
METERING_DEVICE_TYPE = 0x0306
_CONSTANT_ATTRIBUTES = {
0x0300: 0, # unit_of_measure: kWh
0x0301: 1, # multiplier
0x0302: 1000, # divisor
0x0303: 0b0_0100_011, # summation_formatting
0x0306: 0, # metering_device_type: electric
}
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.consumption_bus.add_listener(self)
# initialize constant attributes
self._update_attribute(self.UNIT_OF_MEASUREMENT, 0)
self._update_attribute(self.MULTIPLIER, 1)
self._update_attribute(self.DIVISOR, 1000)
self._update_attribute(self.SUMMATION_FORMATTING, 0b0_0100_011)
self._update_attribute(self.METERING_DEVICE_TYPE, 0)
def consumption_reported(self, value):
"""Consumption reported."""
self._update_attribute(self.CURRENT_SUMM_DELIVERED_ID, round(value))
def instantaneous_demand(self, value):
"""Instantaneous demand reported."""
self._update_attribute(self.INSTANTANEOUS_DEMAND_ID, value)
class pzem004t(CustomDevice):
"""PZEM-004T Ver 3 based on PTVO firmware."""
def __init__(self, *args, **kwargs):
"""Init device."""
self.temperature_bus = Bus()
self.electrical_bus = Bus()
self.consumption_bus = Bus()
super().__init__(*args, **kwargs)
signature = {
MODELS_INFO: [("CSRLabs", "pzem004t")],
ENDPOINTS: {
# <SimpleDescriptor endpoint=1 profile=260 device_type=65534
# device_version=1
# input_clusters=[0, 7, 20]
# output_clusters=[0, 18]>
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: PTVO_DEVICE,
INPUT_CLUSTERS: [
Basic.cluster_id,
OnOffConfiguration.cluster_id,
MultistateValue.cluster_id,
],
OUTPUT_CLUSTERS: [
Basic.cluster_id,
MultistateInput.cluster_id,
],
},
# <SimpleDescriptor endpoint=1 profile=260 device_type=65534
# device_version=1
# input_clusters=[12, 20]
# output_clusters=[]>
2: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: PTVO_DEVICE,
INPUT_CLUSTERS: [
AnalogInput.cluster_id,
MultistateValue.cluster_id,
],
OUTPUT_CLUSTERS: [],
},
# <SimpleDescriptor endpoint=1 profile=260 device_type=65534
# device_version=1
# input_clusters=[12]
# output_clusters=[]>
3: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: PTVO_DEVICE,
INPUT_CLUSTERS: [AnalogInput.cluster_id],
OUTPUT_CLUSTERS: [],
},
},
}
replacement = {
SKIP_CONFIGURATION: True,
ENDPOINTS: {
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: PTVO_DEVICE,
INPUT_CLUSTERS: [
Basic.cluster_id,
OnOffConfiguration.cluster_id,
MultistateValue.cluster_id,
TemperatureMeasurementCluster,
ElectricalMeasurementCluster,
MeteringCluster,
],
OUTPUT_CLUSTERS: [Basic.cluster_id],
},
2: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.METER_INTERFACE,
INPUT_CLUSTERS: [
PtvoAnalogInputCluster,
MultistateValue.cluster_id,
],
OUTPUT_CLUSTERS: [],
},
3: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.METER_INTERFACE,
INPUT_CLUSTERS: [PtvoAnalogInputCluster],
OUTPUT_CLUSTERS: [],
},
},
}