Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting to disable default battery state value sources #401 #584

Merged
merged 2 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading