Skip to content

Commit

Permalink
Merge pull request #642 from maxwroc/FixSorting
Browse files Browse the repository at this point in the history
Fixed sorting by state option (numeric state values)
  • Loading branch information
maxwroc authored Jan 9, 2024
2 parents f52a2c1 + 28fd97a commit 9475255
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "battery-state-card",
"version": "3.1.2",
"version": "3.1.3",
"description": "Battery State card for Home Assistant",
"main": "dist/battery-state-card.js",
"author": "Max Chodorowski",
Expand All @@ -21,7 +21,7 @@
"release": "rollup --environment RELEASE -c",
"watch": "rollup -c --watch",
"test": "jest",
"test+integration": "jest --testPathPattern=test/entity",
"test+integration": "jest --testPathPattern=\"test/(entity|card)\"",
"test+coverage": "jest --coverage",
"test+coverage+unit": "jest --coverage --testPathPattern=test/other",
"test+debug": "SET DEBUG_MODE=1&&jest"
Expand Down
6 changes: 3 additions & 3 deletions src/entity-fields/battery-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ export const getBatteryLevel = (config: IBatteryEntityConfig, hass?: HomeAssista
}
}
else {
const candidates: string[] = [
const candidates: (string | number | undefined)[] = [
config.non_battery_entity ? null: entityData.attributes.battery_level,
config.non_battery_entity ? null: entityData.attributes.battery,
entityData.state
];

state = candidates.find(val => isNumber(val)) ||
state = candidates.find(val => isNumber(val))?.toString() ||
candidates.find(val => val !== null && val !== undefined)?.toString() ||
UnknownLevel
UnknownLevel;
}

let displayValue: string | undefined;
Expand Down
33 changes: 33 additions & 0 deletions test/card/sorting.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { BatteryStateCard } from "../../src/custom-elements/battery-state-card";
import { CardElements, HomeAssistantMock } from "../helpers";


describe("Sorting", () => {
test.each([
["state", ["50", "90", "40"], "40 %, 50 %, 90 %"],
["state", ["40.5", "40.9", "40", "40.4"], "40 %, 40,4 %, 40,5 %, 40,9 %"],
])("Items correctly sorted", async (sort: ISimplifiedArray<ISortOption>, entityStates: string[], expectedOrder: string) => {

const hass = new HomeAssistantMock<BatteryStateCard>();
const entities = entityStates.map((state, i) => {
const batt = hass.addEntity(`Batt ${i + 1}`, state);
return batt.entity_id;
});

hass.mockFunc("formatEntityState", (entityData: any) => `${entityData.state.replace(".", ",")} %`);

const cardElem = hass.addCard("battery-state-card", {
title: "Header",
entities,
sort
});

// waiting for card to be updated/rendered
await cardElem.cardUpdated;

const card = new CardElements(cardElem);

const result = card.items.map(e => e.stateText).join(", ");
expect(result).toBe(expectedOrder);
});
});
9 changes: 9 additions & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ export class CardElements {
return this.card.shadowRoot!.querySelectorAll(".card-content > * > battery-state-entity").length;
}

get items(): EntityElements[] {
const result: EntityElements[] = [];
for (let index = 0; index < this.itemsCount; index++) {
result.push(this.item(index));
}

return result;
}

item(index: number) {
const entity = this.card.shadowRoot!.querySelectorAll<BatteryStateEntity>(".card-content > * > battery-state-entity")[index];
if (!entity) {
Expand Down
17 changes: 17 additions & 0 deletions test/other/entity-fields/battery-level.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,21 @@ describe("Battery level", () => {

expect(unit).toBe(expectedUnit);
})

test.each([
["45", {}, "45"],
[46, {}, "46"],
["ok", { battery_level: "47" }, "47"],
["ok", { battery_level: 48 }, "48"],
["ok", { battery: "49" }, "49"],
["ok", { battery: 50 }, "50"],
])("state value coming from default places", (entityState: string | number, entityAttributes: IMap<string | number>, expectedState: string) => {
const hassMock = new HomeAssistantMock(true);
const entity = hassMock.addEntity("Mocked entity", <any>entityState, entityAttributes);

const { state } = getBatteryLevel({ entity: entity.entity_id, default_state_formatting: false, unit: undefined}, hassMock.hass);

expect(typeof(state)).toBe("string");
expect(state).toBe(expectedState);
})
});

0 comments on commit 9475255

Please sign in to comment.