-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
115 lines (93 loc) · 3.14 KB
/
plugin.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
# We-connect - Volkswagen group - Domoticz plugin
# Tested with my VW ID.3
#
# Author: Andreotti 2024
#
# Uses: Python API for the Volkswagen WeConnect Services
# Web: https://github.com/tillsteinbach/WeConnect-python
#
# Setup We-connect API on Raspberry Pi
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# apt install python3-pip
# pip3 install weconnect-cli
#
# When you get this error: "externally-managed-environment"
# pip3 install weconnect-cli --break-system-packages
#
# Test:
# weconnect-cli --username 'email' --password 'my_password'
#
# P.S. the check for the right VIN is not working for some reason!
"""
<plugin key="We-connect" name="We-connect" author="Andreotti" version="1.0.1" >
<description>
<p>
VIN is optional. Use it if you have more then one car connected in your app.<br/>
On error, login again at: https://id.vwgroup.io/
</p>
Credentials:
</description>
<params>
<param field="Username" label="Username" width="150px"/>
<param field="Password" label="Password" width="150px" default="" password="true"/>
<param field="VIN" label="VIN (optional)" width="150px"/>
</params>
</plugin>
"""
import Domoticz
from weconnect import weconnect
class BasePlugin:
runAgain = 1
weConnect = None
def __init__(self):
return
def onStart(self):
self.weConnect = weconnect.WeConnect(username=Parameters["Username"], password=Parameters["Password"], updateAfterLogin=False, loginOnInit=False)
if (self.weConnect):
try:
self.weConnect.login()
except Exception as e:
Domoticz.Log(f"Error during weConnect update: {str(e)}")
return
if len(Devices) == 0:
Domoticz.Device(Name="Battery", Unit=1, Type=243, Subtype=6, Switchtype=0).Create()
Domoticz.Device(Name="Distance", Unit=2, TypeName="Custom", Options={"Custom": "1;km"}).Create()
Domoticz.Log("Created device: ")
def onStop(self):
return
def onDisconnect(self, Connection):
return
def onHeartbeat(self):
self.runAgain = self.runAgain - 1
if (self.runAgain > 0):
return
self.runAgain = 60 # 1x per 5 minuten checken (60 x 5 sec)
if (self.weConnect):
try:
self.weConnect.update()
except Exception as e:
Domoticz.Log(f"Error during weConnect update: {str(e)}")
return
try:
for vin, vehicle in self.weConnect.vehicles.items():
# if (Parameters["VIN"] == "" or vin == Parameters["VIN"]):
if "charging" in vehicle.domains and "batteryStatus" in vehicle.domains["charging"]:
Devices[1].Update(nValue=0, sValue=str(vehicle.domains["charging"]["batteryStatus"].currentSOC_pct.value))
Devices[2].Update(nValue=0, sValue=str(vehicle.domains["charging"]["batteryStatus"].cruisingRangeElectric_km.value))
except Exception as e:
Domoticz.Log(f"Error reading weConnect data: {str(e)}")
return
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()