-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartplug.py
More file actions
85 lines (61 loc) · 2.52 KB
/
smartplug.py
File metadata and controls
85 lines (61 loc) · 2.52 KB
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
import tplink_protocol as plugs
import cTime
# Predefined Smart Plug Commands
# For a full list of commands, consult tplink_commands.txt
commands = {'info' : '{"system":{"get_sysinfo":{}}}',
'on' : '{"system":{"set_relay_state":{"state":1}}}',
'off' : '{"system":{"set_relay_state":{"state":0}}}',
}
class Smartplug:
def __init__(self, ip):
self.ip = ip
plugJSON = plugs.send(self.ip, commands["info"])["system"]["get_sysinfo"]
self.state = plugJSON["relay_state"]
self.pastRuntime = int(plugJSON["on_time"])
self.maxOn = False
def set(self, newState):
if newState == 1:
self.on(self.ip)
elif newState == 0:
self.off(self.ip)
def on(self, ip):
if self.state == 0:
if plugs.send(self.ip, commands["on"])["system"]["set_relay_state"]["err_code"] == 0:
print(cTime.nowf() + " - ACTION: Lights On")
self.state = 1
return 0
else:
print(cTime.nowf() + " - ALERT: Failed to turn lights on", file=sys.stderr)
def off(self, ip):
if self.state == 1:
currRuntime = plugs.send(self.ip, commands["info"])["system"]["get_sysinfo"]["on_time"]
if plugs.send(self.ip, commands["off"])["system"]["set_relay_state"]["err_code"] == 0:
print(cTime.nowf() + " - ACTION: Lights Off")
self.state = 0
self.pastRuntime += int(currRuntime)
return 0
else:
print(cTime.nowf() + " - ALERT: Failed to turn lights off", file=sys.stderr)
def getState(self):
return self.state
def getRunTime(self):
return self.pastRuntime + int(getTimeOn)
def checkMaxOn(self):
if self.getRunTime() > 43200:
self.maxOn = True
def getMaxOn(self):
return self.maxOn
def timeReset(self):
plugJSON = plugs.send(self.ip, commands["info"])["system"]["get_sysinfo"]
currState = plugJSON["relay_state"]
if currState == 1:
plugs.send(self.ip, commands["off"])
self.pastRuntime = 0
self.maxOn = False
if currState == 1:
plugs.send(self.ip, commands["on"])
def updateState(self):
self.state = plugs.send(self.ip, commands["info"])["system"]["get_sysinfo"]["relay_state"]
return self.state
def getTimeOn(self):
return plugs.send(self.ip, commands["info"])["system"]["get_sysinfo"]["on_time"]