diff --git a/package.json b/package.json index 2a45ea0..be83db1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/components/battery.ts b/src/components/battery.ts index c35944f..5e9b5cd 100644 --- a/src/components/battery.ts +++ b/src/components/battery.ts @@ -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, @@ -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 )} ` : null} @@ -87,7 +90,15 @@ export const batteryElement = ( }} > - ${displayValue(main.hass, battery.state.toBattery)}` : ""} ${entities.battery?.display_state === "two_way" || @@ -110,7 +121,15 @@ export const batteryElement = ( }} > - ${displayValue(main.hass, battery.state.fromBattery)}` : ""} diff --git a/src/components/grid.ts b/src/components/grid.ts index 04feaaa..2eedecb 100644 --- a/src/components/grid.ts +++ b/src/components/grid.ts @@ -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`
@@ -35,7 +36,7 @@ export const gridElement = ( } }} > - ${generalSecondarySpan(main.hass, main, templatesObj, grid, "grid")} + ${generalSecondarySpan(main.hass, main, config, templatesObj, grid, "grid")} ${(entities.grid?.display_state === "two_way" || entities.grid?.display_state === undefined || @@ -57,7 +58,8 @@ export const gridElement = ( }} > - ${displayValue(main.hass, grid.state.toGrid)} + + ${displayValue(main.hass, grid.state.toGrid, grid.unit, grid.unit_white_space, grid.decimals, undefined, config.watt_threshold)} ` : null} ${((entities.grid?.display_state === "two_way" || @@ -80,7 +82,8 @@ export const gridElement = ( } }} > - ${displayValue(main.hass, grid.state.fromGrid)} + + ${displayValue(main.hass, grid.state.fromGrid, grid.unit, grid.unit_white_space, grid.decimals, undefined, config.watt_threshold)} ` : ""} ${grid.powerOutage?.isOutage && !grid.powerOutage?.entityGenerator ? html`${grid.powerOutage.name}` : ""} diff --git a/src/components/home.ts b/src/components/home.ts index 62f5043..cd40446 100644 --- a/src/components/home.ts +++ b/src/components/home.ts @@ -50,7 +50,7 @@ export const homeElement = ( } }} > - ${generalSecondarySpan(main.hass, main, templatesObj, home, "home")} + ${generalSecondarySpan(main.hass, main, config, templatesObj, home, "home")} ${homeUsageToDisplay} diff --git a/src/components/individualLeftBottomElement.ts b/src/components/individualLeftBottomElement.ts index a7df3ea..fa17019 100644 --- a/src/components/individualLeftBottomElement.ts +++ b/src/components/individualLeftBottomElement.ts @@ -62,7 +62,7 @@ export const individualLeftBottomElement = ( } }} > - ${individualSecondarySpan(hass, main, templatesObj, individualObj, 1, "left-bottom")} + ${individualSecondarySpan(hass, main, config, templatesObj, individualObj, 1, "left-bottom")} ${individualObj?.field?.display_zero_state !== false || (individualObj?.state || 0) > (individualObj.displayZeroTolerance ?? 0) ? html` - ${individualSecondarySpan(main.hass, main, templatesObj, individualObj, 0, "left-top")} + ${individualSecondarySpan(main.hass, main, config, templatesObj, individualObj, 0, "left-top")} ${individualObj?.field?.display_zero_state !== false || (individualObj.state || 0) > (individualObj.displayZeroTolerance ?? 0) ? html` diff --git a/src/components/individualRightBottomElement.ts b/src/components/individualRightBottomElement.ts index 6b01f5e..3a33906 100644 --- a/src/components/individualRightBottomElement.ts +++ b/src/components/individualRightBottomElement.ts @@ -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")} ${individualObj?.field?.display_zero_state !== false || (individualObj.state || 0) > (individualObj.displayZeroTolerance ?? 0) ? html` diff --git a/src/components/individualRightTopElement.ts b/src/components/individualRightTopElement.ts index 1f80389..8628b06 100644 --- a/src/components/individualRightTopElement.ts +++ b/src/components/individualRightTopElement.ts @@ -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")} ${individualObj?.field?.display_zero_state !== false || (individualObj.state || 0) > (individualObj.displayZeroTolerance ?? 0) ? html` diff --git a/src/components/nonFossil.ts b/src/components/nonFossil.ts index 89dfc53..a47cb8c 100644 --- a/src/components/nonFossil.ts +++ b/src/components/nonFossil.ts @@ -37,7 +37,7 @@ export const nonFossilElement = ( } }} > - ${generalSecondarySpan(main.hass, main, templatesObj, nonFossil, "low-carbon")} + ${generalSecondarySpan(main.hass, main, config, templatesObj, nonFossil, "low-carbon")} ${entities.fossil_fuel_percentage?.display_zero_state !== false || (nonFossil.state.power || 0) > (entities.fossil_fuel_percentage?.display_zero_tolerance || 0) diff --git a/src/components/solar.ts b/src/components/solar.ts index 0303d45..a92fe6e 100644 --- a/src/components/solar.ts +++ b/src/components/solar.ts @@ -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, @@ -30,10 +31,20 @@ export const solarElement = ( } }} > - ${generalSecondarySpan(main.hass, main, templatesObj, solar, "solar")} + ${generalSecondarySpan(main.hass, main, config, templatesObj, solar, "solar")} ${entities.solar?.display_zero_state !== false || (solar.state.total || 0) > 0 - ? html` ${displayValue(main.hass, solar.state.total as number)}` + ? html` + ${displayValue( + main.hass, + solar.state.total as number, + solar.state.unit, + solar.state.unit_white_space, + solar.state.decimals, + undefined, + config.watt_threshold + )} + ` : ""}
`; diff --git a/src/components/spans/baseSecondarySpan.ts b/src/components/spans/baseSecondarySpan.ts index a48b6d5..d817875 100644 --- a/src/components/spans/baseSecondarySpan.ts +++ b/src/components/spans/baseSecondarySpan.ts @@ -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; @@ -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` { 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"]; @@ -524,7 +557,7 @@ export class PowerFlowCardPlus extends LitElement { templatesObj, })} ${solar.has - ? solarElement(this, { + ? solarElement(this, this._config, { entities, solar, templatesObj, @@ -554,7 +587,7 @@ export class PowerFlowCardPlus extends LitElement { : html``}
${grid.has - ? gridElement(this, { + ? gridElement(this, this._config, { entities, grid, templatesObj, @@ -581,7 +614,7 @@ export class PowerFlowCardPlus extends LitElement { ? html`
- ${battery.has ? batteryElement(this, { battery, entities }) : html`
`} + ${battery.has ? batteryElement(this, this._config, { battery, entities }) : html`
`} ${individualFieldLeftBottom ? individualLeftBottomElement(this, this.hass, this._config, { displayState: getIndividualDisplayState(individualFieldLeftBottom), diff --git a/src/type.ts b/src/type.ts index a20e37d..97d240e 100644 --- a/src/type.ts +++ b/src/type.ts @@ -137,3 +137,6 @@ export type GridObject = { }; }; }; + +export type OfflineStr = "unavailable" | "unknown"; +export const offlineStr = ["unavailable", "unknown"]; diff --git a/src/utils/displayNonFossilState.ts b/src/utils/displayNonFossilState.ts index 0a60658..f171c70 100644 --- a/src/utils/displayNonFossilState.ts +++ b/src/utils/displayNonFossilState.ts @@ -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); }; diff --git a/src/utils/displayValue.ts b/src/utils/displayValue.ts index ea8c61b..c511730 100644 --- a/src/utils/displayValue.ts +++ b/src/utils/displayValue.ts @@ -7,17 +7,20 @@ 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); const v = formatNumber(transformValue(isKW ? round(valueInNumber / 1000, decimals ?? 2) : round(valueInNumber, decimals ?? 0)), hass.locale); + return `${v}${unitWhiteSpace === false ? "" : " "}${unit || (isKW ? "kW" : "W")}`; };