import
tkinter as tk
from
tkinter
import
*
from
tkinter
import
messagebox
import
requests
import
http.client
import
time
import
json
path
=
""
sonoff_url
=
'NOT_INIT'
class
RelayButton(tk.Button):
def
__init__(
self
, parent, url
=
"", on_image
=
None
, off_image
=
None
,
*
*
kwargs):
super
().__init__(parent, image
=
off_image, command
=
self
.toggle)
self
.url
=
url
self
._on
=
False
self
.on_image
=
on_image
self
.off_image
=
off_image
self
.label
=
Label(text
=
"
", fg="
Black
", font=("
Helvetica",
12
))
self
.label.place(x
=
10
,y
=
15
)
self
.update_clock()
self
.templabel
=
Label(text
=
"temp info"
, fg
=
"Black"
, font
=
(
"Helvetica"
,
12
))
self
.templabel.place(x
=
10
, y
=
40
)
self
.getTemp()
@property
def
on(
self
):
return
self
._on
@on
.setter
def
on(
self
, on):
self
._on
=
on
if
on:
requests.post(
f
"{self.url}cm?cmnd=Power On"
)
self
[
"image"
]
=
self
.on_image
else
:
requests.post(
f
"{self.url}cm?cmnd=Power Off"
)
self
[
"image"
]
=
self
.off_image
def
toggle(
self
):
self
.on
=
not
self
.on
def
update_clock(
self
):
now
=
time.strftime(
"%I:%M:%S %p "
+
' - '
+
"%x"
)
self
.label.configure(text
=
'Time/Date: '
+
now)
self
.after(
1000
,
self
.update_clock)
def
getTemp(
self
):
sonoff_url
=
SONOFF_Temp
sonR1
=
requests.get(sonoff_url)
x
=
json.loads(sonR1.text)
status
=
x[
"StatusSNS"
]
timestamp
=
status[
"Time"
]
device_names
=
list
(status.keys())[
1
:
-
1
]
temp_units
=
status[
"TempUnit"
]
for
name
in
device_names:
device
=
status[name]
self
.templabel.configure(text
=
'Living Room '
+
f
'Temperature={device["Temperature"]} {temp_units}'
)
self
.after(
5000
,
self
.getTemp)
def
new_button(name, url, parent, row, column,
*
*
kwargs):
on_image
=
tk.PhotoImage(
file
=
f
"{path}{name}_on.png"
)
off_image
=
tk.PhotoImage(
file
=
f
"{path}{name}_off.png"
)
button
=
RelayButton(parent, url, on_image, off_image,
*
*
kwargs)
button.grid(row
=
row, column
=
column, padx
=
50
, pady
=
110
)
return
button
def
main():
app
=
tk.Tk()
app.title(
'Home Relay Controls'
)
buttons
=
[
]
for
b
in
buttons:
b.on
=
False
app.mainloop()
if
__name__
=
=
"__main__"
:
main()