Skip to content

Commit

Permalink
fix(#175): passthrough with remaining parent/child state
Browse files Browse the repository at this point in the history
  • Loading branch information
MindFreeze committed Apr 15, 2024
1 parent 1ea68bc commit fc111a9
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ export class Chart extends LitElement {
const { parent, child } = connection;

if (!connection.calculating) {
connection.calculating = true;
[parent, child].forEach(ent => {
if (ent.type === 'remaining_child_state') {
this.connectionsByParent.get(ent)!.forEach(c => {
if (!c.ready) {
if (c.calculating) {
if (c.calculating && c !== connection) {
throw new Error('Circular reference detected in/near ' + JSON.stringify(ent));
}
c.calculating = true;
Expand All @@ -156,7 +157,7 @@ export class Chart extends LitElement {
if (ent.type === 'remaining_parent_state') {
this.connectionsByChild.get(ent)?.forEach(c => {
if (!c.ready) {
if (c.calculating) {
if (c.calculating && c !== connection) {
throw new Error('Circular reference detected in/near ' + JSON.stringify(ent));
}
c.calculating = true;
Expand Down Expand Up @@ -195,6 +196,7 @@ export class Chart extends LitElement {
}
connection.ready = true;
if (
!force &&
(child.type === 'remaining_parent_state' &&
(child.add_entities?.length || child.subtract_entities?.length) &&
childState === Infinity) ||
Expand Down Expand Up @@ -458,9 +460,8 @@ export class Chart extends LitElement {
if (!connections) {
throw new Error('Invalid entity config ' + JSON.stringify(entityConf));
}
const { parent } = connections[0];
const state = connections.reduce((sum, c) => (c.ready ? sum + c.state : Infinity), 0);
const parentEntity = this._getEntityState(parent);
const parentEntity = this._getEntityState(this._findRelatedRealEntity(connections, 'parents'));
const { unit_of_measurement } = normalizeStateValue(
this.config.unit_prefix,
0,
Expand All @@ -473,19 +474,26 @@ export class Chart extends LitElement {
if (!connections) {
throw new Error('Invalid entity config ' + JSON.stringify(entityConf));
}
const { child } = connections[0];
const state = connections.reduce((sum, c) => (c.ready ? sum + c.state : Infinity), 0);
const childEntity = this._getEntityState(child);
const childEntity = this._getEntityState(this._findRelatedRealEntity(connections, 'children'));
const { unit_of_measurement } = normalizeStateValue(
this.config.unit_prefix,
0,
this._getUnitOfMeasurement(childEntity.attributes.unit_of_measurement),
);
return { ...childEntity, state, attributes: { ...childEntity.attributes, unit_of_measurement } };
}

let entity = this.states[getEntityId(entityConf)];
if (!entity) {
if (entityConf.type === 'passthrough') {
console.log('entityConf', entityConf);
const connections = this.connectionsByParent.get(entityConf);
if (!connections) {
throw new Error('Invalid entity config ' + JSON.stringify(entityConf));
}
return this._getEntityState(connections[0].child);
}
throw new Error('Entity not found "' + getEntityId(entityConf) + '"');
}

Expand All @@ -501,6 +509,20 @@ export class Chart extends LitElement {
return entity;
}

private _findRelatedRealEntity(connections: ConnectionState[], direction: 'parents' | 'children') {
// find the first parent/child that is not type: passthrough
// a deep search on the first connection must be enough
const candidate = direction === 'parents' ? connections[0].parent : connections[0].child;
if (candidate.type !== 'passthrough') {
return candidate;
}
const deeperConnections = direction === 'parents' ? this.connectionsByChild.get(candidate) : this.connectionsByParent.get(candidate);
if (!deeperConnections) {
throw new Error('Invalid entity config ' + JSON.stringify(candidate));
}
return this._findRelatedRealEntity(deeperConnections, direction);
}

static get styles(): CSSResultGroup {
return styles;
}
Expand Down

0 comments on commit fc111a9

Please sign in to comment.