Skip to content

Commit

Permalink
Merge pull request #635 from maxwroc/FixUnitWithStringState
Browse files Browse the repository at this point in the history
Don't show unit when state is a string
  • Loading branch information
maxwroc authored Jan 8, 2024
2 parents 67b09e6 + f76f8d9 commit 4834601
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/entity-fields/battery-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const getBatteryLevel = (config: IBatteryEntityConfig, hass?: HomeAssista
return {
state: processedValue,
level: isNumber(processedValue) ? Number(processedValue) : undefined,
unit: getUnit(processedValue, undefined, config, hass),
unit: getUnit(processedValue, undefined, undefined, config, hass),
}
}

Expand Down Expand Up @@ -101,18 +101,18 @@ export const getBatteryLevel = (config: IBatteryEntityConfig, hass?: HomeAssista
const formattedState = hass.formatEntityState(entityData);

// assuming it is a number followed by unit
[displayValue, unit] = formattedState.split(" ", 2);
[state, unit] = formattedState.split(" ", 2);
unit = unit;
}

return {
state: displayValue || state,
level: isNumber(state) ? Number(state) : undefined,
unit: getUnit(state, unit, config, hass),
unit: getUnit(state, displayValue, unit, config, hass),
};
}

const getUnit = (state: string, unit: string | undefined, config: IBatteryEntityConfig, hass?: HomeAssistantExt): string | undefined => {
const getUnit = (state: string, displayValue: string | undefined, unit: string | undefined, config: IBatteryEntityConfig, hass?: HomeAssistantExt): string | undefined => {
if (config.unit) {
// config unit override
unit = config.unit
Expand All @@ -122,7 +122,7 @@ const getUnit = (state: string, unit: string | undefined, config: IBatteryEntity
unit = unit || hass?.states[config.entity]?.attributes["unit_of_measurement"] || "%"
}

if (!isNumber(state)) {
if (!isNumber(state) || (displayValue && !isNumber(displayValue))) {
// for non numeric states unit should not be rendered
unit = undefined;
}
Expand Down
14 changes: 7 additions & 7 deletions test/other/entity-fields/battery-level.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ describe("Battery level", () => {
["ok", "100", 100, "%", undefined],
["empty", "0", 0, "%", undefined],
["20", "20", 20, "%", undefined],
["charge", "Empty", 0, "%", "Empty"],
["charge", "StateFromOtherEntity", 0, "%", "{sensor.other_entity.state}"],
["charge", "Empty", 0, undefined, "Empty"],
["charge", "StateFromOtherEntity", 0, undefined, "{sensor.other_entity.state}"],
])
("state map applied", (entityState: string, expectedState: string, expectedLevel: number | undefined, expectedUnit: string | undefined, display?: string) => {

Expand All @@ -189,17 +189,17 @@ describe("Battery level", () => {
});

test.each([
[undefined, "45", "dbm", { state: "[45]", level: 45, unit: "[dbm]" }], // test default when the setting is not set in the config
[true, "45", "dbm", { state: "[45]", level: 45, unit: "[dbm]" }], // test when the setting is explicitly true
[undefined, "45", "dbm", { state: "45", level: 45, unit: "[dbm]" }], // test default when the setting is not set in the config
[true, "45", "dbm", { state: "45", level: 45, unit: "[dbm]" }], // test when the setting is explicitly true
[false, "45", "dbm", { state: "45", level: 45, unit: "%" }], // test when the setting is turned off
[true, "45", "dbm", { state: "56", level: 56, unit: "%" }, [ { from: "45", to: "56" } ]], // test when the state was changed by state_map
[true, "45", "dbm", { state: "33", level: 45, unit: "%" }, [ { from: "45", to: "45", display: "33" } ]], // test when the display value was changed by state_map
[true, "45", "dbm", { state: "Low", level: 45, unit: undefined }, [ { from: "45", to: "45", display: "Low" } ]], // test when the display value was changed by state_map
])
("default HA formatting ", (defaultStateFormatting: boolean | undefined, entityState: string, unitOfMeasurement: string, expected: { state: string, level: number, unit?: string }, stateMap: IConvert[] | undefined = undefined) => {
("default HA formatting", (defaultStateFormatting: boolean | undefined, entityState: string, unitOfMeasurement: string, expected: { state: string, level: number, unit?: string }, stateMap: IConvert[] | undefined = undefined) => {

const hassMock = new HomeAssistantMock(true);
hassMock.addEntity("Mocked entity", entityState);
hassMock.mockFunc("formatEntityState", (entityData: any) => `[${entityData.state}] [${unitOfMeasurement}]`);
hassMock.mockFunc("formatEntityState", (entityData: any) => `${entityData.state} [${unitOfMeasurement}]`);

const { state, level, unit } = getBatteryLevel({ entity: "mocked_entity", default_state_formatting: defaultStateFormatting, state_map: stateMap }, hassMock.hass);

Expand Down

0 comments on commit 4834601

Please sign in to comment.