diff --git a/src/custom-elements/battery-state-entity.ts b/src/custom-elements/battery-state-entity.ts index 3f67899..6c71108 100644 --- a/src/custom-elements/battery-state-entity.ts +++ b/src/custom-elements/battery-state-entity.ts @@ -95,7 +95,7 @@ export class BatteryStateEntity extends LovelaceCard { } this.entityData = { - ...this.hass?.states[this.config.entity] + ...this.hass.states[this.config.entity] }; if (this.config.extend_entity_data !== false) { diff --git a/src/entity-fields/battery-level.ts b/src/entity-fields/battery-level.ts index a1f3db5..0b8ac6d 100644 --- a/src/entity-fields/battery-level.ts +++ b/src/entity-fields/battery-level.ts @@ -19,7 +19,7 @@ const formattedStatePattern = /(-?[0-9,.]+)\s?(.*)/; * @param hass HomeAssistant state object * @returns Battery level */ -export const getBatteryLevel = (config: IBatteryEntityConfig, hass: HomeAssistantExt | undefined, entityData: IMap | undefined): IBatteryState => { +export const getBatteryLevel = (config: IBatteryEntityConfig, hass: HomeAssistantExt, entityData: IMap | undefined): IBatteryState => { const UnknownLevel = hass?.localize("state.default.unknown") || "Unknown"; let state: string; let unit: string | undefined; diff --git a/src/entity-fields/charging-state.ts b/src/entity-fields/charging-state.ts index e95f194..4092e5b 100644 --- a/src/entity-fields/charging-state.ts +++ b/src/entity-fields/charging-state.ts @@ -8,12 +8,7 @@ import { log, safeGetArray } from "../utils"; * @param hass HomeAssistant state object * @returns Whether battery is in chargin mode */ - export const getChargingState = (config: IBatteryEntityConfig, state: string, hass?: HomeAssistant): boolean => { - - if (!hass) { - return false; - } - + export const getChargingState = (config: IBatteryEntityConfig, state: string, hass: HomeAssistant): boolean => { const chargingConfig = config.charging_state; if (!chargingConfig) { return getDefaultChargingState(config, hass); diff --git a/src/entity-fields/get-icon.ts b/src/entity-fields/get-icon.ts index e1501f0..190fe6e 100644 --- a/src/entity-fields/get-icon.ts +++ b/src/entity-fields/get-icon.ts @@ -10,7 +10,7 @@ import { RichStringProcessor } from "../rich-string-processor"; * @param hass HomeAssistant state object * @returns Mdi icon string */ -export const getIcon = (config: IBatteryEntityConfig, level: number | undefined, isCharging: boolean, hass: HomeAssistant | undefined): string => { +export const getIcon = (config: IBatteryEntityConfig, level: number | undefined, isCharging: boolean, hass: HomeAssistant): string => { if (isCharging && config.charging_state?.icon) { return config.charging_state.icon; } @@ -18,7 +18,7 @@ export const getIcon = (config: IBatteryEntityConfig, level: number | undefined, if (config.icon) { const attribPrefix = "attribute."; // check if we should return the icon/string from the attribute value - if (hass && config.icon.startsWith(attribPrefix)) { + if (config.icon.startsWith(attribPrefix)) { const attribName = config.icon.substr(attribPrefix.length); const val = hass.states[config.entity].attributes[attribName] as string | undefined; if (!val) { @@ -29,7 +29,7 @@ export const getIcon = (config: IBatteryEntityConfig, level: number | undefined, return val; } - const processor = new RichStringProcessor(hass, { ...hass?.states[config.entity] }); + const processor = new RichStringProcessor(hass, { ...hass.states[config.entity] }); return processor.process(config.icon); } diff --git a/src/entity-fields/get-name.ts b/src/entity-fields/get-name.ts index 422a6e4..026b829 100644 --- a/src/entity-fields/get-name.ts +++ b/src/entity-fields/get-name.ts @@ -9,7 +9,7 @@ import { RichStringProcessor } from "../rich-string-processor"; * @param hass HomeAssistant state object * @returns Battery name */ -export const getName = (config: IBatteryEntityConfig, hass: HomeAssistant | undefined, entityData: IMap): string => { +export const getName = (config: IBatteryEntityConfig, hass: HomeAssistant, entityData: IMap): string => { if (config.name) { const proc = new RichStringProcessor(hass, entityData); return proc.process(config.name); diff --git a/src/entity-fields/get-secondary-info.ts b/src/entity-fields/get-secondary-info.ts index addaea5..5497d3f 100644 --- a/src/entity-fields/get-secondary-info.ts +++ b/src/entity-fields/get-secondary-info.ts @@ -9,7 +9,7 @@ import { isNumber } from "../utils"; * @param entidyData Entity data * @returns Secondary info text */ -export const getSecondaryInfo = (config: IBatteryEntityConfig, hass: HomeAssistant | undefined, entityData: IMap | undefined): string => { +export const getSecondaryInfo = (config: IBatteryEntityConfig, hass: HomeAssistant, entityData: IMap | undefined): string => { if (config.secondary_info) { const processor = new RichStringProcessor(hass, entityData); diff --git a/src/rich-string-processor.ts b/src/rich-string-processor.ts index 3ab52ce..b3b7b4c 100644 --- a/src/rich-string-processor.ts +++ b/src/rich-string-processor.ts @@ -35,7 +35,7 @@ const validEntityDomains = [ */ export class RichStringProcessor { - constructor(private hass: HomeAssistant | undefined, private entityData: IMap | undefined) { + constructor(private hass: HomeAssistant, private entityData: IMap | undefined) { } /** @@ -87,7 +87,7 @@ const validEntityDomains = [ if (validEntityDomains.includes(chunks[0])) { data = { - ...this.hass?.states[chunks.splice(0, 2).join(".")] + ...this.hass.states[chunks.splice(0, 2).join(".")] }; } diff --git a/test/other/entity-fields/charging-state.test.ts b/test/other/entity-fields/charging-state.test.ts index 5098a04..97145bb 100644 --- a/test/other/entity-fields/charging-state.test.ts +++ b/test/other/entity-fields/charging-state.test.ts @@ -8,15 +8,6 @@ describe("Charging state", () => { const hassMock = new HomeAssistantMock(true); const isCharging = getChargingState({ entity: "any" }, "90", hassMock.hass); - expect(isCharging).toBe(false); - }) - - test("is false when there is no hass", () => { - const isCharging = getChargingState( - { entity: "sensor.my_entity", charging_state: { attribute: [ { name: "is_charging", value: "true" } ] } }, - "45", - undefined); - expect(isCharging).toBe(false); }) diff --git a/test/other/entity-fields/get-icon.test.ts b/test/other/entity-fields/get-icon.test.ts index ea670d8..99c9d09 100644 --- a/test/other/entity-fields/get-icon.test.ts +++ b/test/other/entity-fields/get-icon.test.ts @@ -3,7 +3,7 @@ import { HomeAssistantMock } from "../../helpers"; describe("Get icon", () => { test("charging and charging icon set in config", () => { - let icon = getIcon({ entity: "", charging_state: { icon: "mdi:custom" } }, 20, true, undefined); + let icon = getIcon({ entity: "", charging_state: { icon: "mdi:custom" } }, 20, true, new HomeAssistantMock(true).hass); expect(icon).toBe("mdi:custom"); }); @@ -12,7 +12,7 @@ describe("Get icon", () => { [200], [NaN], ])("returns unknown state icon when invalid state passed", (invalidEntityState: number) => { - let icon = getIcon({ entity: "" }, invalidEntityState, false, undefined); + let icon = getIcon({ entity: "" }, invalidEntityState, false, new HomeAssistantMock(true).hass); expect(icon).toBe("mdi:battery-unknown"); }); @@ -38,12 +38,12 @@ describe("Get icon", () => { [95, true, "mdi:battery-charging-100"], [100, true, "mdi:battery-charging-100"], ])("returns correct state icon", (batteryLevel: number, isCharging: boolean, expectedIcon: string) => { - let icon = getIcon({ entity: "" }, batteryLevel, isCharging, undefined); + let icon = getIcon({ entity: "" }, batteryLevel, isCharging, new HomeAssistantMock(true).hass); expect(icon).toBe(expectedIcon); }); test("returns custom icon from config", () => { - let icon = getIcon({ entity: "", icon: "mdi:custom" }, 20, false, undefined); + let icon = getIcon({ entity: "", icon: "mdi:custom" }, 20, false, new HomeAssistantMock(true).hass); expect(icon).toBe("mdi:custom"); }); diff --git a/test/other/entity-fields/get-name.test.ts b/test/other/entity-fields/get-name.test.ts index bf8a786..34bcace 100644 --- a/test/other/entity-fields/get-name.test.ts +++ b/test/other/entity-fields/get-name.test.ts @@ -3,14 +3,13 @@ import { HomeAssistantMock } from "../../helpers"; describe("Get name", () => { test("returns name from the config", () => { - const hassMock = new HomeAssistantMock(true); - let name = getName({ entity: "test", name: "Entity name" }, hassMock.hass, {}) + let name = getName({ entity: "test", name: "Entity name" }, new HomeAssistantMock(true).hass, {}) expect(name).toBe("Entity name"); }); test("returns entity id when friendly_name is missing", () => { - let name = getName({ entity: "sensor.my_entity_id" }, undefined, { attributes: {} }) + let name = getName({ entity: "sensor.my_entity_id" }, new HomeAssistantMock(true).hass, { attributes: {} }) expect(name).toBe("sensor.my_entity_id"); });