Skip to content

Commit

Permalink
feat(#37): add reconcile_to: latest option
Browse files Browse the repository at this point in the history
  • Loading branch information
MindFreeze committed May 16, 2024
1 parent 569e451 commit 1aa42a8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ This card is intended to display connections between entities with numeric state
| Name | Type | Requirement | Default | Description |
| -------------------- | ------- | ------------ | ------------------- | ------------------------------------------- |
| should_be | string | **Required** | | Valid options are 'equal', 'equal_or_less', 'equal_or_more'
| reconcile_to | string | **Required** | | Valid options are 'min', 'max', 'mean'
| reconcile_to | string | **Required** | | Which value to display in case of inconsistency. Valid options are 'min', 'max', 'mean', 'latest

### Entity types

Expand Down
7 changes: 4 additions & 3 deletions src/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class Chart extends LitElement {
const unit_of_measurement = this._getUnitOfMeasurement(
entityConf.unit_of_measurement || entity.attributes.unit_of_measurement,
);
const normalized = normalizeStateValue(this.config.unit_prefix, Number(entity.state), unit_of_measurement);
const normalized = {...normalizeStateValue(this.config.unit_prefix, Number(entity.state), unit_of_measurement), last_updated: entity.last_updated};

if (entityConf.type === 'passthrough') {
normalized.state = this.connections
Expand Down Expand Up @@ -280,7 +280,7 @@ export class Chart extends LitElement {
entityConf,
'parents',
this.connectionsByChild.get(entityConf) ?? [],
conf => this._getMemoizedState(conf).state,
conf => this._getMemoizedState(conf),
);
if (reconciliations.size) {
shouldRecalc = true;
Expand All @@ -294,7 +294,7 @@ export class Chart extends LitElement {
entityConf,
'children',
this.connectionsByParent.get(entityConf) ?? [],
conf => this._getMemoizedState(conf).state,
conf => this._getMemoizedState(conf),
);
if (reconciliations.size) {
shouldRecalc = true;
Expand Down Expand Up @@ -613,6 +613,7 @@ export class Chart extends LitElement {
throw this.error;
}
this.entityStates.clear();
this.reconciledStates.clear();
const containerClasses = classMap({
container: true,
wide: !!this.config.wide,
Expand Down
17 changes: 12 additions & 5 deletions src/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ export function reconcileEntity(
entityConf: EntityConfigInternal,
direction: 'parents' | 'children',
connections: ConnectionState[],
getState: (entityConf: EntityConfigInternal) => number,
getState: (entityConf: EntityConfigInternal) => { state: number; last_updated: string },
) {
const reconcileConf = direction === 'parents' ? entityConf.parents_sum! : entityConf.children_sum!;
const reconciliations = new Map<EntityConfigInternal, number>();
const state = getState(entityConf);
const { state, last_updated } = getState(entityConf);
const relatedConfigs: EntityConfigInternal[] = [];
const connectionSide = direction === 'children' ? 'child' : 'parent';
const sum =
connections.reduce((sum, c) => {
relatedConfigs.push(c[connectionSide]);
return sum + getState(c[connectionSide]);
return sum + getState(c[connectionSide]).state;
}, 0) ?? 0;
const { should_be } = reconcileConf;
if (
Expand All @@ -36,13 +36,20 @@ export function reconcileEntity(
reconciled = (sum + state) / 2;
break;
case 'latest':
// @TODO
const entityLastUpdated = new Date(last_updated).getTime();
const relationIsFresher = relatedConfigs.some(relatedConf => {
const relatedLastUpdated = new Date(getState(relatedConf).last_updated).getTime();
return relatedLastUpdated > entityLastUpdated;
});
if (relationIsFresher) {
reconciled = sum;
}
break;
}
reconciliations.set(entityConf, reconciled);
relatedConfigs.forEach(relatedConf => {
// don't compute a scaling factor in advance, because it increases floating point error
reconciliations.set(relatedConf, getState(relatedConf) / sum * reconciled);
reconciliations.set(relatedConf, (getState(relatedConf).state / sum) * reconciled);
});
}
return reconciliations;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,5 @@ export interface ConnectionState {
export interface NormalizedState {
state: number;
unit_of_measurement?: string;
last_updated: string;
}

0 comments on commit 1aa42a8

Please sign in to comment.