-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_tasmota.py
83 lines (68 loc) · 3.19 KB
/
device_tasmota.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
import device
import logging
import time
import elfeconstant
from devicecallback import DeviceCallback
class DeviceTasmota (device.Device):
def __init__(self, ref=""):
super().__init__("{0}.{1}".format (ref, __name__))
self.value = 0
self.acknoledge = False
def incomingMessage (self, mqtt, devicetype, device, topic, payload):
pass
def outgoingMessage(self, topic, payload):
self.broker.SendMessage (topic, payload)
def ems_callback (self, topic, payload):
data = payload.decode('utf-8')
if (data.find("success")) != -1:
self.logger.info ("device action ACK for {0} {1}".format (topic, payload))
self.acknoledge = True
return 0
else:
return 1
def ProcessError (self, equipement_pilote_ou_mesure_id):
pass
def Action (self, commande, equipement_pilote_ou_mesure_id):
if commande == elfeconstant.DEVICE_ACTION_ON:
self.logger.debug ("{0} commande:{1}".format(type(self), commande))
self.acknoledge = False
callback = DeviceCallback (self)
self.broker.RegisterCallback (self.deviceinfo[2], callback)
time.sleep (0.5)
self.outgoingMessage (self.deviceinfo[3], "ON")
begin = time.time ()
while not self.acknoledge:
time.sleep (0.5)
if (time.time() - begin > 5):
break
self.UpdateActivationTime (equipement_pilote_ou_mesure_id, time.time())
if not self.acknoledge:
self.logger.warning ("timeout waiting for acknoledge equipement_domotique {0}".format (self.equipement_domotique_id))
self.broker.UnRegisterCallback (self.deviceinfo[2])
self.ProcessError (equipement_pilote_ou_mesure_id)
result = -1
else:
self.logger.info ("Action acknoledged for equipement_domotique {0}".format (self.deviceinfo[1]))
result = 1
else:
self.logger.debug ("{0} commande:{1}".format(type(self), commande))
self.acknoledge = False
callback = DeviceCallback (self)
self.broker.RegisterCallback (self.deviceinfo[2], callback)
time.sleep (0.5)
self.outgoingMessage (self.deviceinfo[3], "OFF")
begin = time.time ()
while not self.acknoledge:
time.sleep (0.5)
if (time.time() - begin > 5):
break
if not self.acknoledge:
self.logger.warning ("timeout waiting for acknoledge equipement_domotique {0}".format (self.equipement_domotique_id))
self.broker.UnRegisterCallback (self.deviceinfo[2])
self.ProcessError (equipement_pilote_ou_mesure_id)
result = -1
else:
#self.UpdateActivationTime (equipement_pilote_ou_mesure_id, time.time())
self.logger.info ("Action acknoledged for equipement_domotique {0}".format (self.deviceinfo[1]))
result = 1
return result