-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartPanel.py
162 lines (122 loc) · 4.2 KB
/
SmartPanel.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
#!/usr/bin/python3
import asyncio
# SmartPanel - Smart Home control panel
# with Raspberry Pi and RPi Touch Screen
# Author: Stefan Haun <tux@netz39.de>
import signal
import sys
import configparser
from kivy.app import App
from kivy.config import Config
from kivy.lang import Builder
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.relativelayout import RelativeLayout
from shelly import ShellyButton
from thing import Thing, WifiRepeater
Builder.load_string('''
#:import BacklightControl backlight.BacklightControl
#:import MqttClient mqtt.MqttClient
#:import ClockWidget clock.ClockWidget
#:import EnvironmentWidget environment.EnvironmentWidget
#:import FavButtonWidget player.FavButtonWidget
#:import PlayerWidget player.PlayerWidget
#:import ShellyButton shelly.ShellyButton
<SmartPanelWidget>:
BacklightControl:
id: backlight
cfg: root.cfg
power: True
MqttClient:
id: mqtt
cfg: root.cfg
pos: [800-32, 460-48]
ClockWidget:
pos: [0, 200]
cfg: root.cfg
basepath: root.IMGDIR
touch_cb: None
EnvironmentWidget:
pos: [330, 200]
cfg: root.cfg
mqtt: root.mqtt
PlayerWidget:
pos: [330, 0]
cfg: root.cfg
mqtt: root.mqtt
FavButtonWidget:
pos: [700, 200]
cfg: root.cfg
mqtt: root.mqtt
''')
class SmartPanelWidget(RelativeLayout):
cfg = ObjectProperty()
mqtt = ObjectProperty()
IMGDIR = StringProperty("resources/nixie/")
def __init__(self, cfg, **kwargs):
super(SmartPanelWidget, self).__init__(**kwargs)
self.cfg = cfg
self.mqtt = self.ids.mqtt
# Initialize the things
for sec in filter(lambda s: s.startswith("Thing:"),
self.cfg.sections()):
t = Thing(cfg_name=sec,
cfg=self.cfg,
mqtt=self.mqtt)
self.bind(cfg=t.setter('cfg'))
self.bind(mqtt=t.setter('mqtt'))
self.add_widget(t)
for sec in filter(lambda s: s.startswith("Shelly"),
self.cfg.sections()):
sb = ShellyButton(cfg_name=sec,
cfg=self.cfg,
mqtt=self.mqtt)
self.bind(cfg=sb.setter('cfg'))
self.bind(mqtt=sb.setter('mqtt'))
self.add_widget(sb)
if "WifiRepeater" in self.cfg.sections():
self.wifi_repeater = WifiRepeater(self.cfg, self.mqtt,
pos=(700, 380))
self.add_widget(self.wifi_repeater)
def on_touch_down(self, touch):
if self.ids.backlight is not None and not self.ids.backlight.power:
block = not self.ids.backlight.power
# Switch on
self.ids.backlight.power = True
# Kill event when back-light is not active
if block:
return True
return super(SmartPanelWidget, self).on_touch_down(touch)
class SmartPanelApp(App):
def __init__(self, cfg, **kwargs):
super(SmartPanelApp, self).__init__(**kwargs)
self.cfg = cfg
def build(self):
widget = SmartPanelWidget(self.cfg,
size=(800, 480))
return widget
running = True
def sigint_handler(_signal, _frame):
global running
if running:
print("SIGINT received. Stopping the queue.")
running = False
else:
print("Receiving SIGINT the second time. Exit.")
sys.exit(0)
async def main():
signal.signal(signal.SIGINT, sigint_handler)
Config.set('kivy', 'default_font', [
' FiraSans-Regular',
'./resources/FiraSans-Regular.ttf',
'./resources/FiraSans-Regular.ttf',
'./resources/FiraSans-Regular.ttf',
'./resources/FiraSans-Regular.ttf'
])
config = configparser.ConfigParser()
config.read("smartpanel.cfg")
app = SmartPanelApp(config)
await app.async_run()
if __name__ == '__main__':
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())