forked from victronenergy/dbus-shelly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meter.py
267 lines (227 loc) · 13.7 KB
/
meter.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
import asyncio
import logging
from asyncio.exceptions import TimeoutError # Deprecated in 3.11
from dbus_next.aio import MessageBus
from __main__ import VERSION
from __main__ import __file__ as MAIN_FILE
from aiovelib.service import Service, IntegerItem, DoubleItem, TextItem
from aiovelib.service import TextArrayItem
from aiovelib.client import Monitor, ServiceHandler
from aiovelib.localsettings import SettingsService, Setting, SETTINGS_SERVICE
logger = logging.getLogger(__name__)
# Text formatters
unit_watt = lambda v: "{:.0f}W".format(v)
unit_volt = lambda v: "{:.1f}V".format(v)
unit_amp = lambda v: "{:.1f}A".format(v)
unit_kwh = lambda v: "{:.2f}kWh".format(v)
unit_productid = lambda v: "0x{:X}".format(v)
class LocalSettings(SettingsService, ServiceHandler):
pass
class Meter(object):
def __init__(self, bus_type):
self.bus_type = bus_type
self.monitor = None
self.service = None
self.servicePM = [None, None, None] # Array to hold services for instances 2, 3, and 4
self.destroyed = False
self.PMSettingsSetup = False
async def wait_for_settings(self):
try:
return await asyncio.wait_for(self.monitor.wait_for_service(SETTINGS_SERVICE), 5)
except TimeoutError:
pass
return None
def get_settings(self):
return self.monitor.get_service(SETTINGS_SERVICE)
async def start(self, host, port, data):
try:
mac = data['result']['mac']
fw = data['result']['fw_id']
except KeyError:
return False
bus = await MessageBus(bus_type=self.bus_type).connect()
self.monitor = await Monitor.create(bus, self.settings_changed)
settingprefix = '/Settings/Devices/shelly_' + mac + '1'
logger.info("Waiting for localsettings")
settings = await self.wait_for_settings()
if settings is None:
logger.error("Failed to connect to localsettings")
return False
logger.info("Connected to localsettings")
await settings.add_settings(
Setting(settingprefix + "/ClassAndVrmInstance", "grid:40", 0, 0, alias="instance"),
Setting(settingprefix + '/Position', 0, 0, 2, alias="position"),
Setting(settingprefix + '/DeviceType', "em", alias="deviceType")
)
role, instance = self.role_instance(settings.get_value(settings.alias("instance")))
DeviceType = settings.get_value(settings.alias("deviceType"))
self.service = await Service.create(bus, "com.victronenergy.{}.shelly_{}".format(role, mac) + '1')
self.service.add_item(TextItem('/Mgmt/ProcessName', MAIN_FILE))
self.service.add_item(TextItem('/Mgmt/ProcessVersion', VERSION))
self.service.add_item(TextItem('/Mgmt/Connection', f"WebSocket {host}:{port}"))
self.service.add_item(IntegerItem('/DeviceInstance', instance))
self.service.add_item(IntegerItem('/ProductId', 0xB034, text=unit_productid))
self.service.add_item(TextItem('/ProductName', "Shelly energy meter + 1"))
self.service.add_item(TextItem('/FirmwareVersion', fw))
self.service.add_item(IntegerItem('/Connected', 1))
self.service.add_item(IntegerItem('/RefreshTime', 100))
self.service.add_item(TextArrayItem('/AllowedRoles', ['grid', 'pvinverter', 'genset', 'acload']))
self.service.add_item(TextItem('/Role', role, writeable=True, onchange=self.role_changed))
self.service.add_item(TextArrayItem('/AllowedDevices', ['em', 'pm']))
self.service.add_item(TextItem('/DeviceType', DeviceType, writeable=True, onchange=self.device_changed))
if role == 'pvinverter':
self.service.add_item(IntegerItem('/Position',
settings.get_value(settings.alias("position")),
writeable=True, onchange=self.position_changed))
self.service.add_item(DoubleItem('/Ac/Energy/Forward', None, text=unit_kwh))
self.service.add_item(DoubleItem('/Ac/Energy/Reverse', None, text=unit_kwh))
self.service.add_item(DoubleItem('/Ac/Power', None, text=unit_watt))
for prefix in (f"/Ac/L{x}" for x in range(1, 4)):
self.service.add_item(DoubleItem(prefix + '/Voltage', None, text=unit_volt))
self.service.add_item(DoubleItem(prefix + '/Current', None, text=unit_amp))
self.service.add_item(DoubleItem(prefix + '/Power', None, text=unit_watt))
self.service.add_item(DoubleItem(prefix + '/Energy/Forward', None, text=unit_kwh))
self.service.add_item(DoubleItem(prefix + '/Energy/Reverse', None, text=unit_kwh))
return True
def destroy(self):
if self.PMSettingsSetup:
if self.service is not None:
self.service.__del__()
for service in self.servicePM:
if service is not None:
service.__del__()
self.service = None
self.servicePM = [None, None, None]
self.settings = None
self.destroyed = True
else:
if self.service is not None:
self.service.__del__()
self.service = None
self.settings = None
self.destroyed = True
async def pmSetup(self):
if self.PMSettingsSetup:
bus = await MessageBus(bus_type=self.bus_type).connect()
for instance_number in range(2, 4):
settingprefix = f'/Settings/Devices/shelly_{mac}{instance_number}'
await settings.add_settings(
Setting(settingprefix + "/ClassAndVrmInstance", "grid:40", 0, 0, alias="instance"),
Setting(settingprefix + '/Position', 0, 0, 2, alias="position"),
Setting(settingprefix + '/DeviceType', "pm", alias="deviceType") #defaults to PM given this device wouldnt exist on EM.
)
role, instance = self.role_instance(settings.get_value(settings.alias("instance")))
DeviceType = settings.get_value(settings.alias("deviceType"))
self.servicePM[instance_number - 2] = await Service.create(
bus, f"com.victronenergy.{role}.shelly_{mac}{instance_number}")
self.servicePM[instance_number - 2].add_item(TextItem('/Mgmt/ProcessName', MAIN_FILE))
self.servicePM[instance_number - 2].add_item(TextItem('/Mgmt/ProcessVersion', VERSION))
self.servicePM[instance_number - 2].add_item(TextItem('/Mgmt/Connection', f"WebSocket {host}:{port}"))
self.servicePM[instance_number - 2].add_item(IntegerItem('/DeviceInstance', instance + instance_number))
self.servicePM[instance_number - 2].add_item(IntegerItem('/ProductId', 0xB034, text=unit_productid))
self.servicePM[instance_number - 2].add_item(TextItem('/ProductName', f"Shelly energy meter + {instance_number}"))
self.servicePM[instance_number - 2].add_item(TextItem('/FirmwareVersion', fw))
self.servicePM[instance_number - 2].add_item(IntegerItem('/Connected', 1))
self.servicePM[instance_number - 2].add_item(IntegerItem('/RefreshTime', 100))
self.servicePM[instance_number - 2].add_item(TextArrayItem('/AllowedRoles', ['grid', 'pvinverter', 'genset', 'acload']))
self.servicePM[instance_number - 2].add_item(TextItem('/Role', role, writeable=True, onchange=self.role_changed))
self.servicePM[instance_number - 2].add_item(TextArrayItem('/AllowedDevices', ['pm'])) #not both here as ones switch 1 is set the rest must follow
self.servicePM[instance_number - 2].add_item(TextItem('/DeviceType', DeviceType, writeable=True, onchange=self.device_changed))
if role == 'pvinverter':
self.servicePM[instance_number - 2].add_item(IntegerItem('/Position',
settings.get_value(settings.alias("position")),
writeable=True, onchange=self.position_changed))
self.servicePM[instance_number - 2].add_item(DoubleItem('/Ac/Energy/Forward', None, text=unit_kwh))
self.servicePM[instance_number - 2].add_item(DoubleItem('/Ac/Energy/Reverse', None, text=unit_kwh))
self.servicePM[instance_number - 2].add_item(DoubleItem('/Ac/Power', None, text=unit_watt))
for prefix in (f"/Ac/L{x}" for x in range(1, 4)):
self.servicePM[instance_number - 2].add_item(DoubleItem(prefix + '/Voltage', None, text=unit_volt))
self.servicePM[instance_number - 2].add_item(DoubleItem(prefix + '/Current', None, text=unit_amp))
self.servicePM[instance_number - 2].add_item(DoubleItem(prefix + '/Power', None, text=unit_watt))
self.servicePM[instance_number - 2].add_item(DoubleItem(prefix + '/Energy/Forward', None, text=unit_kwh))
self.servicePM[instance_number - 2].add_item(DoubleItem(prefix + '/Energy/Reverse', None, text=unit_kwh))
self.PMSettingsSetup = True
async def update(self, data):
if self.service and data.get('method') == 'NotifyStatus':
if self.settings.get_value(settings.alias("DeviceType")) == 'em': #stock shelly_debus code
try:
d = data['params']['em:0']
except KeyError:
pass
else:
with self.service as s:
s['/Ac/L1/Voltage'] = d["a_voltage"]
s['/Ac/L2/Voltage'] = d["b_voltage"]
s['/Ac/L3/Voltage'] = d["c_voltage"]
s['/Ac/L1/Current'] = d["a_current"]
s['/Ac/L2/Current'] = d["b_current"]
s['/Ac/L3/Current'] = d["c_current"]
s['/Ac/L1/Power'] = d["a_act_power"]
s['/Ac/L2/Power'] = d["b_act_power"]
s['/Ac/L3/Power'] = d["c_act_power"]
s['/Ac/Power'] = d["a_act_power"] + d["b_act_power"] + d["c_act_power"]
try:
d = data['params']['emdata:0']
except KeyError:
pass
else:
with self.service as s:
s["/Ac/Energy/Forward"] = round(d["total_act"]/1000, 1)
s["/Ac/Energy/Reverse"] = round(d["total_act_ret"]/1000, 1)
s["/Ac/L1/Energy/Forward"] = round(d["a_total_act_energy"]/1000, 1)
s["/Ac/L1/Energy/Reverse"] = round(d["a_total_act_ret_energy"]/1000, 1)
s["/Ac/L2/Energy/Forward"] = round(d["b_total_act_energy"]/1000, 1)
s["/Ac/L2/Energy/Reverse"] = round(d["b_total_act_ret_energy"]/1000, 1)
s["/Ac/L3/Energy/Forward"] = round(d["c_total_act_energy"]/1000, 1)
s["/Ac/L3/Energy/Reverse"] = round(d["c_total_act_ret_energy"]/1000, 1)
elif self.settings.get_value(settings.alias("DeviceType")) == 'pm': #editted dbus_shelly code to work with PM1
if self.PMSettingsSetup:
for i, service in enumerate(self.servicePM):
try:
d = data['params']['switch:' + str(i+1)]
except KeyError:
pass
else:
with service as s:
avoltage = d.get("voltage")
if avoltage is not None:
s['/Ac/L1/Voltage'] = avoltage #could add option to getPhase from settings.
acurrent = d.get("current")
if acurrent is not None:
s['/Ac/L1/Current'] = acurrent #could add option to getPhase from settings.
apower = d.get("apower")
if apower is not None:
s['/Ac/L1/Power'] = apower #could add option to getPhase from settings.
s['/Ac/Power'] = apower
#to add the total energy lookups not sure if can do reverse
#aenergy = d.get("aenergy")
#s["/Ac/Energy/Forward"] = round(aenergy.get("total")/1000,1)
#s["/Ac/L1/Energy/Forward"] = round(aenergy.get("total")/1000,1)
else:
await self.pmSetup() #setups up an extra 3 devices in dbus service for the remaining three switches if not already setup ()
await self.update(data)
def role_instance(self, value):
val = value.split(':')
return val[0], int(val[1])
def settings_changed(self, service, values):
if service.alias("instance") in values:
self.destroy()
def role_changed(self, val):
if val not in ['grid', 'pvinverter', 'genset', 'acload']:
return False
settings = self.get_settings()
if settings is None:
return False
p = settings.alias("instance")
role, instance = self.role_instance(settings.get_value(p))
settings.set_value(p, "{}:{}".format(val, instance))
self.destroy() # restart
return True
def position_changed(self, val):
if not 0 <= val <= 2:
return False
settings = self.get_settings()
if settings is None:
return False
settings.set_value(settings.alias("position"), val)
return True