Skip to content

Commit 73b6d70

Browse files
committed
fix(*): Fixed various issues.
1 parent fc2f298 commit 73b6d70

6 files changed

Lines changed: 107 additions & 82 deletions

File tree

hassio/BarWidget.qml

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,57 @@ NIconButton {
1616

1717
property var main: pluginApi?.mainInstance ?? null
1818

19+
// "Connected" — authenticated and live
20+
// "Disconnected" — was connected, now dropped (reconnecting)
21+
// "Connecting" — socket open but not yet authed (token present)
22+
// "Unconfigured" — no token set, nothing to attempt
23+
// "AuthFailed" — token rejected by HA
1924
readonly property string _status: {
20-
if (!!(root.main && !root.main.connected && !root.main.authFailed && root.main.haToken !== ""))
25+
if (!root.main)
26+
return "Unconfigured";
27+
if (root.main.haToken === "")
28+
return "Unconfigured";
29+
if (root.main.authFailed)
30+
return "AuthFailed";
31+
if (!root.main.connected)
2132
return "Disconnected";
22-
if (!!(root.main && (root.main.authFailed || root.main.haToken === "")))
33+
if (!root.main.authenticated)
2334
return "Connecting";
2435
return "Connected";
2536
}
2637

2738
readonly property string _statusLabel: {
28-
if (root._status === "Disconnected")
39+
switch (root._status) {
40+
case "Connected":
41+
return pluginApi?.tr("widget.status_connected");
42+
case "Disconnected":
2943
return pluginApi?.tr("widget.status_disconnected");
30-
if (root._status === "Connecting")
44+
case "Connecting":
3145
return pluginApi?.tr("widget.status_connecting");
32-
return pluginApi?.tr("widget.status_connected");
46+
case "AuthFailed":
47+
return pluginApi?.tr("widget.status_auth_failed");
48+
case "Unconfigured":
49+
return pluginApi?.tr("widget.status_unconfigured");
50+
default:
51+
return pluginApi?.tr("widget.status_unconfigured");
52+
}
3353
}
3454

3555
icon: "smart-home"
3656
colorFg: {
3757
switch (root._status) {
3858
case "Connected":
3959
return Color.mPrimary;
40-
case "Disconnected":
41-
return Color.mError;
4260
case "Connecting":
4361
return Color.mOnError;
44-
default:
62+
case "Disconnected":
63+
return Color.mError;
64+
case "AuthFailed":
4565
return Color.mError;
66+
case "Unconfigured":
67+
return Color.mOnSurfaceVariant;
68+
default:
69+
return Color.mOnSurfaceVariant;
4670
}
4771
}
4872

@@ -60,7 +84,7 @@ NIconButton {
6084

6185
implicitHeight: Style.barHeight
6286

63-
// Pulse animation while connecting
87+
// Pulse only when actively trying to connect (token present, socket live, not yet authed)
6488
SequentialAnimation on opacity {
6589
running: root._status === "Connecting"
6690
loops: Animation.Infinite
@@ -76,6 +100,6 @@ NIconButton {
76100
}
77101
}
78102

79-
// Snap back to full opacity when done
103+
// Snap back to full opacity when not connecting
80104
opacity: root._status !== "Connecting" ? 1.0 : opacity
81105
}

hassio/BrowserView.qml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ Item {
5858
}
5959
}
6060

61-
// Call it when entities load
6261
function _fetchAll() {
6362
root._loading = true;
6463
root.main.getAllStates(function (results) {
@@ -116,7 +115,7 @@ Item {
116115
}
117116
}
118117

119-
//Entity list
118+
// Entity list
120119
NScrollView {
121120
Layout.fillWidth: true
122121
Layout.fillHeight: true
@@ -132,8 +131,9 @@ Item {
132131
spacing: Style.marginS
133132

134133
delegate: Rectangle {
134+
id: entityRow
135135
width: ListView.view.width
136-
height: 56
136+
height: Style.rowHeightM
137137
color: Color.mSurfaceVariant
138138
radius: Style.radiusM
139139

@@ -171,8 +171,8 @@ Item {
171171
}
172172

173173
NIconButton {
174-
icon: parent.parent.pinned ? "pin-filled" : "pin"
175-
color: parent.parent.pinned ? Color.mTertiary : Color.mOutline
174+
icon: entityRow.pinned ? "pin-filled" : "pin"
175+
color: entityRow.pinned ? Color.mTertiary : Color.mOutline
176176

177177
onClicked: root._togglePin(model.entity_id)
178178
}

hassio/Main.qml

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ QtObject {
1414

1515
property ListModel entities: ListModel {}
1616

17+
// Fast entity_id → ListModel index lookup — rebuilt on every _populateEntities call
18+
property var _entityIndex: ({})
19+
1720
property int _msgId: 1
1821
property int _initialFetchId: -1
1922
property var _pendingCallbacks: ({})
@@ -40,19 +43,18 @@ QtObject {
4043

4144
onStatusChanged: function (status) {
4245
if (status === WebSocket.Open) {
43-
console.info("[HASS] WebSocket connected");
46+
Logger.i("HASS", "WebSocket connected");
4447
root.connected = true;
4548
root.authenticated = false;
4649
} else if (status === WebSocket.Closed) {
47-
console.warn("[HASS] WebSocket closed");
50+
Logger.w("HASS", "WebSocket closed");
4851
root.connected = false;
4952
root.authenticated = false;
50-
// Only retry if we aren't in an auth failure state
5153
if (!root.authFailed) {
5254
root._scheduleReconnect();
5355
}
5456
} else if (status === WebSocket.Error) {
55-
console.error("[HASS] WebSocket error");
57+
Logger.e("HASS", "WebSocket error");
5658
root.connected = false;
5759
root.authenticated = false;
5860
if (!root.authFailed) {
@@ -69,14 +71,14 @@ QtObject {
6971
root._authenticate();
7072
break;
7173
case "auth_ok":
72-
console.info("[HASS] Authenticated");
74+
Logger.i("HASS", "Authenticated");
7375
root.authenticated = true;
7476
root._resetReconnect();
7577
root._fetchStates();
7678
root._subscribeEvents();
7779
break;
7880
case "auth_invalid":
79-
console.error("[HASS] Auth failed — check your token");
81+
Logger.e("HASS", "Auth failed — check your token");
8082
root.authenticated = false;
8183
root.authFailed = true;
8284
root._resetReconnect();
@@ -92,7 +94,7 @@ QtObject {
9294
}
9395

9496
if (!data.success) {
95-
console.error("[HASS] Service call failed:", JSON.stringify(data.error));
97+
Logger.e("HASS", "Service call failed: " + JSON.stringify(data.error));
9698
}
9799

98100
if (root._pendingCallbacks[data.id]) {
@@ -116,7 +118,7 @@ QtObject {
116118
property Timer _reconnectTimer: Timer {
117119
repeat: false
118120
onTriggered: {
119-
console.warn("[HASS] Reconnect attempt", root._reconnectAttempts + 1);
121+
Logger.w("HASS", "Reconnect attempt " + (root._reconnectAttempts + 1));
120122
_socket.active = false;
121123
_socket.active = true;
122124
}
@@ -135,7 +137,7 @@ QtObject {
135137
}
136138

137139
function _fetchStates() {
138-
_initialFetchId = _nextId(); // Store the ID we are sending
140+
_initialFetchId = _nextId();
139141
_socket.sendTextMessage(JSON.stringify({
140142
id: _initialFetchId,
141143
type: "get_states"
@@ -154,10 +156,12 @@ QtObject {
154156
const pinned = pluginApi?.pluginSettings?.entities ?? [];
155157

156158
root.entities.clear();
159+
root._entityIndex = {};
157160

158161
for (const state of allStates) {
159162
if (!pinned.includes(state.entity_id))
160163
continue;
164+
const idx = root.entities.count;
161165
root.entities.append({
162166
entity_id: state.entity_id,
163167
friendly_name: state.attributes.friendly_name ?? state.entity_id,
@@ -169,26 +173,27 @@ QtObject {
169173
supports_brightness: _supportsColorMode(state.attributes.supported_color_modes, ["brightness", "color_temp", "hs", "xy", "rgb", "rgbw", "rgbww"]),
170174
supports_color_temp: _supportsColorMode(state.attributes.supported_color_modes, ["color_temp"])
171175
});
176+
root._entityIndex[state.entity_id] = idx;
172177
}
173178

174-
console.info("[HASS] entities model count:", root.entities.count);
179+
Logger.i("HASS", "Entities loaded: " + root.entities.count);
175180
}
176181

177182
function _handleStateChange(data) {
178183
const entity_id = data.entity_id;
179184
const newState = data.new_state;
180185
if (!newState)
181186
return;
182-
for (let i = 0; i < root.entities.count; i++) {
183-
if (root.entities.get(i).entity_id !== entity_id)
184-
continue;
185-
root.entities.setProperty(i, "state", newState.state);
186-
root.entities.setProperty(i, "unit", newState.attributes.unit_of_measurement ?? "");
187-
root.entities.setProperty(i, "brightness", newState.attributes.brightness ?? -1);
188-
root.entities.setProperty(i, "color_temp", newState.attributes.color_temp_kelvin ? Math.round(1000000 / newState.attributes.color_temp_kelvin) : (newState.attributes.color_temp ?? -1));
189-
root.entityUpdated(entity_id);
187+
188+
const i = root._entityIndex[entity_id];
189+
if (i === undefined)
190190
return;
191-
}
191+
192+
root.entities.setProperty(i, "state", newState.state);
193+
root.entities.setProperty(i, "unit", newState.attributes.unit_of_measurement ?? "");
194+
root.entities.setProperty(i, "brightness", newState.attributes.brightness ?? -1);
195+
root.entities.setProperty(i, "color_temp", newState.attributes.color_temp_kelvin ? Math.round(1000000 / newState.attributes.color_temp_kelvin) : (newState.attributes.color_temp ?? -1));
196+
root.entityUpdated(entity_id);
192197
}
193198

194199
function callService(domain, service, entity_id) {
@@ -225,7 +230,7 @@ QtObject {
225230
}
226231

227232
function reconnect() {
228-
console.info("[HASS] Manual reconnect initiated");
233+
Logger.i("HASS", "Manual reconnect initiated");
229234
root.authFailed = false;
230235
root._resetReconnect();
231236
root.connected = false;
@@ -237,7 +242,7 @@ QtObject {
237242
function _scheduleReconnect() {
238243
const delay = Math.min(root._reconnectBaseInterval * Math.pow(2, root._reconnectAttempts), root._reconnectMaxInterval);
239244
root._reconnectAttempts++;
240-
console.warn("[HASS] Scheduling reconnect in", delay / 1000, "seconds (attempt", root._reconnectAttempts, ")");
245+
Logger.w("HASS", "Reconnecting in " + (delay / 1000) + "s (attempt " + root._reconnectAttempts + ")");
241246
root._reconnectTimer.interval = delay;
242247
root._reconnectTimer.start();
243248
}
@@ -275,7 +280,7 @@ QtObject {
275280
}
276281

277282
property Timer _settingsDebounce: Timer {
278-
interval: 100
283+
interval: 300
279284
repeat: false
280285
onTriggered: {
281286
Logger.i("HASS", "Settings changed, reconnecting...");
@@ -285,6 +290,7 @@ QtObject {
285290
root.connected = false;
286291
root.authenticated = false;
287292
root.entities.clear();
293+
root._entityIndex = {};
288294
_socket.active = true;
289295
}
290296
}

0 commit comments

Comments
 (0)