Skip to content

Commit

Permalink
Merge pull request #484 from flixlix/fix-watt-threshold
Browse files Browse the repository at this point in the history
fix: 🐛 no watt_threshold provided to displayValue
  • Loading branch information
flixlix authored Apr 14, 2024
2 parents 0f3e59a + 571315c commit 0cb1084
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "power-flow-card-plus",
"version": "0.2.0-414",
"version": "0.2.0",
"description": "A power flow card for Home Assistant",
"keywords": [
"home-assistant",
Expand Down
27 changes: 23 additions & 4 deletions src/components/battery.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { html } from "lit";
import { PowerFlowCardPlus } from "../power-flow-card-plus";
import { ConfigEntities } from "../power-flow-card-plus-config";
import { ConfigEntities, PowerFlowCardPlusConfig } from "../power-flow-card-plus-config";
import { displayValue } from "../utils/displayValue";

export const batteryElement = (
main: PowerFlowCardPlus,
config: PowerFlowCardPlusConfig,
{
battery,
entities,
Expand Down Expand Up @@ -52,7 +53,9 @@ export const batteryElement = (
battery.state_of_charge.state,
battery.state_of_charge.unit,
battery.state_of_charge.unit_white_space,
battery.state_of_charge.decimals
battery.state_of_charge.decimals,
undefined,
config.watt_threshold
)}
</span>`
: null}
Expand Down Expand Up @@ -87,7 +90,15 @@ export const batteryElement = (
}}
>
<ha-icon class="small" .icon=${"mdi:arrow-down"}></ha-icon>
${displayValue(main.hass, battery.state.toBattery)}</span
${displayValue(
main.hass,
battery.state.toBattery,
battery.unit,
battery.unit_white_space,
battery.decimals,
undefined,
config.watt_threshold
)}</span
>`
: ""}
${entities.battery?.display_state === "two_way" ||
Expand All @@ -110,7 +121,15 @@ export const batteryElement = (
}}
>
<ha-icon class="small" .icon=${"mdi:arrow-up"}></ha-icon>
${displayValue(main.hass, battery.state.fromBattery)}</span
${displayValue(
main.hass,
battery.state.fromBattery,
battery.unit,
battery.unit_white_space,
battery.decimals,
undefined,
config.watt_threshold
)}</span
>`
: ""}
</div>
Expand Down
11 changes: 7 additions & 4 deletions src/components/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { PowerFlowCardPlus } from "../power-flow-card-plus";
import { displayValue } from "../utils/displayValue";
import { generalSecondarySpan } from "./spans/generalSecondarySpan";
import { TemplatesObj } from "../type";
import { ConfigEntities } from "../power-flow-card-plus-config";
import { ConfigEntities, PowerFlowCardPlusConfig } from "../power-flow-card-plus-config";

export const gridElement = (
main: PowerFlowCardPlus,
config: PowerFlowCardPlusConfig,
{ entities, grid, templatesObj }: { entities: ConfigEntities; grid: any; templatesObj: TemplatesObj }
) => {
return html`<div class="circle-container grid">
Expand Down Expand Up @@ -35,7 +36,7 @@ export const gridElement = (
}
}}
>
${generalSecondarySpan(main.hass, main, templatesObj, grid, "grid")}
${generalSecondarySpan(main.hass, main, config, templatesObj, grid, "grid")}
<ha-icon .icon=${grid.icon}></ha-icon>
${(entities.grid?.display_state === "two_way" ||
entities.grid?.display_state === undefined ||
Expand All @@ -57,7 +58,8 @@ export const gridElement = (
}}
>
<ha-icon class="small" .icon=${"mdi:arrow-left"}></ha-icon>
${displayValue(main.hass, grid.state.toGrid)}
${displayValue(main.hass, grid.state.toGrid, grid.unit, grid.unit_white_space, grid.decimals, undefined, config.watt_threshold)}
</span>`
: null}
${((entities.grid?.display_state === "two_way" ||
Expand All @@ -80,7 +82,8 @@ export const gridElement = (
}
}}
>
<ha-icon class="small" .icon=${"mdi:arrow-right"}></ha-icon>${displayValue(main.hass, grid.state.fromGrid)}
<ha-icon class="small" .icon=${"mdi:arrow-right"}></ha-icon>
${displayValue(main.hass, grid.state.fromGrid, grid.unit, grid.unit_white_space, grid.decimals, undefined, config.watt_threshold)}
</span>`
: ""}
${grid.powerOutage?.isOutage && !grid.powerOutage?.entityGenerator ? html`<span class="grid power-outage">${grid.powerOutage.name}</span>` : ""}
Expand Down
2 changes: 1 addition & 1 deletion src/components/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const homeElement = (
}
}}
>
${generalSecondarySpan(main.hass, main, templatesObj, home, "home")}
${generalSecondarySpan(main.hass, main, config, templatesObj, home, "home")}
<ha-icon .icon=${home.icon}></ha-icon>
${homeUsageToDisplay}
<svg class="home-circle-sections">
Expand Down
2 changes: 1 addition & 1 deletion src/components/individualLeftBottomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const individualLeftBottomElement = (
}
}}
>
${individualSecondarySpan(hass, main, templatesObj, individualObj, 1, "left-bottom")}
${individualSecondarySpan(hass, main, config, templatesObj, individualObj, 1, "left-bottom")}
<ha-icon id="individual-left-bottom-icon" .icon=${individualObj?.icon}></ha-icon>
${individualObj?.field?.display_zero_state !== false || (individualObj?.state || 0) > (individualObj.displayZeroTolerance ?? 0)
? html` <span class="individual-bottom individual-left-bottom"
Expand Down
2 changes: 1 addition & 1 deletion src/components/individualLeftTopElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const individualLeftTopElement = (
}
}}
>
${individualSecondarySpan(main.hass, main, templatesObj, individualObj, 0, "left-top")}
${individualSecondarySpan(main.hass, main, config, templatesObj, individualObj, 0, "left-top")}
<ha-icon id="individual-left-top-icon" .icon=${individualObj.icon}></ha-icon>
${individualObj?.field?.display_zero_state !== false || (individualObj.state || 0) > (individualObj.displayZeroTolerance ?? 0)
? html` <span class="individual-top individual-left-top">
Expand Down
2 changes: 1 addition & 1 deletion src/components/individualRightBottomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const individualRightBottomElement = (
}
}}
>
${individualSecondarySpan(main.hass, main, templatesObj, individualObj, 3, "right-bottom")}
${individualSecondarySpan(main.hass, main, config, templatesObj, individualObj, 3, "right-bottom")}
<ha-icon id="individual-right-bottom-icon" .icon=${individualObj.icon}></ha-icon>
${individualObj?.field?.display_zero_state !== false || (individualObj.state || 0) > (individualObj.displayZeroTolerance ?? 0)
? html` <span class="individual-bottom individual-right-bottom">
Expand Down
2 changes: 1 addition & 1 deletion src/components/individualRightTopElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const individualRightTopElement = (
}
}}
>
${individualSecondarySpan(main.hass, main, templatesObj, individualObj, 2, "right-top")}
${individualSecondarySpan(main.hass, main, config, templatesObj, individualObj, 2, "right-top")}
<ha-icon id="individual-right-top-icon" .icon=${individualObj.icon}></ha-icon>
${individualObj?.field?.display_zero_state !== false || (individualObj.state || 0) > (individualObj.displayZeroTolerance ?? 0)
? html` <span class="individual-top individual-right-top">
Expand Down
2 changes: 1 addition & 1 deletion src/components/nonFossil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const nonFossilElement = (
}
}}
>
${generalSecondarySpan(main.hass, main, templatesObj, nonFossil, "low-carbon")}
${generalSecondarySpan(main.hass, main, config, templatesObj, nonFossil, "low-carbon")}
<ha-icon .icon=${nonFossil.icon} class="low-carbon"></ha-icon>
${entities.fossil_fuel_percentage?.display_zero_state !== false ||
(nonFossil.state.power || 0) > (entities.fossil_fuel_percentage?.display_zero_tolerance || 0)
Expand Down
17 changes: 14 additions & 3 deletions src/components/solar.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { html } from "lit";
import { PowerFlowCardPlus } from "../power-flow-card-plus";
import { ConfigEntities } from "../power-flow-card-plus-config";
import { ConfigEntities, PowerFlowCardPlusConfig } from "../power-flow-card-plus-config";
import { generalSecondarySpan } from "./spans/generalSecondarySpan";
import { displayValue } from "../utils/displayValue";
import { TemplatesObj } from "../type";

export const solarElement = (
main: PowerFlowCardPlus,
config: PowerFlowCardPlusConfig,
{
entities,
solar,
Expand All @@ -30,10 +31,20 @@ export const solarElement = (
}
}}
>
${generalSecondarySpan(main.hass, main, templatesObj, solar, "solar")}
${generalSecondarySpan(main.hass, main, config, templatesObj, solar, "solar")}
<ha-icon id="solar-icon" .icon=${solar.icon}></ha-icon>
${entities.solar?.display_zero_state !== false || (solar.state.total || 0) > 0
? html` <span class="solar"> ${displayValue(main.hass, solar.state.total as number)}</span>`
? html` <span class="solar">
${displayValue(
main.hass,
solar.state.total as number,
solar.state.unit,
solar.state.unit_white_space,
solar.state.decimals,
undefined,
config.watt_threshold
)}
</span>`
: ""}
</div>
</div>`;
Expand Down
2 changes: 2 additions & 0 deletions src/components/spans/baseSecondarySpan.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { html } from "lit";
import { PowerFlowCardPlusConfig } from "../../power-flow-card-plus-config";
import { PowerFlowCardPlus } from "../../power-flow-card-plus";
import { offlineStr } from "../../type";

type BaseSecondarySpan = {
main: PowerFlowCardPlus;
Expand All @@ -12,6 +13,7 @@ type BaseSecondarySpan = {
};

export const baseSecondarySpan = ({ main, className, template, value, entityId, icon }: BaseSecondarySpan) => {
if (value && offlineStr.includes(value)) return html``;
if (value || template) {
return html`<span
class="secondary-info ${className}"
Expand Down
5 changes: 4 additions & 1 deletion src/components/spans/generalSecondarySpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { HomeAssistant } from "custom-card-helpers";
import { displayValue } from "../../utils/displayValue";
import { PowerFlowCardPlus } from "../../power-flow-card-plus";
import { TemplatesObj } from "../../type";
import { PowerFlowCardPlusConfig } from "../../power-flow-card-plus-config";

export const generalSecondarySpan = (
hass: HomeAssistant,
main: PowerFlowCardPlus,
config: PowerFlowCardPlusConfig,
templatesObj: TemplatesObj,
field: {
secondary: {
Expand Down Expand Up @@ -36,7 +38,8 @@ export const generalSecondarySpan = (
field.secondary.unit,
field.secondary.unit_white_space,
field.secondary.decimals,
field.secondary.accept_negative
field.secondary.accept_negative,
config.watt_threshold
),
template: templatesObj[`${key}Secondary`],
})}`
Expand Down
5 changes: 4 additions & 1 deletion src/components/spans/individualSecondarySpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { displayValue } from "../../utils/displayValue";
import { isNumberValue } from "../../utils/utils";
import { baseSecondarySpan } from "./baseSecondarySpan";
import { IndividualObject } from "../../states/raw/individual/getIndividualObject";
import { PowerFlowCardPlusConfig } from "../../power-flow-card-plus-config";

export type IndividualKey = `left-top` | `left-bottom` | `right-top` | `right-bottom`;

export const individualSecondarySpan = (
hass: HomeAssistant,
main: PowerFlowCardPlus,
config: PowerFlowCardPlusConfig,
templatesObj: TemplatesObj,
individual: IndividualObject,
index: number,
Expand All @@ -26,7 +28,8 @@ export const individualSecondarySpan = (
individual?.secondary?.unit || undefined,
individual?.secondary.unit_white_space,
individual?.secondary.decimals || 0,
individual?.secondary.accept_negative || false
individual?.secondary.accept_negative || false,
config.watt_threshold
)
: undefined;

Expand Down
49 changes: 41 additions & 8 deletions src/power-flow-card-plus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,43 @@ export class PowerFlowCardPlus extends LitElement {
const homeUsageToDisplay =
entities.home?.override_state && entities.home.entity
? entities.home?.subtract_individual
? displayValue(this.hass, getEntityStateWatts(this.hass, entities.home.entity) - totalIndividualConsumption)
: displayValue(this.hass, getEntityStateWatts(this.hass, entities.home!.entity))
? displayValue(
this.hass,
getEntityStateWatts(this.hass, entities.home.entity) - totalIndividualConsumption,
entities.home?.unit_of_measurement,
entities.home?.unit_white_space,
undefined,
undefined,
this._config.watt_threshold
)
: displayValue(
this.hass,
getEntityStateWatts(this.hass, entities.home.entity),
entities.home?.unit_of_measurement,
entities.home?.unit_white_space,
undefined,
undefined,
this._config.watt_threshold
)
: entities.home?.subtract_individual
? displayValue(this.hass, totalHomeConsumption - totalIndividualConsumption || 0)
: displayValue(this.hass, totalHomeConsumption);
? displayValue(
this.hass,
totalHomeConsumption - totalIndividualConsumption || 0,
entities.home?.unit_of_measurement,
entities.home?.unit_white_space,
undefined,
undefined,
this._config.watt_threshold
)
: displayValue(
this.hass,
totalHomeConsumption,
entities.home?.unit_of_measurement,
entities.home?.unit_white_space,
undefined,
undefined,
this._config.watt_threshold
);

const totalLines =
grid.state.toHome +
Expand Down Expand Up @@ -468,7 +500,8 @@ export class PowerFlowCardPlus extends LitElement {
const getIndividualDisplayState = (field?: IndividualObject) => {
if (!field) return "";
if (field?.state === undefined) return "";
return displayValue(this.hass, field?.state, field?.unit, field?.unit_white_space, field?.decimals);
// return displayValue(this.hass, field?.state, field?.unit, field?.unit_white_space, field?.decimals);
return displayValue(this.hass, field?.state, field?.unit, field?.unit_white_space, field?.decimals, undefined, this._config.watt_threshold);
};

const individualKeys = ["left-top", "left-bottom", "right-top", "right-bottom"];
Expand Down Expand Up @@ -524,7 +557,7 @@ export class PowerFlowCardPlus extends LitElement {
templatesObj,
})}
${solar.has
? solarElement(this, {
? solarElement(this, this._config, {
entities,
solar,
templatesObj,
Expand Down Expand Up @@ -554,7 +587,7 @@ export class PowerFlowCardPlus extends LitElement {
: html``}
<div class="row">
${grid.has
? gridElement(this, {
? gridElement(this, this._config, {
entities,
grid,
templatesObj,
Expand All @@ -581,7 +614,7 @@ export class PowerFlowCardPlus extends LitElement {
? html`<div class="row">
<div class="spacer"></div>
${battery.has ? batteryElement(this, { battery, entities }) : html`<div class="spacer"></div>`}
${battery.has ? batteryElement(this, this._config, { battery, entities }) : html`<div class="spacer"></div>`}
${individualFieldLeftBottom
? individualLeftBottomElement(this, this.hass, this._config, {
displayState: getIndividualDisplayState(individualFieldLeftBottom),
Expand Down
3 changes: 3 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,6 @@ export type GridObject = {
};
};
};

export type OfflineStr = "unavailable" | "unknown";
export const offlineStr = ["unavailable", "unknown"];
16 changes: 7 additions & 9 deletions src/utils/displayNonFossilState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ export const displayNonFossilState = (
nonFossilFuelWatts = 0;
}
}
result = displayValue(hass, nonFossilFuelWatts, undefined, unitWhiteSpace);
} else {
let nonFossilFuelPercentage: number = 100 - (getEntityState(hass, entityFossil) ?? 0);
if (displayZeroTolerance) {
if (nonFossilFuelPercentage < displayZeroTolerance) {
nonFossilFuelPercentage = 0;
}
return displayValue(hass, nonFossilFuelWatts, undefined, unitWhiteSpace, 0, undefined, config.watt_threshold);
}
let nonFossilFuelPercentage: number = 100 - (getEntityState(hass, entityFossil) ?? 0);
if (displayZeroTolerance) {
if (nonFossilFuelPercentage < displayZeroTolerance) {
nonFossilFuelPercentage = 0;
}
result = displayValue(hass, nonFossilFuelPercentage, unitOfMeasurement, unitWhiteSpace, 0);
}
return result;
return displayValue(hass, nonFossilFuelPercentage, undefined, unitWhiteSpace, 0, undefined, config.watt_threshold);
};
Loading

0 comments on commit 0cb1084

Please sign in to comment.