Skip to content

Commit

Permalink
Merge pull request #190 from flixlix/feat-add-invert-state-option
Browse files Browse the repository at this point in the history
Feat add invert state option
  • Loading branch information
flixlix authored May 22, 2023
2 parents 9745255 + 892c91c commit b13952c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/power-flow-card-plus-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ComboEntity, IndividualDeviceType, SecondaryInfoType, baseConfigEntity,
interface mainConfigOptions {
dashboard_link?: string;
dashboard_link_label?: string;
inverted_entities: string | string[];
kw_decimals: number;
min_flow_rate: number;
max_flow_rate: number;
Expand Down
10 changes: 4 additions & 6 deletions src/power-flow-card-plus.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* eslint-disable wc/guard-super-call */
/* eslint-disable import/extensions */
/* eslint-disable no-nested-ternary */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket";
import { formatNumber, HomeAssistant, LovelaceCardEditor } from "custom-card-helpers";
import { html, LitElement, PropertyValues, svg, TemplateResult } from "lit";
import { customElement, property, query, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { PowerFlowCardPlusConfig } from "./power-flow-card-plus-config";
import { coerceNumber, coerceStringArray, round, isNumberValue } from "./utils/utils";
import { coerceNumber, round, isNumberValue } from "./utils/utils";
import { EntityType } from "./type";
import { logError } from "./logging";
import { registerCustomCard } from "./utils/register-custom-card";
Expand Down Expand Up @@ -48,7 +47,6 @@ export class PowerFlowCardPlus extends LitElement {
}
this._config = {
...config,
inverted_entities: coerceStringArray(config.inverted_entities, ","),
kw_decimals: coerceNumber(config.kw_decimals, defaultValues.kilowattDecimals),
min_flow_rate: coerceNumber(config.min_flow_rate, defaultValues.minFlowRate),
max_flow_rate: coerceNumber(config.max_flow_rate, defaultValues.maxFlowRate),
Expand Down Expand Up @@ -89,7 +87,7 @@ export class PowerFlowCardPlus extends LitElement {

private entityAvailable = (entityId: string): boolean => isNumberValue(this.hass.states[entityId]?.state);

private entityInverted = (entityType: EntityType) => this._config!.inverted_entities.includes(entityType);
private entityInverted = (entityType: EntityType) => !!this._config.entities[entityType]?.invert_state;

private previousDur: { [name: string]: number } = {};

Expand Down Expand Up @@ -157,10 +155,10 @@ export class PowerFlowCardPlus extends LitElement {
const unitOfMeasurement: "W" | "%" = this._config!.entities.fossil_fuel_percentage?.state_type === "percentage" ? "%" : "W" || "W";
const nonFossilFuelDecimal: number = 1 - this.getEntityState(entityFossil) / 100;
let gridConsumption: number;
if (typeof this._config!.entities.grid!.entity === "string") {
if (typeof this._config.entities.grid?.entity === "string") {
gridConsumption = totalFromGrid;
} else {
gridConsumption = this.getEntityStateWatts(this._config!.entities!.grid!.entity!.consumption) || 0;
gridConsumption = this.getEntityStateWatts(this._config.entities.grid?.entity.consumption) || 0;
}

/* based on choice, change output from watts to % */
Expand Down
1 change: 1 addition & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface baseConfigEntity {
unit_of_measurement?: string;
use_metadata?: boolean;
secondary_info?: SecondaryInfoType;
invert_state?: boolean;
}

export type gridPowerOutage = {
Expand Down
5 changes: 0 additions & 5 deletions src/ui-editor/schema/_schema-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,6 @@ export const advancedOptionsSchema = [
},
],
},
{
name: "inverted_entities",
label: "Inverted Entities",
selector: { template: {} },
},
],
},
];
5 changes: 5 additions & 0 deletions src/ui-editor/schema/battery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const mainSchema = {
},
},
},
{
name: "invert_state",
label: "Invert State",
selector: { boolean: {} },
},
],
};

Expand Down
16 changes: 14 additions & 2 deletions src/ui-editor/schema/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ import {
customColorsSchema,
} from "./_schema-base";

const mainSchema = {
...getBaseMainConfigSchema("grid"),
schema: [
...getBaseMainConfigSchema("grid").schema,
{
name: "invert_state",
label: "Invert State",
selector: { boolean: {} },
},
],
};

const powerOutageGridSchema = [
{
name: "entity",
Expand All @@ -17,7 +29,7 @@ const powerOutageGridSchema = [
column_min_width: "200px",
schema: [
{ name: "label_alert", label: "Outage Label", selector: { text: {} } },
{ name: "icon_alert", label: "Outage Icon", selector: { icon: {} } },
{ name: "icon_alert", label: "Outage Icon", selector: { icon: {} } },
{ name: "state_alert", label: "Outage State", selector: { text: {} } },
],
},
Expand All @@ -26,7 +38,7 @@ const powerOutageGridSchema = [
export const gridSchema = [
getEntityCombinedSelectionSchema(),
getEntitySeparatedSelectionSchema(),
getBaseMainConfigSchema("grid"),
mainSchema,
customColorsSchema,
{
title: "Secondary Info",
Expand Down

0 comments on commit b13952c

Please sign in to comment.