Skip to content

Commit

Permalink
Fix error when attibute is not found #68
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwroc committed May 28, 2020
1 parent 094f7ab commit 2ea31eb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "battery-state-card",
"version": "1.3.5",
"version": "1.3.6",
"description": "Battery State card for Home Assistant",
"main": "dist/battery-state-card.js",
"repository": {
Expand Down
10 changes: 8 additions & 2 deletions src/battery-vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,15 @@ class BatteryViewModel {
* @param entityData Entity state data
*/
private getLevel(entityData: HassEntity): string {
const UnknownLevel = "Unknown";
let level: string;

if (this.config.attribute) {
level = entityData.attributes[this.config.attribute]
level = entityData.attributes[this.config.attribute];
if (level == undefined) {
log(`Attribute "${this.config.attribute}" doesn't exist on "${this.config.entity}" entity`);
level = UnknownLevel;
}
}
else {
const candidates: string[] = [
Expand All @@ -211,7 +217,7 @@ class BatteryViewModel {
entityData.state
];

level = candidates.find(n => n !== null && n !== undefined)?.toString() || "Unknown";
level = candidates.find(n => n !== null && n !== undefined)?.toString() || UnknownLevel;
}

// check if we should convert value eg. for binary sensors
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HomeAssistant } from "./ha-types";

console.info(
"%c BATTERY-STATE-CARD %c 1.3.5",
"%c BATTERY-STATE-CARD %c 1.3.6",
"color: white; background: forestgreen; font-weight: 700;",
"color: forestgreen; background: white; font-weight: 700;",
);
Expand Down

0 comments on commit 2ea31eb

Please sign in to comment.