-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpzem004t.py
231 lines (185 loc) · 8.01 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
"""Device handler for PTVO PZEM-004T Ver 3"""
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, AnalogInput, MultistateValue, MultistateInput
from zigpy.zcl.clusters.measurement import TemperatureMeasurement
from zigpy.zcl.clusters.smartenergy import Metering
from zhaquirks.const import (
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
)
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"
ENERGY_REPORTED = "energy_reported"
INSTANTANEOUS_DEMAND = "instantaneous_demand"
PTVO_DEVICE = 0xfffe
class PtvoAnalogInputCluster(CustomCluster, AnalogInput):
cluster_id = AnalogInput.cluster_id
def __init__(self, *args, **kwargs):
"""Init."""
self._current_state = {}
self._current_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."""
v_value = self._current_value
self.endpoint.device.electrical_bus.listener_event(VOLTAGE_REPORTED, v_value)
if value == "A":
"""Current value."""
c_value = self._current_value
self.endpoint.device.electrical_bus.listener_event(CURRENT_REPORTED, c_value)
if value == "W":
"""Power value."""
p_value = self._current_value
self.endpoint.device.electrical_bus.listener_event(POWER_REPORTED, p_value)
self.endpoint.device.energy_bus.listener_event(INSTANTANEOUS_DEMAND, p_value)
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
self.endpoint.device.energy_bus.listener_event(ENERGY_REPORTED, e_value)
class SmartEnergyCluster(LocalDataCluster, Metering):
cluster_id = Metering.cluster_id
CURRENT_SUMM_DELIVERED_VALUE_ID = 0x0000
INSTANTANEOUS_DEMAND_VALUE_ID = 0x0400
POWER_WATT = 0x0000
"""Setting unit of measurement."""
_CONSTANT_ATTRIBUTES = {0x0300: POWER_WATT}
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.energy_bus.add_listener(self)
def energy_reported(self, value):
"""Energy reported."""
self._update_attribute(self.CURRENT_SUMM_DELIVERED_VALUE_ID, value)
def instantaneous_demand(self, value):
"""Instantaneous power demand reported."""
self._update_attribute(self.INSTANTANEOUS_DEMAND_VALUE_ID, 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
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)
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.energy_bus = Bus()
super().__init__(*args, **kwargs)
signature = {
MODELS_INFO: [("ptvo.info", "pzem004t")],
ENDPOINTS: {
# <SimpleDescriptor endpoint=1 profile=260 device_type=65534
# device_version=1
# input_clusters=[0, 12, 20, 2821]
# output_clusters=[0, 18]>
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: PTVO_DEVICE,
INPUT_CLUSTERS: [
Basic.cluster_id,
AnalogInput.cluster_id,
MultistateValue.cluster_id,
Diagnostic.cluster_id,
],
OUTPUT_CLUSTERS: [
Basic.cluster_id,
MultistateInput.cluster_id,
],
},
# <SimpleDescriptor endpoint=1 profile=260 device_type=65534
# device_version=1
# input_clusters=[12]
# output_clusters=[]>
2: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: PTVO_DEVICE,
INPUT_CLUSTERS: [AnalogInput.cluster_id],
OUTPUT_CLUSTERS: [],
},
},
}
replacement = {
ENDPOINTS: {
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: PTVO_DEVICE,
INPUT_CLUSTERS: [
Basic.cluster_id,
Diagnostic.cluster_id,
PtvoAnalogInputCluster,
TemperatureMeasurementCluster,
SmartEnergyCluster,
ElectricalMeasurementCluster,
],
OUTPUT_CLUSTERS: [Basic.cluster_id],
},
2: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.METER_INTERFACE,
INPUT_CLUSTERS: [PtvoAnalogInputCluster],
OUTPUT_CLUSTERS: [],
},
},
}