forked from LeoJr2015/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
automationTest.py
121 lines (100 loc) · 3.58 KB
/
automationTest.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# automationTest.py
#
# Copyright 2014 Scott Thomson <scott@scott-Aspire-6930G>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import urllib
import urllib2
import json
def printJSON(data):
print json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
class DomoticzGateway:
def __init__(self,ip,port):
self.ip = ip
self.port = port
self.lights = []
def testConnection(self):
sunRiseValues = {'type' : 'command', 'param' : 'getSunRiseSet'}
result = self.getDomoticzData(sunRiseValues)
if 'status' in result.keys():
if result['status'] == 'OK':
print "Good Response"
active = True
else:
print "Bad Response"
active = False
return active
def getDomoticzData(self, request):
url = 'http://' + self.ip + ':' + self.port + '/json.htm'
data = urllib.urlencode(request)
url = url + '?' + data
req = urllib2.Request(url)
response = urllib2.urlopen(req)
page = response.read()
result = json.loads(page)
return result
def getLights(self):
getSwitchesValues = {'type' : 'command', 'param' : 'getlightswitches'}
result = self.getDomoticzData(getSwitchesValues)
if self.isCommandValid(result):
#printJSON(result['result'])
self.lights = result['result']
#for light in self.lights:
#print light
def commandLight(self,idx,state):
lightCommandValues = {'type' : 'command', 'param' : 'switchlight', 'idx':'','switchcmd':'','level':'0'}
lightCommandValues['idx'] = idx
lightCommandValues['switchcmd'] = state
result = self.getDomoticzData(lightCommandValues)
return self.isCommandValid(result)
def setLightLevel(self,idx,value):
if not self.lights == []:
print "We have lights"
else:
print "No Lights currently registered"
def isCommandValid(self,result):
if 'status' in result.keys():
if result['status'] == 'OK':
return True
else:
print "Invalid Command"
return False
def getLightIndex(self,name):
if not self.lights == []:
for light in self.lights:
if light['Name'] == name:
index = light['idx']
return index
else:
return -1
def main():
active = False
ip = '192.168.1.76'
port = '8080'
d = DomoticzGateway(ip,port)
print d.testConnection()
#d.commandLight('5','On')
d.getLights()
d.setLightLevel('5','50')
print "Light Index: ", d.getLightIndex('Landing')
return 0
if __name__ == '__main__':
main()