diff --git a/package.json b/package.json index ac785380..3c9fdbd5 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" diff --git a/src/entity-fields/battery-level.ts b/src/entity-fields/battery-level.ts index 72f2fd68..c17e660d 100644 --- a/src/entity-fields/battery-level.ts +++ b/src/entity-fields/battery-level.ts @@ -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; diff --git a/test/card/sorting.test.ts b/test/card/sorting.test.ts new file mode 100644 index 00000000..124fbbcf --- /dev/null +++ b/test/card/sorting.test.ts @@ -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, entityStates: string[], expectedOrder: string) => { + + const hass = new HomeAssistantMock(); + 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); + }); +}); \ No newline at end of file diff --git a/test/helpers.ts b/test/helpers.ts index d8978968..593f95ff 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -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(".card-content > * > battery-state-entity")[index]; if (!entity) { diff --git a/test/other/entity-fields/battery-level.test.ts b/test/other/entity-fields/battery-level.test.ts index d03198f1..364bdf0a 100644 --- a/test/other/entity-fields/battery-level.test.ts +++ b/test/other/entity-fields/battery-level.test.ts @@ -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, expectedState: string) => { + const hassMock = new HomeAssistantMock(true); + const entity = hassMock.addEntity("Mocked entity", 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); + }) }); \ No newline at end of file