-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.py
174 lines (146 loc) · 6.27 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
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
#
# Author: Philou55 sur base GizMoCuz
#
# Integration Domoticz de l ERL D2L pour le compteur Linky
#
"""
<plugin key="LinkyD2L" name="D2L pour Linky" author="Philou55" version="1.0.0">
<description>
Declarez le D2L avec vos identifiants ConsoSpy et vous obtiendrez
<ul style="list-style-type:square">
<li>votre index de relève en temps réel</li>
</ul>
</description>
<params>
<param field="Username" label="Identifiant" width="100px" required="true" default=""/>
<param field="Password" label="mot de passe" width="100px" required="true" default=""/>
<param field="Mode1" label="Scan Delay (mn)" width="20px" required="true" default="1"/>
<param field="Mode2" label="Debug Y/N" width="20px" required="true" default="N"/>
</params>
</plugin>
"""
import Domoticz
import json
import os
import datetime
#import requests
class BasePlugin:
# controle du heartbeat
flag01 = 0
nflag01 = 6
sMins = None
path = None
sUser = None
sPassword = None
sDebug = None
debug = False
def __init__(self):
#self.var = 123
return
def onStart(self):
self.flag01=0
# self.nflag01 = int(Parameters["Frequence"])
Domoticz.Heartbeat(20)
self.path=os.getcwd() + "/plugins/LinkyD2L"
self.sUser=Parameters["Username"]
self.sPassword=Parameters["Password"]
self.sMins=Parameters["Mode1"]
self.nflag01 = int(self.sMins) * 3
self.sDebug=Parameters["Mode2"].upper()
if self.sDebug == "Y" : self.debug=True
Domoticz.Log("START with (" + self.sUser+","+self.sPassword+") scan every "+self.sMins+" mn ("+str(self.nflag01)+ "HB) , Debug="+self.sDebug)
if(len(Devices)==0):
Domoticz.Device(Name="LKY_IndexKWH", Unit=1, TypeName="Custom", Used=1).Create()
Domoticz.Device(Name="LKY_Amperes", Unit=2, TypeName="Current/Ampere", Used=1).Create()
Domoticz.Device(Name="LKY_Watts", Unit=3, TypeName="kWh", Used=1).Create()
if self.debug: DumpConfigToLog()
def onStop(self):
Domoticz.Log("onStop called")
def onConnect(self, Connection, Status, Description):
Domoticz.Log("onConnect called")
def onMessage(self, Connection, Data):
Domoticz.Log("onMessage called")
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
def onDisconnect(self, Connection):
Domoticz.Log("onDisconnect called")
def onHeartbeat(self):
if self.flag01 == 0:
script=self.path + "/D2L.py"
res=os.system("python " + script + " " + self.sUser + " "+ self.sPassword)
if self.debug: Domoticz.Log(script+"Result is "+str(res))
fichier=(self.path + "/" + "D2L.json")
if os.path.isfile(fichier):
with open(fichier) as json_file:
data = json.load(json_file)
# Domoticz.Log(data['Heure']+"="+data['Index'])
# index recupere par GetIndexBetween
ix0=data['Index0'].split("=")
nValue=float(ix0[1])/1000.
#Domoticz.Log(str(nValue)+ " Kwh")
Devices[1].Update(nValue=int(data['Index']),sValue=str(nValue))
# intensite du courant
sValue2=data['Courant']+";0;0"
Devices[2].Update(nValue=int(data['Courant']),sValue=sValue2)
#Domoticz.Log(Devices[2].Name+" : "+sValue2)
# calcul de la puissance en WH
ix1=data['Index1'].split("=")
conso=int(ix0[1]) - int(ix1[1])
#Domoticz.Log(str(ix0[0]) + " , " + str(ix1[0]))
dat0 = datetime.datetime.strptime(ix0[0],'%Y-%m-%dT%H:%M:%S')
dat1 = datetime.datetime.strptime(ix1[0],'%Y-%m-%dT%H:%M:%S')
duree=(dat0-dat1).seconds
if int(duree) != 0 :
instantwatt=float(conso)*3600/float(duree)
#Domoticz.Log("Conso:"+str(conso)+" de "+ix1[0]+" a "+ix0[0]+" soit "+ str(duree)+ "==> " +str(instantwatt))
sValue3=str(instantwatt)+";"+str(nValue*1000)
Devices[3].Update(nValue=0,sValue=sValue3)
#Domoticz.Log(Devices[3].Name+" ==> "+sValue3)
# trace pour florent
# Domoticz.Log("Florent --> Lastindex : " + data['Heure'] + ":" + data['Index'] + ", IndexsBetween(0) : " + data['Index0'])
else:
Domoticz.Log("fichier D2L.txt inexistant")
self.flag01 = self.flag01 + 1
if self.flag01 >= self.nflag01: self.flag01=0
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onConnect(Connection, Status, Description):
global _plugin
_plugin.onConnect(Connection, Status, Description)
def onMessage(Connection, Data):
global _plugin
_plugin.onMessage(Connection, Data)
def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)
def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
global _plugin
_plugin.onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile)
def onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Log( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Log("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Log("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Log("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Log("Device Name: '" + Devices[x].Name + "'")
Domoticz.Log("Device nValue: " + str(Devices[x].nValue))
Domoticz.Log("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Log("Device LastLevel: " + str(Devices[x].LastLevel))
return