From b855b3e1034c0df9b8f41f11097f869b257d701c Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Fri, 8 Dec 2023 10:14:55 +0100 Subject: [PATCH] Format number attribute for media player (#18949) --- .../entity/compute_attribute_display.ts | 17 +++++++------- src/data/entity_attributes.ts | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/common/entity/compute_attribute_display.ts b/src/common/entity/compute_attribute_display.ts index bfb394a2d0..77bc61bbd4 100644 --- a/src/common/entity/compute_attribute_display.ts +++ b/src/common/entity/compute_attribute_display.ts @@ -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"; @@ -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, @@ -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); } diff --git a/src/data/entity_attributes.ts b/src/data/entity_attributes.ts index 00e2e6d3dc..3a07ae022a 100644 --- a/src/data/entity_attributes.ts +++ b/src/data/entity_attributes.ts @@ -1,3 +1,6 @@ +import { formatDuration } from "../common/datetime/duration"; +import { FrontendLocaleData } from "./translation"; + export const STATE_ATTRIBUTES = [ "entity_id", "assumed_state", @@ -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: "°", @@ -74,4 +78,22 @@ export const DOMAIN_ATTRIBUTES_UNITS = { sensor: { battery_level: "%", }, + media_player: { + volume_level: "%", + }, } as const satisfies Record>; + +type Formatter = (value: number, locale: FrontendLocaleData) => string; + +export const DOMAIN_ATTRIBUTES_FORMATERS: Record< + string, + Record +> = { + 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"), + }, +};