Skip to content

Commit

Permalink
Format number attribute for media player (#18949)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored Dec 8, 2023
1 parent 80edeeb commit b855b3e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/common/entity/compute_attribute_display.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HassConfig, HassEntity } from "home-assistant-js-websocket";
import {
DOMAIN_ATTRIBUTES_FORMATERS,
DOMAIN_ATTRIBUTES_UNITS,
TEMPERATURE_ATTRIBUTES,
} from "../../data/entity_attributes";
Expand All @@ -14,11 +15,10 @@ import { formatNumber } from "../number/format_number";
import { capitalizeFirstLetter } from "../string/capitalize-first-letter";
import { isDate } from "../string/is_date";
import { isTimestamp } from "../string/is_timestamp";
import { blankBeforePercent } from "../translations/blank_before_percent";
import { blankBeforeUnit } from "../translations/blank_before_unit";
import { LocalizeFunc } from "../translations/localize";
import { computeDomain } from "./compute_domain";
import { computeStateDomain } from "./compute_state_domain";
import { blankBeforeUnit } from "../translations/blank_before_unit";

export const computeAttributeValueDisplay = (
localize: LocalizeFunc,
Expand All @@ -39,19 +39,18 @@ export const computeAttributeValueDisplay = (

// Number value, return formatted number
if (typeof attributeValue === "number") {
const formattedValue = formatNumber(attributeValue, locale);

const domain = computeStateDomain(stateObj);

const formatter = DOMAIN_ATTRIBUTES_FORMATERS[domain]?.[attribute];

const formattedValue = formatter
? formatter(attributeValue, locale)
: formatNumber(attributeValue, locale);

let unit = DOMAIN_ATTRIBUTES_UNITS[domain]?.[attribute] as
| string
| undefined;

if (domain === "light" && attribute === "brightness") {
const percentage = Math.round((attributeValue / 255) * 100);
return `${percentage}${blankBeforePercent(locale)}%`;
}

if (domain === "weather") {
unit = getWeatherUnit(config, stateObj as WeatherEntity, attribute);
}
Expand Down
22 changes: 22 additions & 0 deletions src/data/entity_attributes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { formatDuration } from "../common/datetime/duration";
import { FrontendLocaleData } from "./translation";

export const STATE_ATTRIBUTES = [
"entity_id",
"assumed_state",
Expand Down Expand Up @@ -64,6 +67,7 @@ export const DOMAIN_ATTRIBUTES_UNITS = {
color_temp_kelvin: "K",
min_color_temp_kelvin: "K",
max_color_temp_kelvin: "K",
brightness: "%",
},
sun: {
elevation: "°",
Expand All @@ -74,4 +78,22 @@ export const DOMAIN_ATTRIBUTES_UNITS = {
sensor: {
battery_level: "%",
},
media_player: {
volume_level: "%",
},
} as const satisfies Record<string, Record<string, string>>;

type Formatter = (value: number, locale: FrontendLocaleData) => string;

export const DOMAIN_ATTRIBUTES_FORMATERS: Record<
string,
Record<string, Formatter>
> = {
light: {
brightness: (value) => Math.round((value / 255) * 100).toString(),
},
media_player: {
volume_level: (value) => Math.round(value * 100).toString(),
media_duration: (value) => formatDuration(value.toString(), "s"),
},
};

0 comments on commit b855b3e

Please sign in to comment.