diff --git a/README.md b/README.md index 0917bccf..1c588a7b 100644 --- a/README.md +++ b/README.md @@ -66,23 +66,23 @@ I recommend looking at the [Example usage section](#example-usage) to understand #### Entities object -All entites (except _battery_charge_) should have a `unit_of_measurement` attribute of W(watts) or kW(kilowatts). +At least one of _grid_, _battery_, or _solar_ is required. All entites (except _battery_charge_) should have a `unit_of_measurement` attribute of W(watts) or kW(kilowatts). -| Name | Type | Default | Description | -| -------------- | :------------------ | ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| grid | `string` / `object` | **required** | Entity ID of a sensor supporting a single state with negative values for production and positive values for consumption or an object for [split entites](#split-entities). Examples of both can be found below. | -| battery | `string` / `object` | | Entity ID of a sensor supporting a single state with negative values for production and positive values for consumption or an object for [split entites](#split-entities). Examples of both can be found below. | -| battery_charge | `string` | | Entity ID providing a state with the current percentage of charge on the battery. | -| solar | `string` | | Entity ID providing a state with the value of generation. | +| Name | Type | Description | +| -------------- | :------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| grid | `string` / `object` | Entity ID of a sensor supporting a single state with negative values for production and positive values for consumption or an object for [split entites](#split-entities). Examples of both can be found below. | +| battery | `string` / `object` | Entity ID of a sensor supporting a single state with negative values for production and positive values for consumption or an object for [split entites](#split-entities). Examples of both can be found below. | +| battery_charge | `string` | Entity ID providing a state with the current percentage of charge on the battery. | +| solar | `string` | Entity ID providing a state with the value of generation. | #### Split entities Can be use with either Grid or Battery configuration. The same `unit_of_measurement` rule as above applies. -| Name | Type | Default | Description | -| ----------- | -------- | --------------------- | ------------------------------------------------------------------------------------------------- | -| consumption | `string` | **required** for grid | Entity ID providing a state value for consumption, this is required if using a split grid object. | -| production | `string` | | Entity ID providing a state value for production | +| Name | Type | Description | +| ----------- | -------- | ------------------------------------------------------------------------------------------------- | +| consumption | `string` | Entity ID providing a state value for consumption, this is required if using a split grid object. | +| production | `string` | Entity ID providing a state value for production | ### Example usage diff --git a/src/power-flow-card.ts b/src/power-flow-card.ts index 8ff6cb26..08d8e3c6 100644 --- a/src/power-flow-card.ts +++ b/src/power-flow-card.ts @@ -46,6 +46,16 @@ export class PowerFlowCard extends LitElement { @query("#solar-home-flow") solarToHomeFlow?: SVGSVGElement; setConfig(config: PowerFlowCardConfig): void { + if ( + !config.entities || + (!config.entities.battery && + !config.entities.grid && + !config.entities.solar) + ) { + throw new Error( + "At least one entity for battery, grid or solar must be defined" + ); + } this._config = { ...config, inverted_entities: coerceStringArray(config.inverted_entities, ","), @@ -112,20 +122,26 @@ export class PowerFlowCard extends LitElement { const { entities } = this._config; + const hasGrid = entities.grid !== undefined; + const hasBattery = entities.battery !== undefined; const hasSolarProduction = entities.solar !== undefined; const hasReturnToGrid = - typeof entities.grid === "string" || entities.grid.production; + hasGrid && + (typeof entities.grid === "string" || entities.grid.production); let totalFromGrid = 0; - if (typeof entities.grid === "string") { - if (this.entityInverted("grid")) - totalFromGrid = Math.abs( - Math.min(this.getEntityStateWatts(entities.grid), 0) - ); - else totalFromGrid = Math.max(this.getEntityStateWatts(entities.grid), 0); - } else { - totalFromGrid = this.getEntityStateWatts(entities.grid.consumption); + if (hasGrid) { + if (typeof entities.grid === "string") { + if (this.entityInverted("grid")) + totalFromGrid = Math.abs( + Math.min(this.getEntityStateWatts(entities.grid), 0) + ); + else + totalFromGrid = Math.max(this.getEntityStateWatts(entities.grid), 0); + } else { + totalFromGrid = this.getEntityStateWatts(entities.grid.consumption); + } } let totalSolarProduction: number = 0; @@ -208,7 +224,6 @@ export class PowerFlowCard extends LitElement { batteryToGrid = returnedToGrid; } - //! Clean up this name let solarToGrid = 0; if (hasSolarProduction && returnedToGrid) solarToGrid = returnedToGrid - (batteryToGrid ?? 0); @@ -325,66 +340,63 @@ export class PowerFlowCard extends LitElement { ` : html``}
-
-
- - ${returnedToGrid !== null - ? html` + ${hasGrid + ? html`
+
+ + ${returnedToGrid !== null + ? html` + ${this.displayValue(returnedToGrid)} + ` + : null} + ${this.displayValue(returnedToGrid)} - ` - : null} - - ${this.displayValue(totalFromGrid)} - -
- ${this.hass.localize( - "ui.panel.lovelace.cards.energy.energy_distribution.grid" - )} -
+ >${this.displayValue(totalFromGrid)} +
+
+ ${this.hass.localize( + "ui.panel.lovelace.cards.energy.energy_distribution.grid" + )} +
` + : html`
`}
-
+
${this.displayValue(totalHomeConsumption)} - ${homeSolarCircumference !== undefined - ? html` - ${homeSolarCircumference !== undefined - ? svg` + ${homeSolarCircumference !== undefined + ? svg`` - : ""} - ${homeBatteryCircumference - ? svg`` - : ""} - - ` - : ""} + : ""} + +
${this.hass.localize( @@ -468,7 +479,9 @@ export class PowerFlowCard extends LitElement {
` : ""} -
- - - ${gridConsumption - ? svg` + + + ${gridConsumption + ? svg` ` - : ""} - -
+ : ""} + +
` + : null} ${hasBattery ? html`
${batteryConsumption @@ -619,7 +638,7 @@ export class PowerFlowCard extends LitElement {
` : ""} - ${hasBattery + ${hasGrid && hasBattery ? html`