@@ -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