-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd1112d
commit c8f9219
Showing
1 changed file
with
47 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,61 @@ | ||
import requests | ||
from requests.auth import HTTPBasicAuth | ||
import re | ||
import sys | ||
import json | ||
|
||
end_str = '\n\nIf you are having trouble, please visit https://github.com/Floplosion05/Shelly' | ||
errors = [] | ||
|
||
shelly25_relay_url = 'http://{0}/relay/{1}?{2}' | ||
shelly25_roller_url = 'http://{0}/roller/{1}?{2}' | ||
shelly25_relay_commands = {'turn' : ['on', 'off', 'toggle'], 'timer' : None} | ||
shelly25_roller_commands = {'go' : ['open', 'stop', 'close'], 'roller_pos' : None} | ||
shelly25_relay = {'url' : shelly25_relay_url, 'commands' : shelly25_relay_commands} | ||
shelly25_roller = {'url' : shelly25_roller_url, 'commands' : shelly25_relay_commands} | ||
devices = [shelly25_relay, shelly25_roller] | ||
#shelly25_relay = {'url' : 'http://{0}/relay/{1}?{2}', 'commands' : {'turn' : ['on', 'off', 'toggle'], 'timer' : None}} | ||
shelly25_roller = {'url' : 'http://{0}/roller/{1}?{2}', 'commands' : {'move' : ['open', 'stop', 'close'], 'pos' : ['to_pos']}} | ||
|
||
class Shelly25: | ||
class Shelly25_roller: | ||
|
||
def __init__(self, type, ip, command, errors, value = None): | ||
def __init__(self, ip): | ||
self.type = type | ||
self.ip = ip | ||
self.command = command | ||
if self.type == 'Roller': | ||
pass | ||
|
||
|
||
self.errors = errors | ||
|
||
def go(self, device): | ||
r = requests.get(device['url'].format(self.ip, self.channel, self.command)) | ||
pass | ||
|
||
def pos(self, device): | ||
r = requests.get('') | ||
def go(self, command, value = None, channel = 0): | ||
if (command in shelly25_roller['commands']['move']): | ||
if (value == None): | ||
r = requests.get(shelly25_roller['url'].format(self.ip, channel, 'go=' + command)) | ||
print(r.content.decode()) | ||
elif (value != None): | ||
try: | ||
if (1 <= value <= 120): | ||
r = requests.get(shelly25_roller['url'].format(self.ip, channel, 'turn=' + command + '&duration=' + str(value))) | ||
print(r.content.decode()) | ||
except Exception as ex: | ||
print('Failed with output: ' + str(ex)) | ||
print(r.content.decode()) | ||
|
||
elif (command in shelly25_roller['commands']['pos']): | ||
r = requests.get(shelly25_roller['url'].format(self.ip, channel, '')) | ||
try: | ||
if (r.json()['positioning'] == True): | ||
try: | ||
if (1 <= value <= 100): | ||
r = requests.get(shelly25_roller['url'].format(self.ip, channel, 'go=' + command + '&roller_pos=' + str(value))) | ||
print(r.content.decode()) | ||
except Exception as ex: | ||
print('Failed with output: ' + str(ex)) | ||
print(r.content.decode()) | ||
else: | ||
print('Device isnt calibrated, to calibrate use:\nx = Shelly25_roller\nx.calibrate()') | ||
except Exception as ex: | ||
print('Failed with output: ' + str(ex)) | ||
print(r.content.decode()) | ||
|
||
else: | ||
print('Didnt recognise command: ' + command) | ||
|
||
def calibrate(self): | ||
r = requests.get(shelly25_roller['url'].format(self.ip, '0/calibrate', '')) | ||
print(r.content.decode()) | ||
|
||
def error(self, code): | ||
exit('Device:\t' + self.ip + '\n' + self.errors[code] + '\nErrorcode: ' + str(code) + end_str) | ||
exit('Device:\t' + self.ip + '\n' + self.errors[code] + '\nErrorcode: ' + str(code) + end_str) | ||
|
||
if __name__ == '__main__': | ||
s = Shelly25_roller('192.168.xxx.xxx') | ||
s.go('to_pos', 100) |