From 1d6d461b6f1529ad738c5e455fcc93d7422e9998 Mon Sep 17 00:00:00 2001 From: Max Chodorowski Date: Sun, 22 Oct 2023 19:48:23 +0100 Subject: [PATCH] Disable default battery state value sources #401 --- README.md | 1 + src/battery-provider.ts | 2 +- src/entity-fields/battery-level.ts | 4 ++-- src/typings.d.ts | 5 +++++ test/other/entity-fields/battery-level.test.ts | 10 ++++++++++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9d509168..b401169f 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ These options can be specified both per-entity and at the top level (affecting a | round | number | | v2.1.0 | Rounds the value to number of fractional digits | unit | string | `"%"` | v2.1.0 | Override for unit displayed next to the state/level value ([example](#other-use-cases)) | value_override | [KString](#keyword-string-kstring) | | v3.0.0 | Allows to override the battery level value. Note: when used the `multiplier`, `round`, `state_map` setting is ignored +| non_battery_entity | boolean | `false` | Disables default battery state sources e.g. "battery_level" attribute ### Keyword string (KString) diff --git a/src/battery-provider.ts b/src/battery-provider.ts index 2ba180a4..9fc7636d 100644 --- a/src/battery-provider.ts +++ b/src/battery-provider.ts @@ -5,7 +5,7 @@ import { BatteryStateEntity } from "./custom-elements/battery-state-entity"; /** * Properties which should be copied over to individual entities from the card */ -const entititesGlobalProps: (keyof IBatteryEntityConfig)[] = [ "tap_action", "state_map", "charging_state", "secondary_info", "colors", "bulk_rename", "icon", "round", "unit", "value_override" ]; +const entititesGlobalProps: (keyof IBatteryEntityConfig)[] = [ "tap_action", "state_map", "charging_state", "secondary_info", "colors", "bulk_rename", "icon", "round", "unit", "value_override", "non_battery_entity" ]; const regExpPattern = /\/([^/]+)\/([igmsuy]*)/; diff --git a/src/entity-fields/battery-level.ts b/src/entity-fields/battery-level.ts index 7ac595be..d370e51a 100644 --- a/src/entity-fields/battery-level.ts +++ b/src/entity-fields/battery-level.ts @@ -37,8 +37,8 @@ import { isNumber, log } from "../utils"; } else { const candidates: string[] = [ - entityData.attributes.battery_level, - entityData.attributes.battery, + config.non_battery_entity ? null: entityData.attributes.battery_level, + config.non_battery_entity ? null: entityData.attributes.battery, entityData.state ]; diff --git a/src/typings.d.ts b/src/typings.d.ts index fe3b2cd9..c2787dab 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -227,6 +227,11 @@ interface IBatteryEntityConfig { * Override for unit shown next to the value */ unit?: string; + + /** + * Whether the entity is not a battery entity + */ + non_battery_entity?: boolean; } interface IBatteryCardConfig { diff --git a/test/other/entity-fields/battery-level.test.ts b/test/other/entity-fields/battery-level.test.ts index 70027557..44203a98 100644 --- a/test/other/entity-fields/battery-level.test.ts +++ b/test/other/entity-fields/battery-level.test.ts @@ -85,6 +85,16 @@ describe("Battery level", () => { expect(level).toBe("45"); }); + test("is taken from dafault locations - non battery entity", () => { + + const hassMock = new HomeAssistantMock(true); + hassMock.addEntity("Mocked entity", "OK", { battery_level: "45%" }); + + const level = getBatteryLevel({ entity: "mocked_entity", non_battery_entity: true }, hassMock.hass); + + expect(level).toBe("OK"); + }); + test("is taken from dafault locations - state", () => { const hassMock = new HomeAssistantMock(true);