Skip to content

Commit

Permalink
Made hass mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwroc committed Feb 17, 2024
1 parent 144b5b3 commit ba61aa2
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/custom-elements/battery-state-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class BatteryStateEntity extends LovelaceCard<IBatteryEntityConfig> {
}

this.entityData = <any>{
...this.hass?.states[this.config.entity]
...this.hass.states[this.config.entity]
};

if (this.config.extend_entity_data !== false) {
Expand Down
2 changes: 1 addition & 1 deletion src/entity-fields/battery-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> | undefined): IBatteryState => {
export const getBatteryLevel = (config: IBatteryEntityConfig, hass: HomeAssistantExt, entityData: IMap<any> | undefined): IBatteryState => {
const UnknownLevel = hass?.localize("state.default.unknown") || "Unknown";
let state: string;
let unit: string | undefined;
Expand Down
7 changes: 1 addition & 6 deletions src/entity-fields/charging-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/entity-fields/get-icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ 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;
}

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) {
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/entity-fields/get-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>): string => {
export const getName = (config: IBatteryEntityConfig, hass: HomeAssistant, entityData: IMap<any>): string => {
if (config.name) {
const proc = new RichStringProcessor(hass, entityData);
return proc.process(config.name);
Expand Down
2 changes: 1 addition & 1 deletion src/entity-fields/get-secondary-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> | undefined): string => {
export const getSecondaryInfo = (config: IBatteryEntityConfig, hass: HomeAssistant, entityData: IMap<any> | undefined): string => {
if (config.secondary_info) {
const processor = new RichStringProcessor(hass, entityData);

Expand Down
4 changes: 2 additions & 2 deletions src/rich-string-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const validEntityDomains = [
*/
export class RichStringProcessor {

constructor(private hass: HomeAssistant | undefined, private entityData: IMap<any> | undefined) {
constructor(private hass: HomeAssistant, private entityData: IMap<any> | undefined) {
}

/**
Expand Down Expand Up @@ -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(".")]
};
}

Expand Down
9 changes: 0 additions & 9 deletions test/other/entity-fields/charging-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})

Expand Down
8 changes: 4 additions & 4 deletions test/other/entity-fields/get-icon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

Expand All @@ -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");
});

Expand All @@ -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");
});

Expand Down
5 changes: 2 additions & 3 deletions test/other/entity-fields/get-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down

0 comments on commit ba61aa2

Please sign in to comment.