Skip to content

Commit

Permalink
Merge pull request #655 from maxwroc/FixColorDecimals
Browse files Browse the repository at this point in the history
Fixed icon color for decimal entity values
  • Loading branch information
maxwroc authored Jan 13, 2024
2 parents 7bc2d9c + aab43ca commit c76defb
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "battery-state-card",
"version": "3.1.4",
"version": "3.1.5",
"description": "Battery State card for Home Assistant",
"main": "dist/battery-state-card.js",
"author": "Max Chodorowski",
Expand Down
4 changes: 2 additions & 2 deletions src/entity-fields/battery-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getBatteryLevel = (config: IBatteryEntityConfig, hass?: HomeAssista
const processedValue = stringProcessor.process(config.value_override.toString());
return {
state: processedValue,
level: isNumber(processedValue) ? Number(processedValue) : undefined,
level: isNumber(processedValue) ? toNumber(processedValue) : undefined,
unit: getUnit(processedValue, undefined, undefined, config, hass),
}
}
Expand Down Expand Up @@ -107,7 +107,7 @@ export const getBatteryLevel = (config: IBatteryEntityConfig, hass?: HomeAssista

return {
state: displayValue || state,
level: isNumber(state) ? Number(state) : undefined,
level: isNumber(state) ? toNumber(state) : undefined,
unit: getUnit(state, displayValue, unit, config, hass),
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/sorting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IBatteryCollection } from "./battery-provider";
import { isNumber, log, safeGetConfigArrayOfObjects } from "./utils";
import { isNumber, log, safeGetConfigArrayOfObjects, toNumber } from "./utils";

/**
* Sorts batteries by given criterias and returns their IDs
Expand Down Expand Up @@ -76,8 +76,8 @@ import { isNumber, log, safeGetConfigArrayOfObjects } from "./utils";
* @returns Comparison result
*/
const compareNumbers = (a: string, b: string): number => {
let aNum = Number(a);
let bNum = Number(b);
let aNum = toNumber(a);
let bNum = toNumber(b);
aNum = isNaN(aNum) ? -1 : aNum;
bNum = isNaN(bNum) ? -1 : bNum;
return aNum - bNum;
Expand Down
5 changes: 3 additions & 2 deletions test/other/entity-fields/battery-level.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,13 @@ describe("Battery level", () => {
[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: "Low", level: 45, unit: undefined }, [ { from: "45", to: "45", display: "Low" } ]], // test when the display value was changed by state_map
[true, "45.4", "dbm", { state: "45,4", level: 45.4, unit: "[dbm]" }, undefined, ","], // test when default HA formatting returns state with comma as decimal point
])
("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, decimalPoint: string = ".") => {

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.replace(".", decimalPoint)} [${unitOfMeasurement}]`);

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

Expand Down

0 comments on commit c76defb

Please sign in to comment.