forked from ithinkido/penplotter-webserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasmota.py
64 lines (59 loc) · 2.9 KB
/
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
import configparser
import requests
from flask_socketio import SocketIO, emit
# Read Configuration
config = configparser.ConfigParser()
config.read('config.ini')
TASMOTA_ENABLE = False
if (config.has_option('tasmota', 'tasmota_enable')):
TASMOTA_ENABLE = config['tasmota']['tasmota_ip']
TASMOTA_IP = False
if (config.has_option('tasmota', 'tasmota_enable')):
TASMOTA_IP = config['tasmota']['tasmota_ip']
def tasmota_setStatus(socketio, status):
if TASMOTA_ENABLE == 'true':
if status == 'on' or status == 'off':
try:
r = requests.get("http://{ip}/cm?cmnd=Power%20{status}".format(ip=TASMOTA_IP, status=status.capitalize() )).content
return r
except requests.exceptions.Timeout:
socketio.emit('error', {'data': 'Timeout while trying to contact Tasmota device'})
return print('Timeout while trying to contact Tasmota device')
# Maybe set up for a retry, or continue in a retry loop
except requests.exceptions.TooManyRedirects:
socketio.emit('error', {'data': 'TooManyRedirects while trying to contact Tasmota device'})
return print('TooManyRedirects')
# Tell the user their URL was bad and try a different one
except requests.exceptions.ConnectionError:
socketio.emit('error', {'data': 'Connection Error while trying to contact Tasmota device'})
return print('ConnectionError')
except requests.exceptions.RequestException as e:
# catastrophic error. bail.
socketio.emit('error', {'data': repr(e)})
raise SystemExit(e)
else:
print('Please use only on or off')
else:
return False
def tasmota_setToggle(socketio):
if TASMOTA_ENABLE == 'true':
try:
r = requests.get("http://{ip}/cm?cmnd=Power%20TOGGLE".format(ip=TASMOTA_IP)).content
return r
except requests.exceptions.Timeout:
socketio.emit('error', {'data': 'Timeout while trying to contact Tasmota device'})
return print('Timeout while trying to contact Tasmota device')
# Maybe set up for a retry, or continue in a retry loop
except requests.exceptions.TooManyRedirects:
socketio.emit('error', {'data': 'TooManyRedirects while trying to contact Tasmota device'})
return print('TooManyRedirects')
# Tell the user their URL was bad and try a different one
except requests.exceptions.ConnectionError:
socketio.emit('error', {'data': 'Connection Error while trying to contact Tasmota device'})
return print('ConnectionError')
except requests.exceptions.RequestException as e:
# catastrophic error. bail.
socketio.emit('error', {'data': repr(e)})
raise SystemExit(e)
else:
return False