Skip to content

Commit

Permalink
feat: log unavailable/misconfigured entities to browser console (ulic…
Browse files Browse the repository at this point in the history
  • Loading branch information
ulic75 authored May 21, 2022
1 parent 77bd3ab commit f32576a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
26 changes: 22 additions & 4 deletions src/power-flow-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ import { css, html, LitElement, svg, TemplateResult } from "lit";
import { customElement, property, query, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { PowerFlowCardConfig } from "./power-flow-card-config.js";
import { coerceNumber, coerceStringArray, round } from "./utils.js";
import {
coerceNumber,
coerceStringArray,
round,
isNumberValue,
} from "./utils.js";
import { EntityType } from "./type.js";
import "./logging.js";
import { logError } from "./logging.js";

const CIRCLE_CIRCUMFERENCE = 238.76104;
const KW_DECIMALS = 1;
Expand Down Expand Up @@ -55,6 +60,13 @@ export class PowerFlowCard extends LitElement {
public getCardSize(): Promise<number> | number {
return 3;
}
private unavailableOrMisconfiguredError = (entityId: string | undefined) =>
logError(
`entity "${entityId ?? "Unknown"}" is not available or misconfigured`
);

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

private entityInverted = (entityType: EntityType) =>
this._config!.inverted_entities.includes(entityType);
Expand All @@ -68,12 +80,18 @@ export class PowerFlowCard extends LitElement {
};

private getEntityState = (entity: string | undefined): number => {
if (!entity) return 0;
if (!entity || !this.entityAvailable(entity)) {
this.unavailableOrMisconfiguredError(entity);
return 0;
}
return coerceNumber(this.hass.states[entity].state);
};

private getEntityStateWatts = (entity: string | undefined): number => {
if (!entity) return 0;
if (!entity || !this.entityAvailable(entity)) {
this.unavailableOrMisconfiguredError(entity);
return 0;
}
const stateObj = this.hass.states[entity];
const value = coerceNumber(stateObj.state);
if (stateObj.attributes.unit_of_measurement === "W") return value;
Expand Down
18 changes: 9 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
/* eslint-disable no-redeclare */
export function isNumberValue(value: any): boolean {
// parseFloat(value) handles most of the cases we're interested in (it treats null, empty string,
// and other non-number values as NaN, where Number just uses 0) but it considers the string
// '123hello' to be a valid number. Therefore we also check if Number(value) is NaN.
// eslint-disable-next-line no-restricted-globals
return !isNaN(parseFloat(value as any)) && !isNaN(Number(value));
}

export function coerceNumber(value: any): number;
export function coerceNumber<D>(value: any, fallback: D): number | D;
export function coerceNumber(value: any, fallbackValue = 0) {
return _isNumberValue(value) ? Number(value) : fallbackValue;
return isNumberValue(value) ? Number(value) : fallbackValue;
}

export function coerceStringArray(
Expand Down Expand Up @@ -30,11 +38,3 @@ export const round = (value: number, decimalPlaces: number): number =>
Number(
`${Math.round(Number(`${value}e${decimalPlaces}`))}e-${decimalPlaces}`
);

export function _isNumberValue(value: any): boolean {
// parseFloat(value) handles most of the cases we're interested in (it treats null, empty string,
// and other non-number values as NaN, where Number just uses 0) but it considers the string
// '123hello' to be a valid number. Therefore we also check if Number(value) is NaN.
// eslint-disable-next-line no-restricted-globals
return !isNaN(parseFloat(value as any)) && !isNaN(Number(value));
}

0 comments on commit f32576a

Please sign in to comment.