Skip to content

Commit

Permalink
fix(#93): force update on date change
Browse files Browse the repository at this point in the history
  • Loading branch information
MindFreeze committed Feb 21, 2023
1 parent 19a329b commit 9e67fe4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class Chart extends LitElement {
// https://lit.dev/docs/components/properties/
@property({ attribute: false }) public hass!: HomeAssistant;
@property({ attribute: false }) public states!: HassEntities;
@property({ attribute: false }) public forceUpdateTs!: number;

@state() private config!: Config;
@state() private sections: SectionState[] = [];
Expand All @@ -34,6 +35,9 @@ export class Chart extends LitElement {
if (!this.config) {
return false;
}
if (changedProps.has('forceUpdateTs')) {
return true;
}
const now = Date.now();
if (this.config.throttle && now - this.lastUpdate < this.config.throttle) {
// woah there
Expand Down
8 changes: 6 additions & 2 deletions src/ha-sankey-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class SankeyChart extends SubscribeMixin(LitElement) {
@state() private states: HassEntities = {};
@state() private entityIds: string[] = [];
@state() private error?: Error | unknown;
@state() private forceUpdateTs?: number;

public hassSubscribe() {
if (!this.config.energy_date_selection) {
Expand Down Expand Up @@ -119,6 +120,7 @@ export class SankeyChart extends SubscribeMixin(LitElement) {
}
});
this.states = states;
this.forceUpdateTs = Date.now();
});
}),
];
Expand Down Expand Up @@ -163,7 +165,8 @@ export class SankeyChart extends SubscribeMixin(LitElement) {
const sources = (collection.prefs?.energy_sources || [])
.map(s => ({
...s,
ids: [s, ...(s.flow_from || [])].map(f => f.stat_energy_from)
ids: [s, ...(s.flow_from || [])]
.map(f => f.stat_energy_from)
.filter(id => {
if (!id || !this.hass.states[id]) {
if (id) {
Expand Down Expand Up @@ -259,7 +262,7 @@ export class SankeyChart extends SubscribeMixin(LitElement) {
const grid = sources.find(s => s.type === 'grid');
if (grid && grid?.flow_to?.length) {
// grid export
grid?.flow_to.forEach(({stat_energy_to}) => {
grid?.flow_to.forEach(({ stat_energy_to }) => {
sections[1].entities.unshift({
entity_id: stat_energy_to,
substract_entities: (grid.flow_from || []).map(e => e.stat_energy_from),
Expand Down Expand Up @@ -306,6 +309,7 @@ export class SankeyChart extends SubscribeMixin(LitElement) {
.hass=${this.hass}
.states=${this.config.energy_date_selection ? this.states : this.hass.states}
.config=${this.config}
.forceUpdateTs=${this.forceUpdateTs}
></sankey-chart-base>
${print_yaml && this.config.sections.length
? html`${until(renderError('', { ...this.config, autoconfig: undefined }, this.hass))}`
Expand Down

0 comments on commit 9e67fe4

Please sign in to comment.