-
Notifications
You must be signed in to change notification settings - Fork 19
/
smappee.py
287 lines (225 loc) · 7.73 KB
/
smappee.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
from functools import partial
import time
import device
import probe
from register import *
# CT Type Identifier register to type name mapping
CT_TYPES = [
'SCT01-50A/100A/200A',
'SCT01-400A/800A',
'Rogowski coil 600A-100mV',
'SCT02-50A',
'SCT02-100A',
'SCT02-200A',
'SCT02-400A',
'SCT02-800A',
'SCT03-50A',
'SCT03-100A',
'SCT03-200A',
'Rogowski coil 400A-100mV',
'Closed CT',
]
# CT Associated Voltage register to phase number mapping
CT_PHASE = {
0: -1, # unassigned
1: 0, # L1 forward
16: 0, # L1 reverse
2: 1, # L2 forward
32: 1, # L2 reverse
4: 2, # L3 forward
64: 2, # L3 reverse
};
CT_PHASE_TEXT = {
-1: 'None',
0: 'L1',
1: 'L2',
2: 'L3',
}
DEV_TYPES = {
5400: 'Power Box',
5500: 'CT Hub',
5600: 'Solid Core 3-Phase CT',
}
MAX_BUS_DEVICES = 10
MAX_CT_SLOTS = 28
class Reg_ser(Reg, str):
def __init__(self, base, name):
super().__init__(base, 4, name)
def decode(self, values):
v = '%04d%06d' % (values[0], values[3] << 16 | values[2])
return self.update(v)
class Reg_ver(Reg, int):
def __init__(self, base, *args):
super().__init__(base, 2, *args)
def __int__(self):
v = self.value
return v[0] << 16 | v[1]
def __str__(self):
return '%d.%d' % self.value
def decode(self, values):
return self.update((values[1], values[0]))
class CurrentTransformer:
def __init__(self, dev, slot, sdev, chan):
self.dev = dev
self.slot = slot
self.sdev = sdev
self.chan = chan
def probe(self):
n = self.slot
self.regs = [
Reg_mapu16(0x1000 + n, '/CT/%d/Phase' % n, CT_PHASE,
text=CT_PHASE_TEXT, write=self.set_phase),
Reg_u16(0x1100 + n, '/CT/%d/Type' % n, text=CT_TYPES, write=True),
]
self.phase = self.dev.read_register(self.regs[0])
if self.phase is None:
self.log.warning('CT %d configured outside Venus', n)
return False
return True
def set_phase(self, n):
v = 0 if n < 0 else 1 << int(n)
self.phase = n
self.dev.write_register(self.regs[0], v)
if n >= 0:
for ct in self.dev.all_cts:
if ct is not self and ct.phase == n:
ct.set_phase(-1)
def identify(self, v):
self.dev.write_register(Reg_u16(0x0900 + self.slot), v)
class PowerBox(device.CustomName, device.EnergyMeter):
vendor_id = 'smappee'
vendor_name = 'Smappee'
productid = 0xb018
productname = 'Smappee Power Box'
min_fwver = (1, 44)
age_limit_fast = 0
refresh_time = 100
def probe_device(self, n):
base = 0x1480 + 0x20 * n
regs = [
Reg_u16(base + 0x00, '/Device/%d/Type' % n, text=DEV_TYPES),
Reg_u16(base + 0x01, '/Device/%d/Slots' % n),
Reg_ser(base + 0x00, '/Device/%d/Serial' % n),
Reg_ver(base + 0x04, '/Device/%d/FirmwareVersion' % n),
]
if self.read_register(regs[0]) == 0:
return
slots = self.read_register(regs[1])
for s in range(slots):
addr = base + 0x0a + s + (s > 7)
chan = chr(ord('A') + s)
sreg = Reg_u16(addr, '/Device/%d/Channel/%s/Slot' % (n, chan))
slot = self.read_register(sreg)
regs.append(sreg)
self.probe_ct(slot, n, chan)
self.info_regs += regs
def probe_ct(self, n, sdev, chan):
ct = CurrentTransformer(self, n, sdev, chan)
if not ct.probe():
return
if ct.phase >= 0:
self.ct_phase[ct.phase].append(ct)
self.all_cts.append(ct)
self.info_regs += ct.regs
def add_phase(self, ph, ct):
n = ph + 1
s = ct.slot
self.voltage_regs += [
Reg_f32l(0x0000 + 4 * ph, '/Ac/L%d/Voltage' % n, 1, '%.1f V'),
]
self.current_regs += [
Reg_f32l(0x0080 + 4 * s, '/Ac/L%d/Current' % n, 1, '%.1f A'),
]
self.power_regs += [
Reg_f32l(0x0380 + 2 * s, '/Ac/L%d/Power' % n, 1, '%.1f W'),
]
self.energy_regs += [
Reg_s32l(0x3000 + 4 * s, '/Ac/L%d/Energy/Forward' % n, 1000, '%.1f kWh'),
Reg_s32l(0x3002 + 4 * s, '/Ac/L%d/Energy/Reverse' % n, 1000, '%.1f kWh')
]
def init_virtual(self):
mask = 0
for n in range(3):
if self.ct_phase[n]:
mask |= 1 << self.ct_phase[n][0].slot
self.write_register(Reg_s32l(0x1400), mask)
if not mask:
return
self.data_regs += [
Reg_f32l(0x03c0, '/Ac/Power', 1, '%.1f W'),
Reg_s32l(0x3100, '/Ac/Energy/Forward', 1000, '%.1f kWh'),
Reg_s32l(0x3102, '/Ac/Energy/Reverse', 1000, '%.1f kWh'),
]
def device_init(self):
self.info_regs = [
Reg_ser( 0x1620, '/Serial'),
Reg_ver( 0x1624, '/FirmwareVersion'),
Reg_f32l(0x03f6, '/Ac/FrequencyNominal', 1, '%.0f Hz'),
Reg_u16( 0x1180, '/PhaseConfig', write=True),
]
self.data_regs = [
Reg_f32l(0x03f8, '/Ac/Frequency', 1, '%.1f Hz'),
]
fw = self.read_register(self.info_regs[1])
if fw < self.min_fwver:
self.log.info('%s firmware %s is too old', self.productname, fw)
raise Exception()
# reset CT slot mapping
self.write_modbus(0x1140, list(range(MAX_CT_SLOTS)))
self.all_cts = []
self.ct_phase = [[], [], []]
self.voltage_regs = []
self.current_regs = []
self.power_regs = []
self.energy_regs = []
for n in range(MAX_BUS_DEVICES):
self.probe_device(n)
if not any(self.ct_phase):
self.log.info('No CTs configured, guessing')
for n in range(3):
if len(self.all_cts) > n:
ct = self.all_cts[n]
ct.set_phase(n)
self.ct_phase[n].append(ct)
for n in range(3):
if self.ct_phase[n]:
ct = self.ct_phase[n][0]
self.add_phase(n, ct)
self.data_regs += [
self.voltage_regs,
self.current_regs,
self.power_regs,
self.energy_regs,
]
self.nr_phases = len(self.voltage_regs)
self.init_virtual()
# save settings to Power Box flash
self.write_register(Reg_u16(0xfde8), 1)
time.sleep(0.25)
def ct_identify(self, ct, path, val):
ct.identify(val)
return False
def device_init_late(self):
super().device_init_late()
for ct in self.all_cts:
cb = partial(self.ct_identify, ct)
self.dbus.add_path('/CT/%d/Identify' % ct.slot, None,
writeable=True, onchangecallback=cb)
self.dbus.add_path('/CT/%d/Device' % ct.slot, ct.sdev)
self.dbus.add_path('/CT/%d/DeviceChannel' % ct.slot, ct.chan)
self.dbus.add_path('/CTTypes', CT_TYPES)
def dbus_write_register(self, reg, path, val):
super().dbus_write_register(reg, path, val)
self.sched_reinit()
def write_modbus(self, base, val):
self.modbus.write_registers(base, val, unit=self.unit)
models = {
5400: {
'model': 'MOD-VAC-1',
'handler': PowerBox,
},
}
probe.add_handler(probe.ModelRegister(Reg_u16(0x1620), models,
methods=['rtu', 'tcp'],
rates=[38400],
units=[61]))