Skip to content

Commit

Permalink
fix: 🐛 watt threshold on display value
Browse files Browse the repository at this point in the history
  • Loading branch information
flixlix committed Apr 14, 2024
1 parent 2072e63 commit 571315c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 92 deletions.
51 changes: 27 additions & 24 deletions src/components/battery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ export const batteryElement = (
}}
id="battery-state-of-charge-text"
>
${displayValue({
hass: main.hass,
value: battery.state_of_charge.state,
unit: battery.state_of_charge.unit,
unitWhiteSpace: battery.state_of_charge.unit_white_space,
decimals: battery.state_of_charge.decimals,
watt_threshold: config.watt_threshold,
})}
${displayValue(
main.hass,
battery.state_of_charge.state,
battery.state_of_charge.unit,
battery.state_of_charge.unit_white_space,
battery.state_of_charge.decimals,
undefined,
config.watt_threshold
)}
</span>`
: null}
<ha-icon
Expand Down Expand Up @@ -89,14 +90,15 @@ export const batteryElement = (
}}
>
<ha-icon class="small" .icon=${"mdi:arrow-down"}></ha-icon>
${displayValue({
hass: main.hass,
value: battery.state.toBattery,
unit: battery.unit,
unitWhiteSpace: battery.unit_white_space,
decimals: battery.decimals,
watt_threshold: config.watt_threshold,
})}</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 @@ -119,14 +121,15 @@ export const batteryElement = (
}}
>
<ha-icon class="small" .icon=${"mdi:arrow-up"}></ha-icon>
${displayValue({
hass: main.hass,
value: battery.state.fromBattery,
unit: battery.unit,
unitWhiteSpace: battery.unit_white_space,
decimals: battery.decimals,
watt_threshold: config.watt_threshold,
})}</span
${displayValue(
main.hass,
battery.state.fromBattery,
battery.unit,
battery.unit_white_space,
battery.decimals,
undefined,
config.watt_threshold
)}</span
>`
: ""}
</div>
Expand Down
18 changes: 2 additions & 16 deletions src/components/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,7 @@ export const gridElement = (
>
<ha-icon class="small" .icon=${"mdi:arrow-left"}></ha-icon>
${displayValue({
hass: main.hass,
value: grid.state.toGrid,
unit: grid.unit,
unitWhiteSpace: grid.unit_white_space,
decimals: grid.decimals,
watt_threshold: config.watt_threshold,
})}
${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 @@ -90,14 +83,7 @@ export const gridElement = (
}}
>
<ha-icon class="small" .icon=${"mdi:arrow-right"}></ha-icon>
${displayValue({
hass: main.hass,
value: grid.state.fromGrid,
unit: grid.unit,
unitWhiteSpace: grid.unit_white_space,
decimals: grid.decimals,
watt_threshold: config.watt_threshold,
})}
${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
17 changes: 9 additions & 8 deletions src/components/solar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ export const solarElement = (
<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({
hass: main.hass,
value: solar.state.total as number,
unit: solar.state.unit,
unitWhiteSpace: solar.state.unit_white_space,
decimals: solar.state.decimals,
watt_threshold: config.watt_threshold,
})}
${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>
Expand Down
3 changes: 2 additions & 1 deletion src/components/spans/generalSecondarySpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,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
3 changes: 2 additions & 1 deletion src/components/spans/individualSecondarySpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,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
61 changes: 37 additions & 24 deletions src/power-flow-card-plus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,23 +393,43 @@ export class PowerFlowCardPlus extends LitElement {
const homeUsageToDisplay =
entities.home?.override_state && entities.home.entity
? entities.home?.subtract_individual
? displayValue({
hass: this.hass,
value: getEntityStateWatts(this.hass, entities.home.entity) - totalIndividualConsumption,
watt_threshold: this._config.watt_threshold,
})
: displayValue({
hass: this.hass,
value: getEntityStateWatts(this.hass, entities.home.entity),
watt_threshold: this._config.watt_threshold,
})
? 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({
hass: this.hass,
value: totalHomeConsumption - totalIndividualConsumption || 0,
watt_threshold: this._config.watt_threshold,
})
: displayValue({ hass: this.hass, value: totalHomeConsumption, watt_threshold: this._config.watt_threshold });
? 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 @@ -481,14 +501,7 @@ export class PowerFlowCardPlus extends LitElement {
if (!field) return "";
if (field?.state === undefined) return "";
// return displayValue(this.hass, field?.state, field?.unit, field?.unit_white_space, field?.decimals);
return displayValue({
hass: this.hass,
value: field?.state,
unit: field?.unit,
unitWhiteSpace: field?.unit_white_space,
decimals: field?.decimals,
watt_threshold: this._config.watt_threshold,
});
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
18 changes: 2 additions & 16 deletions src/utils/displayNonFossilState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,13 @@ export const displayNonFossilState = (
nonFossilFuelWatts = 0;
}
}
return displayValue({
hass,
value: nonFossilFuelWatts,
unit: undefined,
unitWhiteSpace,
decimals: 0,
watt_threshold: config.watt_threshold,
});
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;
}
}
return displayValue({
hass,
value: nonFossilFuelPercentage,
unit: undefined,
unitWhiteSpace,
decimals: 0,
watt_threshold: config.watt_threshold,
});
return displayValue(hass, nonFossilFuelPercentage, undefined, unitWhiteSpace, 0, undefined, config.watt_threshold);
};
5 changes: 3 additions & 2 deletions src/utils/displayValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ export const displayValue = (
unit?: string | undefined,
unitWhiteSpace?: boolean | undefined,
decimals?: number | undefined,
accept_negative?: boolean | undefined
accept_negative?: boolean | undefined,
watt_threshold: number | undefined = 1000
): string => {
if (value === null) return "0";

if (!isNumberValue(value)) return value.toString();

const valueInNumber = Number(value);

const isKW = unit === undefined && valueInNumber >= 1000;
const isKW = unit === undefined && valueInNumber >= watt_threshold;

const transformValue = (v: number) => (!accept_negative ? Math.abs(v) : v);

Expand Down

0 comments on commit 571315c

Please sign in to comment.