From 478539d58dff579ddc6b8231df88d6c3f0a93d09 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 16:59:06 -1000 Subject: [PATCH 01/10] Update src/data/history.ts Co-authored-by: Paulus Schoutsen --- src/data/history.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/data/history.ts b/src/data/history.ts index 26ecabfccb..6fcb8ad9f1 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -231,9 +231,9 @@ class HistoryStream { const purgeBeforePythonTime = (new Date().getTime() - 60 * 60 * this.hoursToShow * 1000) / 1000; const newHistory: HistoryStates = {}; - Object.keys(this.combinedHistory).forEach((entityId) => { + for (const entityId of Object.keys(this.combinedHistory)) { newHistory[entityId] = []; - }); + }; Object.keys(streamMessage.states).forEach((entityId) => { newHistory[entityId] = []; }); From 4091205b58b3aec9e93a1a3475b7fe07f50d2b54 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 16:59:33 -1000 Subject: [PATCH 02/10] Update src/data/history.ts Co-authored-by: Paulus Schoutsen --- src/data/history.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/data/history.ts b/src/data/history.ts index 6fcb8ad9f1..27a1f6ada9 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -238,7 +238,7 @@ class HistoryStream { newHistory[entityId] = []; }); Object.keys(newHistory).forEach((entityId) => { - let purgeOld = false; + let purgeOld = entityId in this.combinedHistory; if ( entityId in this.combinedHistory && entityId in streamMessage.states From e9c5e51f251f99a829f1a6ef93fd67303a34eab0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 17:00:40 -1000 Subject: [PATCH 03/10] review --- src/data/history.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/data/history.ts b/src/data/history.ts index 27a1f6ada9..2e296c1c22 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -233,11 +233,11 @@ class HistoryStream { const newHistory: HistoryStates = {}; for (const entityId of Object.keys(this.combinedHistory)) { newHistory[entityId] = []; - }; - Object.keys(streamMessage.states).forEach((entityId) => { + } + for (const entityId of Object.keys(streamMessage.states)) { newHistory[entityId] = []; - }); - Object.keys(newHistory).forEach((entityId) => { + } + for (const entityId of Object.keys(newHistory)) { let purgeOld = entityId in this.combinedHistory; if ( entityId in this.combinedHistory && @@ -277,7 +277,7 @@ class HistoryStream { break; } } - }); + } this.combinedHistory = newHistory; return this.combinedHistory; } From e4ebb320e5cc2fd2b31d3b97d6fe94c1f6bbd8c0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 17:01:23 -1000 Subject: [PATCH 04/10] review --- src/data/history.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/data/history.ts b/src/data/history.ts index 2e296c1c22..b717e0ed13 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -243,12 +243,11 @@ class HistoryStream { entityId in this.combinedHistory && entityId in streamMessage.states ) { + const lastCombined = this.combinedHistory[entityId][-1]; newHistory[entityId] = this.combinedHistory[entityId].concat( streamMessage.states[entityId] ); - if ( - streamMessage.states[entityId][0] > this.combinedHistory[entityId][-1] - ) { + if (streamMessage.states[entityId][0] > lastCombined) { // If the history is out of order we have to sort it. newHistory[entityId] = newHistory[entityId].sort( (a, b) => a.lu - b.lu From 51a69f10422f1160db04b55259b975525567fbd4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 17:02:15 -1000 Subject: [PATCH 05/10] review --- src/data/history.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/data/history.ts b/src/data/history.ts index b717e0ed13..035ec714c1 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -243,11 +243,12 @@ class HistoryStream { entityId in this.combinedHistory && entityId in streamMessage.states ) { - const lastCombined = this.combinedHistory[entityId][-1]; - newHistory[entityId] = this.combinedHistory[entityId].concat( + const entityCombinedHistory = this.combinedHistory[entityId]; + const lastEntityCombinedHistory = entityCombinedHistory[-1]; + newHistory[entityId] = entityCombinedHistory.concat( streamMessage.states[entityId] ); - if (streamMessage.states[entityId][0] > lastCombined) { + if (streamMessage.states[entityId][0] > lastEntityCombinedHistory) { // If the history is out of order we have to sort it. newHistory[entityId] = newHistory[entityId].sort( (a, b) => a.lu - b.lu From 45316b458f6db694d0054eaade0d9124eb9ea646 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 17:03:18 -1000 Subject: [PATCH 06/10] review --- src/data/history.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/data/history.ts b/src/data/history.ts index 035ec714c1..51f313ec83 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -244,7 +244,8 @@ class HistoryStream { entityId in streamMessage.states ) { const entityCombinedHistory = this.combinedHistory[entityId]; - const lastEntityCombinedHistory = entityCombinedHistory[-1]; + const lastEntityCombinedHistory = + entityCombinedHistory[entityCombinedHistory.length - 1]; newHistory[entityId] = entityCombinedHistory.concat( streamMessage.states[entityId] ); From e1d17f3d3ab8d3c51d453b6800b311b19fa4df91 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 17:03:39 -1000 Subject: [PATCH 07/10] review --- src/data/history.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/data/history.ts b/src/data/history.ts index 51f313ec83..216f764b29 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -249,7 +249,9 @@ class HistoryStream { newHistory[entityId] = entityCombinedHistory.concat( streamMessage.states[entityId] ); - if (streamMessage.states[entityId][0] > lastEntityCombinedHistory) { + if ( + streamMessage.states[entityId][0].lu > lastEntityCombinedHistory.lu + ) { // If the history is out of order we have to sort it. newHistory[entityId] = newHistory[entityId].sort( (a, b) => a.lu - b.lu From 690e2a3aa95cd8a180fc46602e3b560a26769cd1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 17:06:23 -1000 Subject: [PATCH 08/10] review --- src/data/history.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/data/history.ts b/src/data/history.ts index 216f764b29..2c2e93c609 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -238,7 +238,6 @@ class HistoryStream { newHistory[entityId] = []; } for (const entityId of Object.keys(newHistory)) { - let purgeOld = entityId in this.combinedHistory; if ( entityId in this.combinedHistory && entityId in streamMessage.states @@ -257,14 +256,13 @@ class HistoryStream { (a, b) => a.lu - b.lu ); } - purgeOld = true; } else if (entityId in this.combinedHistory) { newHistory[entityId] = this.combinedHistory[entityId]; - purgeOld = true; } else { newHistory[entityId] = streamMessage.states[entityId]; } - if (purgeOld) { + // Remove old history + if (entityId in this.combinedHistory) { const entityHistory = newHistory[entityId]; while (entityHistory[0].lu < purgeBeforePythonTime) { if (entityHistory.length > 1) { From 1e4a3c398bf83ba0eee999ab9e7fd2ac773538de Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 17:08:03 -1000 Subject: [PATCH 09/10] review --- src/data/history.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/data/history.ts b/src/data/history.ts index 2c2e93c609..1f95af2552 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -249,7 +249,7 @@ class HistoryStream { streamMessage.states[entityId] ); if ( - streamMessage.states[entityId][0].lu > lastEntityCombinedHistory.lu + streamMessage.states[entityId][0].lu < lastEntityCombinedHistory.lu ) { // If the history is out of order we have to sort it. newHistory[entityId] = newHistory[entityId].sort( From 9f9ab6c23889291ed1af9a248c4242ce033f9796 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 17:10:55 -1000 Subject: [PATCH 10/10] review --- src/dialogs/more-info/ha-more-info-history.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/dialogs/more-info/ha-more-info-history.ts b/src/dialogs/more-info/ha-more-info-history.ts index e6e03dc8c2..dd2a5c1165 100644 --- a/src/dialogs/more-info/ha-more-info-history.ts +++ b/src/dialogs/more-info/ha-more-info-history.ts @@ -164,6 +164,9 @@ export class MoreInfoHistory extends LitElement { if (!isComponentLoaded(this.hass, "history") || this._subscribed) { return; } + if (this._subscribed) { + this._unsubscribeHistoryTimeWindow(); + } this._subscribed = subscribeHistoryStatesTimeWindow( this.hass!, (combinedHistory) => {