-
Notifications
You must be signed in to change notification settings - Fork 0
/
airpurifier2.py
executable file
·140 lines (102 loc) · 3.82 KB
/
airpurifier2.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
#!/usr/bin/env python3.6
import sys
try:
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QMessageBox
except ImportError:
print('PyQt5 is required to run this script')
sys.exit(1)
try:
from miio.airpurifier import AirPurifier, OperationMode
from miio import exceptions as miio_exceptions
except ImportError:
print('miio is required to run this script')
sys.exit(1)
from mainwindow import Ui_MainWindow
TOKEN = 'ffffffffffffffffffffffffffffffff'
AIR_PUR_IP = '192.168.1.111'
class AirPurifier2App(QMainWindow, Ui_MainWindow):
def init_device(self) -> AirPurifier or None:
try:
return AirPurifier(AIR_PUR_IP, TOKEN)
except ValueError:
QMessageBox.warning(self, 'Token error',
'Token is invalid.\nPlease check the device token')
sys.exit(1)
def init_state_combo(self):
self.stateComboBox.addItem('Idle (OFF)', OperationMode.Idle)
self.stateComboBox.addItem('AUTO', OperationMode.Auto)
self.stateComboBox.addItem('Favorite', OperationMode.Favorite)
self.stateComboBox.addItem('Sleep', OperationMode.Silent)
def __init__(self, *args, **kwargs):
super(AirPurifier2App, self).__init__(*args, **kwargs)
self.setupUi(self)
self.fav_change = False
self.dev = self.init_device()
self.init_state_combo()
self.check_dev()
self.stateComboBox.currentIndexChanged.connect(self.set_mode)
self.favSlider.sliderReleased.connect(self.set_fav_lvl)
self.favSlider.sliderPressed.connect(self.fav_changing)
self.favSlider.sliderReleased.connect(self.fav_changed)
self.timer = QTimer()
self.timer.timeout.connect(self.check_dev)
self.timer.start(2500)
def fav_changing(self):
self.fav_change = True
def fav_changed(self):
self.fav_change = False
def device_get_status(self) -> AirPurifier.status:
if self.dev is not None:
return self.dev.status()
return None
def check_response(self, resp: str):
if 'ok' in resp:
self.statusbar.showMessage('Response OK', 1000)
else:
self.statusbar.showMessage('Response NOK!', 1000)
def set_mode(self):
if self.dev is None:
return
op_mode = self.stateComboBox.itemData(self.stateComboBox.currentIndex())
if op_mode == self.device_get_status().mode:
return
self.check_response(self.dev.set_mode(op_mode))
def set_fav_lvl(self):
if self.dev is None:
return
lvl = window.favSlider.value()
if lvl == self.device_get_status().favorite_level:
return
self.check_response(self.dev.set_favorite_level(lvl))
def check_aqi(self):
if self.dev is None:
return
self.statusbar.showMessage(f'AQI: {self.dev.status().aqi}')
def check_state(self):
if self.dev is None:
return
dev_mode = self.device_get_status().mode
idx = self.stateComboBox.findData(dev_mode)
if idx != self.stateComboBox.currentIndex():
self.stateComboBox.setCurrentIndex(idx)
def check_fav_level(self):
if self.dev is None:
return
if not self.fav_change:
self.favSlider.setValue(self.dev.status().favorite_level)
def check_dev(self):
self.check_aqi()
self.check_state()
self.check_fav_level()
if __name__ == '__main__':
app = QApplication(sys.argv)
try:
window = AirPurifier2App()
window.show()
app.exec_()
except miio_exceptions.DeviceException as ex:
QMessageBox.warning(None, "Device error",
f'{ex}')
sys.exit(1)