Skip to content

Commit

Permalink
Merge pull request #584 from maxwroc/NonBatteryEntities
Browse files Browse the repository at this point in the history
Setting to disable default battery state value sources #401
  • Loading branch information
maxwroc authored Oct 22, 2023
2 parents e01c264 + 798df7b commit f83aa09
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` | v3.0.0 | Disables default battery state sources e.g. "battery_level" attribute

### Keyword string (KString)

Expand Down
2 changes: 1 addition & 1 deletion src/battery-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]*)/;

Expand Down
4 changes: 2 additions & 2 deletions src/entity-fields/battery-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
];

Expand Down
5 changes: 5 additions & 0 deletions src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions test/other/entity-fields/battery-level.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f83aa09

Please sign in to comment.