Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non fossil functionality #12

Merged
merged 2 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "power-distribution-card",
"version": "2.6.2",
"description": "A power distribution card for Home Assistant",
"name": "power-flow-card-plus",
"version": "0.0.8",
"description": "A power flow card for Home Assistant",
"keywords": [
"home-assistant",
"homeassistant",
Expand All @@ -12,14 +12,14 @@
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/ulic75/power-distribution-card.git"
"url": "git+https://github.com/flixlix/power-flow-card-plus.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/ulic75/power-distribution-card/issues"
"url": "https://github.com/flixlix/power-flow-card-plus/issues"
},
"homepage": "https://github.com/ulic75/power-distribution-card#readme",
"homepage": "https://github.com/flixlix/power-flow-card-plus#readme",
"scripts": {
"build": "rollup -c",
"lint": "eslint src/*",
Expand Down
4 changes: 2 additions & 2 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { version } from "../package.json";

// Log Version
console.groupCollapsed(
`%c⚡ Power Flow Card v${version} is installed`,
`%c⚡ Power Flow Card Plus v${version} is installed`,
"color: #488fc2; font-weight: bold"
);
console.log("Readme:", "https://github.com/flixlix/power-flow-card-plus");
Expand All @@ -16,4 +16,4 @@ export const logError = debounce((error: string) => {
"color: #488fc2; font-weight: bold",
"color: #b33a3a; font-weight: normal"
);
}, 500);
}, 60000);
9 changes: 9 additions & 0 deletions src/power-flow-card-plus-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ export interface PowerFlowCardConfig extends LovelaceCardConfig {
grid: string | ComboEntity;
solar?: string;
home?: string;
fossil_fuel_percentage?: {
entity?: string;
name?: string;
icon?: string;
color?: string;
display_zero?: boolean;
state_type?: "percentage" | "power";
color_icon?: boolean;
};
individual2?: {
entity?: string;
name?: string;
Expand Down
157 changes: 146 additions & 11 deletions src/power-flow-card-plus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,43 @@ export class PowerFlowCard extends LitElement {
return value * 1000;
};

private displayNonFossilState = (entity: string | undefined): string => {
if (!entity || !this.entityAvailable(entity)) {
this.unavailableOrMisconfiguredError(entity);
return "NaN";
}
const unitOfMeasurement: "W" | "%" =
this._config!.entities.fossil_fuel_percentage?.state_type === "percentage"
? "%"
: "W" || "W";
const nonFossilFuelDecimal: number = 1 - this.getEntityState(entity) / 100;
let gridConsumption: number;
if (typeof this._config!.entities.grid === "string") {
gridConsumption =
this.getEntityStateWatts(this._config!.entities!.grid) > 0
? this.getEntityStateWatts(this._config!.entities!.grid)
: 0;
} else {
gridConsumption =
this.getEntityStateWatts(this._config!.entities!.grid!.consumption) > 0
? this.getEntityStateWatts(this._config!.entities!.grid!.consumption)
: 0;
}
/* based on choice, change output from watts to % */
let result: string;
if (unitOfMeasurement === "W") {
const nonFossilFuelWatts = gridConsumption * nonFossilFuelDecimal;
result = this.displayValue(nonFossilFuelWatts);
} else {
const nonFossilFuelPercentage: number = 100 - this.getEntityState(entity);
result = nonFossilFuelPercentage
.toFixed(0)
.toString()
.concat(unitOfMeasurement);
}
return result;
};

private displayValue = (value: number | null) => {
if (value === null) return "0";
const isKW = value >= this._config!.watt_threshold;
Expand Down Expand Up @@ -395,12 +432,100 @@ export class PowerFlowCard extends LitElement {
this.previousDur[flowName] = newDur[flowName];
});

const hasNonFossilFuelUsage =
gridConsumption * 1 -
this.getEntityState(entities.fossil_fuel_percentage?.entity) / 100 >
0 &&
entities.fossil_fuel_percentage?.entity !== undefined &&
this.entityAvailable(entities.fossil_fuel_percentage?.entity);

const hasFossilFuelPercentage =
(entities.fossil_fuel_percentage?.entity !== undefined &&
entities.fossil_fuel_percentage?.display_zero === true) ||
hasNonFossilFuelUsage;

this.style.setProperty(
"--non-fossil-color",
this._config.entities.fossil_fuel_percentage?.color ||
"var(--energy-non-fossil-color)"
);
this.style.setProperty(
"--icon-non-fossil-color",
this._config.entities.fossil_fuel_percentage?.color_icon === true
? "var(--non-fossil-color)"
: "var(--primary-text-color)" || "var(--non-fossil-color)"
);

return html`
<ha-card .header=${this._config.title}>
<div class="card-content">
${hasSolarProduction || hasIndividual2 || hasIndividual1
? html`<div class="row">
<div class="spacer"></div>
${!hasFossilFuelPercentage
? html`<div class="spacer"></div>`
: html`<div class="circle-container low-carbon">
<span class="label"
>${!entities.fossil_fuel_percentage?.name
? this.hass.localize(
"ui.panel.lovelace.cards.energy.energy_distribution.low_carbon"
)
: entities.fossil_fuel_percentage?.name}</span
>
<div
class="circle"
@click=${(e: { stopPropagation: () => void }) => {
e.stopPropagation();
this.openDetails(
entities.fossil_fuel_percentage?.entity
);
}}
@keyDown=${(e: {
key: string;
stopPropagation: () => void;
}) => {
if (e.key === "Enter") {
e.stopPropagation();
this.openDetails(
entities.fossil_fuel_percentage?.entity
);
}
}}
>
<ha-icon
.icon=${!entities.fossil_fuel_percentage?.icon
? "mdi:leaf"
: entities.fossil_fuel_percentage?.icon}
class="low-carbon"
></ha-icon>
<span class="low-carbon"
>${this.displayNonFossilState(
entities!.fossil_fuel_percentage!.entity
)}</span
>
</div>
<svg width="80" height="30">
<path
d="M40 -10 v40"
class="low-carbon"
id="low-carbon"
/>
${hasNonFossilFuelUsage
? svg`<circle
r="2.4"
class="low-carbon"
vector-effect="non-scaling-stroke"
>
<animateMotion
dur="1.66s"
repeatCount="indefinite"
calcMode="linear"
>
<mpath xlink:href="#low-carbon" />
</animateMotion>
</circle>`
: ""}
</svg>
</div>`}
${hasSolarProduction
? html`<div class="circle-container solar">
<span class="label"
Expand Down Expand Up @@ -462,7 +587,7 @@ export class PowerFlowCard extends LitElement {
<path d="M40 -10 v50" id="individual2" />
${individual2Usage
? svg`<circle
r="1"
r="2.4"
class="individual2"
vector-effect="non-scaling-stroke"
>
Expand Down Expand Up @@ -508,7 +633,7 @@ export class PowerFlowCard extends LitElement {
<path d="M40 -10 v40" id="individual1" />
${individual1Usage
? svg`<circle
r="1"
r="2.4"
class="individual1"
vector-effect="non-scaling-stroke"
>
Expand Down Expand Up @@ -832,15 +957,9 @@ export class PowerFlowCard extends LitElement {
<path d="M40 40 v-40" id="individual1" />
${individual1Usage
? svg`<circle
r="1"
r="2.4"
class="individual1"
vector-effect="non-scaling-stroke"
onClick=${(e) => {
e.stopPropagation();
this.openDetails(
entities.individual1?.entity
);
}}
vector-effect="non-scaling-stroke"
>
<animateMotion
dur="1.66s"
Expand Down Expand Up @@ -1164,6 +1283,8 @@ export class PowerFlowCard extends LitElement {
--individualone-color: #d0cc5b;
--individualtwo-color: #964cb5;
--clickable-cursor: pointer;
--non-fossil-color: var(--energy-non-fossil-color, #0f9d58);
--icon-non-fossil-color: var(--non-fossil-color, #0f9d58);
}
.card-content {
position: relative;
Expand Down Expand Up @@ -1297,6 +1418,20 @@ export class PowerFlowCard extends LitElement {
.individual1 .circle {
border-color: var(--individualone-color);
}
.low-carbon path {
stroke: var(--non-fossil-color);
}
.low-carbon .circle {
border-color: var(--non-fossil-color);
}
.low-carbon ha-icon {
color: var(--icon-non-fossil-color);
}
circle.low-carbon {
stroke-width: 4;
fill: var(--non-fossil-color);
stroke: var(--non-fossil-color);
}
.solar {
color: var(--primary-text-color);
}
Expand Down