-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysonoff.15m.py
executable file
·245 lines (191 loc) · 13 KB
/
mysonoff.15m.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env PYTHONIOENCODING=UTF-8 /usr/bin/python
# -*- coding: utf-8 -*-
#
# <xbar.title>MySonoff</xbar.title>
# <xbar.version>v1.0</xbar.version>
# <xbar.author>[email protected]</xbar.author>
# <xbar.author.github>pvdabeel</xbar.author.github>
# <xbar.desc>Control your Sonoff switches from the MacOS menubar</xbar.desc>
# <xbar.dependencies>python</xbar.dependencies>
#
# Licence: GPL v3
# Installation instructions:
# --------------------------
# Execute in terminal.app before running :
# sudo easy_install keyring
#
# Ensure you have xbar installed https://github.com/matryer/xbar/releases/latest
# Copy this file to your xbar plugins folder and chmod +x the file from your terminal in that folder
# Run xbar
_DEBUG_ = False
try: # Python 3 dependencies
from urllib.parse import urlencode
from urllib.request import Request, urlopen, build_opener
from urllib.request import ProxyHandler, HTTPBasicAuthHandler, HTTPHandler, HTTPError, URLError
except: # Python 2 dependencies
from urllib import urlencode
from urllib2 import Request, urlopen, build_opener
from urllib2 import ProxyHandler, HTTPBasicAuthHandler, HTTPHandler, HTTPError, URLError
import ast
import json
import sys
import datetime
import calendar
import base64
import math
import keyring # Cowboy access token is stored in OS X keychain
import getpass # Getting password without showing chars in terminal.app
import time
import os
import subprocess
import requests
import binascii
from datetime import date
from tinydb import TinyDB # Keep track of location and cowboy states
from os.path import expanduser
from collections import OrderedDict
import library.snf as sonoff
# Location where to store state files
home = expanduser("~")
state_dir = home+'/.state/mysonoff'
if not os.path.exists(state_dir):
os.makedirs(state_dir)
# The full path to this file
cmd_path = os.path.realpath(__file__)
# Nice ANSI colors
CEND = '\33[0m'
CRED = '\33[31m'
CGREEN = '\33[32m'
CYELLOW = '\33[33m'
CBLUE = '\33[36m'
# Support for OS X Dark Mode
DARK_MODE=True if os.getenv('XBARDarkMode','false') == 'true' else False
# Pretty printing
def justify(string):
return justify(string,10)
def justify(string,number):
length = len(string)
quot = (number - length ) // 4
rem = (number - length ) % 4
return string.ljust(length+rem,' ').ljust(length+rem+quot,'\t')
# Logo for both dark mode and regular mode
def app_print_logo():
if bool(DARK_MODE):
print ('|image=iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAAAAXNSR0IArs4c6QAAAIRlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAACQAAAAAQAAAJAAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAACSgAwAEAAAAAQAAACQAAAAAODYCaQAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAAVlpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xO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xu30bNJPiksJMngRxPuod9uJGWbmMmeIXMJLCXoD2xfkNBjlv2PMae7eFrfGR3oB2PZRwpzm1qLW4IrPYftAovfcs2QKeMApajlmLW/XHBj6L72fdknNe6HtIn8AAAAASUVORK5CYII=')
print('---')
# --------------------------
# The main function
# --------------------------
# The init function: Called to store your username and access_code in OS X Keychain on first launch
def init():
# Here we do the setup
# Store access_token in OS X keychain on first run
print ('Enter your Sonoff username:')
init_username = raw_input()
print ('Enter your Sonoff password:')
init_password = getpass.getpass()
init_access_token = None
try:
c = sonoff.Sonoff(init_username,init_password,'eu')
except HTTPError as e:
print ('Error contacting Sonoff servers. Try again later.')
print e
time.sleep(0.5)
return
except URLError as e:
print ('Error: Unable to connect. Check your connection settings.')
print e
return
except AttributeError as e:
print ('Error: Could not get an access token from Sonoff. Try again later.')
print e
return
keyring.set_password("mysonoff-bitbar","username",init_username)
keyring.set_password("mysonoff-bitbar","password",init_password)
init_password = ''
USERNAME = keyring.get_password("mysonoff-bitbar","username")
# --------------------------
# The main function
# --------------------------
def main(argv):
# CASE 1: init was called
if 'init' in argv:
init()
return
# CASE 2: init was not called, keyring not initialized
if bool(DARK_MODE):
color = '#FFFFFE'
info_color = '#C0C0C0'
else:
color = '#00000E'
info_color = '#616161'
if not USERNAME:
# restart in terminal calling init
app_print_logo()
print ('Login to Sonoff | refresh=true terminal=true bash="\'%s\'" param1="%s" color=%s' % (sys.argv[0], 'init', color))
return
# CASE 3: init was not called, keyring initialized, no connection (access code not valid)
try:
True
# create connection to cowboy account
PASSWORD = keyring.get_password("mysonoff-bitbar","password")
c = sonoff.Sonoff(USERNAME,PASSWORD,'eu')
devices = c.get_devices()
except:
app_print_logo()
print ('Login to Sonoff | refresh=true terminal=true bash="\'%s\'" param1="%s" color=%s' % (sys.argv[0], 'init', color))
return
# CASE 4: all ok, specific command for a specific switch received
if (len(argv) > 1) and not('debug' in argv):
if (argv[1] == "switch"):
if (len(argv) == 4):
c.switch(argv[3],argv[2],None)
elif (len(argv) == 5):
c.switch(argv[3],argv[2],int(argv[4]))
else:
print ('Wrong number of arguments: switch <id> <state:on/off> <optional:outlet>')
return
return
# CASE 5: all ok, all other cases
app_print_logo()
prefix = ''
# --------------------------------------------------
# DEBUG MENU
# --------------------------------------------------
if 'debug' in argv:
for i in devices:
print ('>>> device: %s - %s' % (i['name'], i['params']['switch']))
return
# --------------------------------------------------
# MENU
# --------------------------------------------------
devices_ordered = sorted(devices, key= lambda(i) : i['name'])
for i in devices_ordered:
outlets = i['uiid']
devid = i['deviceid']
name = i['name']
try:
if i['params']['switches']: # Multiple outlets on 1 switch
for d, j in enumerate(i['params']['switches']):
state = j['switch']
if (d < outlets):
outletnbr = d+1
outletname = name + ' (outlet ' + str(outletnbr) + ')\t'
if (state == 'on'):
print ('%s%s%s%s%s | refresh=true terminal=false shell="%s" param1=%s param2=%s param3=%s param4=%s color=%s' % (prefix, justify(outletname,41), CGREEN, state, CEND, cmd_path, 'switch', devid, 'off', d, color))
print ('%s%s%s%s%s | refresh=true alternate=true terminal=true shell="%s" param1=%s param2=%s param3=%s param4=%s color=%s' % (prefix, justify(outletname,41), CGREEN, state, CEND, cmd_path, 'switch', devid, 'off', d, color))
else:
print ('%s%s%s%s%s | refresh=true terminal=false shell="%s" param1=%s param2=%s param3=%s param4=%s color=%s' % (prefix, justify(outletname,41), CRED, state, CEND, cmd_path, 'switch', devid, 'on', d, color))
print ('%s%s%s%s%s | refresh=true alternate=true terminal=true shell="%s" param1=%s param2=%s param3=%s param4=%s color=%s' % (prefix, justify(outletname,41), CRED, state, CEND, cmd_path, 'switch', devid, 'on', d, color))
except: # Just one switch
state = i['params']['switch']
if (state == 'on'):
print ('%s%s%s%s%s | refresh=true terminal=false shell="%s" param1=%s param2=%s param3=%s color=%s' % (prefix, justify(name,41), CGREEN, state, CEND, cmd_path, 'switch', devid, 'off', color))
print ('%s%s%s%s%s | refresh=true alternate=true terminal=true shell="%s" param1=%s param2=%s param3=%s color=%s' % (prefix, justify(name,41), CGREEN, state, CEND, cmd_path, 'switch', devid, 'off', color))
else:
print ('%s%s%s%s%s | refresh=true terminal=false shell="%s" param1=%s param2=%s param3=%s color=%s' % (prefix, justify(name,41), CRED, state, CEND, cmd_path, 'switch', devid, 'on', color))
print ('%s%s%s%s%s | refresh=true alternate=true terminal=true shell="%s" param1=%s param2=%s param3=%s color=%s' % (prefix, justify(name,41), CRED, state, CEND, cmd_path, 'switch', devid, 'on', color))
if __name__ == '__main__':
main(sys.argv)