-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroom_frame.py
More file actions
232 lines (164 loc) · 7.25 KB
/
Copy pathroom_frame.py
File metadata and controls
232 lines (164 loc) · 7.25 KB
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
import os
import sys
from PyQt5.QtWidgets import (
QWidget, QLabel, QPushButton,
QHBoxLayout, QVBoxLayout
)
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QFontMetrics
import qtawesome as qta
from configparser import ConfigParser
from PyQt5.QtGui import QColor
from PyQt5.QtCore import QSize
import qtawesome as qta
from configparser import ConfigParser
from PyQt5.QtGui import QColor
from PyQt5.QtCore import QSize
#from areas import HAControlUIRoom
def get_config_path():
if getattr(sys, 'frozen', False):
base_path = os.path.dirname(sys.executable)
else:
base_path = os.path.dirname(os.path.abspath(__file__))
return os.path.join(base_path, "colors_icons.ini")
config_path = get_config_path()
config_object = ConfigParser()
config_object.read(config_path)
if "colors" not in config_object:
print("No Colors file")
sys.exit(1)
color = config_object["colors"]["sensor_chart"]
available = config_object["colors"]["available"]
unavailable = config_object["colors"]["unavailable"]
background_slider = config_object["colors"]["elements_bg"]
border_bg = config_object["colors"]["elements_border"]
custom_rooms_colors = dict(config_object["custom_rooms_colors"])
default_color = custom_rooms_colors.get("default", "mdi:help-circle")
custom_rooms_icons = dict(config_object["custom_rooms_icons"])
default_icon = custom_rooms_icons.get("default", "mdi:help-circle")
custom_rooms_json = dict(config_object["custom_rooms_json"])
default_json = custom_rooms_json.get("default", "dummy.json")
class SensorRoomCard(QWidget):
ha_state_signal = pyqtSignal(dict)
def __init__(
self,
eid,
etype,
itype,
name,
ha_client,
TYPE_OBJECT_NAME_MAP,
on_tile_click=None,
on_icon_click=None
):
super().__init__()
self.eid = eid
self.ha = ha_client
self.itype = itype
self.setFixedHeight(130)
self.setAutoFillBackground(True)
self.setObjectName(TYPE_OBJECT_NAME_MAP.get(etype, "not_set"))
for key in custom_rooms_json:
if key in self.itype:
json_file = custom_rooms_json[key]
break
self.json_file = json_file
r, g, b = map(int, unavailable.split(","))
self.unavailable_color = QColor(r, g, b)
self.unavailable_color_rgb = f"rgb({r},{g},{b})"
r, g, b = map(int, available.split(","))
self.available_color = QColor(r, g, b)
self.available_color_rgb = f"rgb({r},{g},{b})"
r, g, b = map(int, available.split(","))
self.available_color = QColor(r, g, b)
self.available_color_rgba = f"rgba({r},{g},{b}, 150)"
r, g, b = map(int, border_bg.split(","))
self.border_bg = QColor(r, g, b)
self.border_bg_rgb = f"rgb({r},{g},{b})"
self.color_border = f"rgba({color}, 210)"
self.color_background = f"rgba({color}, 20)"
self.color_icon = f"rgba({color}, 250)"
self.color_background = f"rgba({color}, 20)"
if on_tile_click:
self.mousePressEvent = lambda e: on_tile_click(eid, etype, itype)
self._build_ui(name, etype, itype, on_icon_click)
self.ha_state_signal.connect(self._update_gui)
self.ha.register(self)
def _build_ui(self, name, etype, itype, on_icon_click):
outer = QHBoxLayout(self)
outer.setContentsMargins(0, 0, 0, 0)
self.frame = QWidget()
self.frame.setObjectName("light")
self.frame.setAutoFillBackground(True)
self.frame.setAttribute(Qt.WA_TransparentForMouseEvents, False)
main = QVBoxLayout(self.frame)
main.setContentsMargins(0, 0, 0, 0)
self.ico_button = QPushButton()
self.ico_button.clicked.connect(self.run_room)
color = default_color
for key in custom_rooms_colors:
if key in self.itype:
color = custom_rooms_colors[key]
break
r, g, b = map(int, color.split(","))
self.icon_color = QColor(r, g, b)
r, g, b = map(int, color.split(","))
self.border_rgba = f"rgba({r},{g},{b}, 100)"
r, g, b = map(int, color.split(","))
self.background_rgba = f"rgba({r},{g},{b}, 50)"
icon_name = default_icon
for key in custom_rooms_icons:
if key in self.itype:
icon_name = custom_rooms_icons[key]
break
icon_color = self.icon_color
self.ico_button.setIcon(qta.icon(icon_name, color=icon_color, scale_factor=0.7))
self.ico_button.setIconSize(QSize(60, 50))
self.name_label = QLabel(name)
self.name_label.setStyleSheet(f"background: transparent; color: {self.available_color_rgb}; border: 0px solid rgba(0,94,126, 250);")
metrics = QFontMetrics(self.name_label.font())
self.name_label.setText(metrics.elidedText(name, Qt.ElideRight, 250))
self.value_label = QLabel("...")
self.value_label.setStyleSheet(f"background: transparent; color: {self.available_color_rgba}; font-size: 16px; border: 0px solid rgba(0,94,126, 250);")
main.addStretch()
main.addWidget(self.name_label, alignment=Qt.AlignHCenter)
main.addWidget(self.ico_button, alignment=Qt.AlignHCenter)
main.addWidget(self.value_label, alignment=Qt.AlignHCenter)
main.addStretch()
outer.addWidget(self.frame)
def on_ha_state(self, eid, state_obj):
if eid != self.eid:
return
self.ha_state_signal.emit(state_obj)
def _update_gui(self, state_obj):
state = state_obj.get("state", "")
print(state)
# wartość
try:
val = float(state)
if "." in str(state):
val = round(val, 1)
self.value_label.setText(str(val))
except Exception:
if state == "unavailable":
state ="Niedostepny"
self.frame.setProperty("quality", "unavailable")
self.ico_button.setStyleSheet("background: #121217; color: #64d0e9; border: 1px solid rgba(0,94,126, 250);")
self.frame.style().unpolish(self.frame)
self.frame.style().polish(self.frame)
self.frame.update()
self.value_label.setText(str(state))
self.frame.setStyleSheet(f"background: {self.background_rgba}; border: 1px solid {self.border_rgba}; border-radius: 10px;")
self.ico_button.setStyleSheet(f"background: #121217; color: #64d0e9; border: 1px solid {self.border_rgba}; border-radius: 10px;")
self.ico_button.setFixedWidth(54)
self.frame.style().unpolish(self.frame)
self.frame.style().polish(self.frame)
self.frame.update()
def run_room(self):
print(self.json_file)
# self.room_window = HAControlUIRoom(json_path=self.json_file)
# self.room_window.show()
# self.room_window.raise_()
# self.room_window.activateWindow()
print(f"ico_button clicked! eid={self.eid}")
# tutaj możesz umieścić logikę, którą chcesz uruchomić