-
Notifications
You must be signed in to change notification settings - Fork 1
/
pysonoff.py
executable file
·384 lines (344 loc) · 16.8 KB
/
pysonoff.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# pySONOFF, a ewelink substitute for Linux by D.Sánchez
# This program is published under the EU-GPL, get your copy at
# https://joinup.ec.europa.eu/sites/default/files/custom-page/attachment/eupl_v1.2_en.pdf
# Based on the GTK3 Library in pyGObject for python and driven by the magnificent sonoff-python
# library by Lucien2K - https://github.com/lucien2k/sonoff-python
# You may have to install the dependencies from github. Please read the README file.
import gi, os, sonoff, configobj
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GObject
#Define a version number to be able to change it just in one place
global VERSION_NUMBER, WEB_PAGE
VERSION_NUMBER = "1.1"
WEB_PAGE="https://github.com/dsancheznet/pysonoff/"
class MainWindow(Gtk.Window):
"""
This class defines the main window of the application
"""
def __init__(self):
#Instantiate config file parser
self.cConfig = configobj.ConfigObj( os.path.expanduser("~")+'/.config/sonoff/default.config')
#-----------------------------------------------------------Construct UI
#Configure Main Window
Gtk.Window.__init__(self, title="pysonoff")
self.set_border_width( 5 )
self.set_default_size( int(self.cConfig['Width']), int(self.cConfig['Height']) )
#---------- Header Bar
#Configure a Headerbar
cHeaderBar = Gtk.HeaderBar()
cHeaderBar.set_show_close_button(True)
cHeaderBar.props.title = "pySONOFF " + VERSION_NUMBER
cHeaderBar.props.subtitle = "Your eWeLink replacement on Linux"
self.set_titlebar(cHeaderBar)
#Buttons for Headerbar
myConfigButton = Gtk.Button()
myConfigButton.props.relief = Gtk.ReliefStyle.NONE
myConfigButton.add( Gtk.Image.new_from_gicon( Gio.ThemedIcon( name="emblem-system-symbolic" ), Gtk.IconSize.BUTTON ) )
myConfigButton.connect( "clicked", self.onConfigButtonClicked )
myUpdateButton = Gtk.Button()
myUpdateButton.props.relief = Gtk.ReliefStyle.NONE
myUpdateButton.add( Gtk.Image.new_from_gicon( Gio.ThemedIcon( name="emblem-synchronizing-symbolic" ), Gtk.IconSize.BUTTON ) )
myUpdateButton.connect( "clicked", self.onUpdateButtonClicked )
#Create a switch for the headerbar
self.onoffSwitch = Gtk.Switch()
self.onoffSwitch.props.valign = Gtk.Align.CENTER
self.onoffSwitch.connect( "state-set", self.onSwitchChanged )
#Pack everything
cHeaderBar.pack_start( myConfigButton )
cHeaderBar.pack_end( self.onoffSwitch )
cHeaderBar.pack_end( myUpdateButton )
#----------Header Bar ***END***
#----------Scroller
#Create a scrollable container for the TextView
myScroller = Gtk.ScrolledWindow()
myScroller.set_border_width( 2 )
myScroller.set_policy( Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC )
#Setup treeview an it's data source
self.cTreeStore = Gtk.TreeStore( str, str, str, str, str, bool, int )
self.cTreeView = Gtk.TreeView( model= self.cTreeStore )
#First column
tmpRenderer = Gtk.CellRendererText()
tmpFirstColumn = Gtk.TreeViewColumn( "Name", tmpRenderer, text=0 )
self.cTreeView.append_column( tmpFirstColumn )
#Second column
tmpRenderer = Gtk.CellRendererText()
tmpSecondColumn = Gtk.TreeViewColumn( "ID", tmpRenderer, text=1 )
self.cTreeView.append_column( tmpSecondColumn )
#Third column
tmpRenderer = Gtk.CellRendererText()
tmpThirdColumn = Gtk.TreeViewColumn( "Mfr.", tmpRenderer, text=2 )
self.cTreeView.append_column( tmpThirdColumn )
#Fourth column
tmpRenderer = Gtk.CellRendererText()
tmpFourthColumn = Gtk.TreeViewColumn( "Model", tmpRenderer, text=3 )
self.cTreeView.append_column( tmpFourthColumn )
#Fifth column
tmpRenderer = Gtk.CellRendererText()
tmpFifthColumn = Gtk.TreeViewColumn( "FW", tmpRenderer, text=4 )
self.cTreeView.append_column( tmpFifthColumn )
#Sixth column
tmpRenderer = Gtk.CellRendererToggle()
tmpRenderer.connect("toggled", self.onSwitchToggled )
tmpSixthColumn = Gtk.TreeViewColumn( "State", tmpRenderer, active=5 )
self.cTreeView.append_column( tmpSixthColumn )
#Last column
tmpRenderer = Gtk.CellRendererProgress()
tmpRenderer.props.text = " "
tmpLastColumn = Gtk.TreeViewColumn( "RSSI", tmpRenderer, value=6, )
self.cTreeView.append_column( tmpLastColumn )
#Add treeview to scroller
myScroller.add( self.cTreeView )
#Add scroller to window
self.add( myScroller )
#---------- Scroller ***END***
#---------- Popover
#Define popover items
self.cEntryUsername = Gtk.Entry()
self.cEntryPassword = Gtk.Entry()
self.cEntryPassword.set_visibility( False )
self.cEntryApiKey = Gtk.Entry()
self.cEntryApiKey.set_editable( False )
self.cEntryToken = Gtk.Entry()
self.cEntryToken.set_editable( False )
self.cEntryRegion = Gtk.ComboBoxText()
self.cEntryRegion.append_text( "eu" )
self.cEntryRegion.append_text( "us" )
self.cEntryRegion.append_text( "cn" )
self.cEntryRegion.set_active(0)
self.cPopover = Gtk.Popover()
self.cPopover.set_border_width( 10 )
self.cUsernameLabel = Gtk.Label()
self.cUsernameLabel.set_text("Username")
self.cPasswordLabel = Gtk.Label()
self.cPasswordLabel.set_text("Password")
self.cRegionLabel = Gtk.Label()
self.cRegionLabel.set_text("Region")
self.cApiKeyLabel = Gtk.Label()
self.cApiKeyLabel.set_text("Apikey")
self.cTokenLabel = Gtk.Label()
self.cTokenLabel.set_text("Token")
#Pack popover
tmpVerticalBox = Gtk.Box( orientation= Gtk.Orientation.VERTICAL )
tmpHorizontalBox = Gtk.Box( orientation= Gtk.Orientation.HORIZONTAL)
tmpHorizontalBox.pack_start( self.cUsernameLabel, False, False, 2)
tmpVerticalBox.pack_start(tmpHorizontalBox, True, False, 2)
tmpVerticalBox.pack_start( self.cEntryUsername, True, False, 10 )
tmpHorizontalBox = Gtk.Box( orientation= Gtk.Orientation.HORIZONTAL)
tmpHorizontalBox.pack_start( self.cPasswordLabel, False, False, 2)
tmpVerticalBox.pack_start(tmpHorizontalBox, True, False, 2)
tmpVerticalBox.pack_start( self.cEntryPassword, True, False, 10 )
tmpHorizontalBox = Gtk.Box( orientation= Gtk.Orientation.HORIZONTAL)
tmpHorizontalBox.pack_start( self.cRegionLabel, False, False, 2)
tmpVerticalBox.pack_start(tmpHorizontalBox, True, False, 2)
tmpVerticalBox.pack_start( self.cEntryRegion, True, False, 10 )
tmpHorizontalBox = Gtk.Box( orientation= Gtk.Orientation.HORIZONTAL)
tmpHorizontalBox.pack_start( self.cApiKeyLabel, False, False, 2)
tmpVerticalBox.pack_start(tmpHorizontalBox, True, False, 2)
tmpVerticalBox.pack_start( self.cEntryApiKey, True, False, 10 )
tmpHorizontalBox = Gtk.Box( orientation= Gtk.Orientation.HORIZONTAL)
tmpHorizontalBox.pack_start( self.cTokenLabel, False, False, 2)
tmpVerticalBox.pack_start(tmpHorizontalBox, True, False, 2)
tmpVerticalBox.pack_start( self.cEntryToken, True, False, 10 )
self.cPopover.add( tmpVerticalBox )
#Connect popover
self.cPopover.connect( "closed", self.onPopoverClosed )
self.cPopover.set_position(Gtk.PositionType.BOTTOM)
#---------- Popover ***END***
#--------------------------------------------------Contruct UI ***END***
#Load config into popover fields
self.cLoadConfig()
#Save Window data before closing
self.connect( "delete-event", self.mainWindowDelete )
#Connect the destroy signal to the main loop quit function
self.connect("destroy", Gtk.main_quit )
#Recover old Position
self.move( int(self.cConfig['Pos_X']), int(self.cConfig['Pos_Y']) )
def mainWindowDelete( self, widget, data=None ):
self.cConfig['Width'] = self.get_size()[0]
self.cConfig['Height'] = self.get_size()[1]
self.cConfig['Pos_X'] = self.get_position()[0]
self.cConfig['Pos_Y'] = self.get_position()[1]
self.cConfig.write()
def onSwitchToggled( self, widget, path ):
#Establish the necessary state strings as it turns out that the switches do not accept True or False
tmpOnOff = [ 'off', 'on' ]
#Get position in list (iterator)
tmpIter = self.cTreeStore.get_iter(path)
#Invert checkbox
self.cTreeStore[path][5] = not self.cTreeStore[path][5]
#Do we have childs?
if self.cTreeStore.iter_has_child( tmpIter ):
#YES - Iterate over them
for tmpCount in range( self.cTreeStore.iter_n_children( tmpIter ) ):
#Set child checkbox to parent value
self.cTreeStore.set_value(
self.cTreeStore.iter_nth_child( tmpIter, tmpCount ),
5,
self.cTreeStore[path][5])
#Set switch to checkbox value
self.cHome.switch(
tmpOnOff[int( self.cTreeStore.get_value( self.cTreeStore.iter_nth_child( tmpIter, tmpCount ), 5 ) )],
self.cTreeStore.get_value( self.cTreeStore.iter_nth_child( tmpIter, tmpCount ), 1 ),
int( tmpCount ))
else:
#NO - Set the item to the inverted value
# Does this item hace childs (thus it is not a root item)?
if self.cTreeStore[path][0][0:6]=='Outlet':
#YES - Flick the switch
self.cHome.switch(
tmpOnOff[int( self.cTreeStore[path][5] )],
self.cTreeStore[path][1],
int( self.cTreeStore[path][0][-1:] ))
#Get the root node pointer
tmpRootIter = self.cTreeStore.iter_parent( tmpIter )
#Set a check variable
tmpCheck = True
#Check if all nodes are set
for tmpCount in range( self.cTreeStore.iter_n_children( tmpRootIter ) ):
if self.cTreeStore.get_value( self.cTreeStore.iter_nth_child( tmpRootIter, tmpCount ), 5) == False:
tmpCheck = False
break
if tmpCheck == False:
self.cTreeStore.set_value( self.cTreeStore.iter_parent( tmpIter ), 5, False )
else:
self.cTreeStore.set_value( self.cTreeStore.iter_parent( tmpIter ), 5, True )
else:
#NO - Flick the switch
self.cHome.switch(
tmpOnOff[int( self.cTreeStore[path][5] )],
self.cTreeStore[path][1])
def cLoadConfig( self ):
#Get config file parser
self.cConfig = configobj.ConfigObj( os.path.expanduser("~")+'/.config/sonoff/default.config')
#Read username
self.cEntryUsername.set_text( self.cConfig['Username'] )
#Read password
self.cEntryPassword.set_text( self.cConfig['Password'] )
#Read region
if self.cConfig['Region'] == 'eu':
self.cEntryRegion.set_active(0)
elif self.cConfig['Region'] == 'us':
self.cEntryRegion.set_active(1)
else:
self.cEntryRegion.set_active(2)
#Read API key
self.cEntryApiKey.set_text( self.cConfig['Apikey'] )
#Read Token
self.cEntryToken.set_text( self.cConfig['Token'] )
def onPopoverClosed( self, widget ):
#Are we connected?
if self.onoffSwitch.get_state() == False:
#NO - Write config
self.cConfig.write()
else:
#YES - Print out en error and do nothing (TODO: MessageDialog)
print("Cannot write config while connected (for security reasons)")
def onConfigButtonClicked( self, widget ):
#Position the popover
self.cPopover.set_relative_to( widget )
#Show it...
self.cPopover.show_all()
#Do your thing
self.cPopover.popup()
def onSwitchChanged( self, widget, state ):
if state == True :
#Clear old data
self.cTreeStore.clear()
#Instantiate sonoff with access data
self.cHome = sonoff.Sonoff(
self.cEntryUsername.get_text(),
self.cEntryPassword.get_text(),
self.cEntryRegion.get_active_text(),
self.cEntryApiKey.get_text()
#self.cEntryToken.get_text()
)
self.updateTreeView()
else:
#NO - The slider was turned off.
#Clear the TextView
self.cTreeStore.clear()
def onUpdateButtonClicked( self, widget ):
self.cHome.update_devices()
self.updateTreeView()
def updateTreeView( self ):
#Define hardware models
tmpModel = {
'Basic' : 1 ,
'Sonoff Touch EUC1' : 1 ,
'Touch EU' : 1 ,
'MINI' : 1 ,
'T1 2C' : 2 ,
'4CH' : 4}
tmpTrueFalse = {
'on' : True,
'off' : False }
#Remove old data
self.cTreeStore.clear()
#Iterate over devices
for tmpDevice in self.cHome.get_devices():
#Is yhe model known?
if tmpDevice['productModel'] not in tmpModel:
print('The current product is not found in my device list (' + tmpDevice['productModel'] + '). Please open an issue at ' + WEB_PAGE +'issues')
tmpModel.update( { 'Unknown' : 4} )
#Is the device a multiswitch?
if 'switches' in tmpDevice['params']:
#YES - Check if all childs are on or off
#Presume they are on
tmpChildsPosition = True
#Iterate over them
for tmpOutlet in tmpDevice['params']['switches']:
#Are the switches relevant? (Sometimes there are more outlets in the configuration then on the physical device)
if tmpOutlet['outlet'] < tmpModel[tmpDevice['productModel']]:
#YES - Construct a logical AND -> true + true = true; true + false = false. A false value will always be false.
tmpChildsPosition = tmpChildsPosition and tmpTrueFalse[tmpOutlet['switch']]
else:
#NO - Stop the loop
break
#Insert a parent with the result of the childs and save it's iter
tmpParent = self.cTreeStore.append(
None,
[
tmpDevice['name'],
tmpDevice['deviceid'],
tmpDevice['brandName'],
tmpDevice['productModel'],
tmpDevice['params']['fwVersion'],
tmpChildsPosition,
int( 130+tmpDevice['params']['rssi'] ) ] )
#Iterate over outlets
for tmpOutlet in tmpDevice['params']['switches']:
tmpArray = [
"Outlet "+str( tmpOutlet['outlet'] ),
tmpDevice['deviceid'],
"",
"",
"",
tmpTrueFalse[tmpOutlet['switch']],
int( 130+tmpDevice['params']['rssi'] ) ]
#Is the outlet relevant?
if tmpOutlet['outlet'] < tmpModel[tmpDevice['productModel']]:
#YES - Add the array to our treeview
self.cTreeStore.append( tmpParent, tmpArray )
else:
#NO - Only one outlet available
tmpArray = [
tmpDevice['name'],
tmpDevice['deviceid'],
tmpDevice['brandName'],
tmpDevice['productModel'],
tmpDevice['params']['fwVersion'],
tmpTrueFalse[tmpDevice['params']['switch']],
int( 130+tmpDevice['params']['rssi'] ) ]
# Add the array to the treeview
self.cTreeStore.append( None, tmpArray )
self.cTreeView.expand_all()
#Create Main Window
myWindow = MainWindow()
#Connect the destroy signal to the main loop quit function
myWindow.connect("destroy", Gtk.main_quit)
#Show Windows on screen
myWindow.show_all()
#Main Loop
Gtk.main()