From bad776b979acb32188d98c7549e473ce50bbf770 Mon Sep 17 00:00:00 2001 From: Zack Barett Date: Wed, 30 Mar 2022 13:03:19 -0500 Subject: [PATCH] Allow Sensor Units to be updated via Entity Registry (#12143) --- src/data/entity_registry.ts | 4 ++ .../entities/entity-registry-settings.ts | 55 +++++++++++++++++++ src/translations/en.json | 1 + 3 files changed, 60 insertions(+) diff --git a/src/data/entity_registry.ts b/src/data/entity_registry.ts index eaefcee6e3..e77d75c2b5 100644 --- a/src/data/entity_registry.ts +++ b/src/data/entity_registry.ts @@ -41,6 +41,10 @@ export interface EntityRegistryEntryUpdateParams { disabled_by?: string | null; hidden_by: string | null; new_entity_id?: string; + options_domain?: string; + options?: { + unit_of_measurement?: string | null; + }; } export const findBatteryEntity = ( diff --git a/src/panels/config/entities/entity-registry-settings.ts b/src/panels/config/entities/entity-registry-settings.ts index da5b40359f..a5c215bbd6 100644 --- a/src/panels/config/entities/entity-registry-settings.ts +++ b/src/panels/config/entities/entity-registry-settings.ts @@ -83,6 +83,11 @@ const OVERRIDE_DEVICE_CLASSES = { ], }; +const OVERRIDE_SENSOR_UNITS = { + temperature: ["°C", "°F", "K"], + pressure: ["hPa", "Pa", "kPa", "bar", "cbar", "mbar", "mmHg", "inHg", "psi"], +}; + @customElement("entity-registry-settings") export class EntityRegistrySettings extends SubscribeMixin(LitElement) { @property({ attribute: false }) public hass!: HomeAssistant; @@ -107,6 +112,8 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { @state() private _helperConfigEntry?: ConfigEntry; + @state() private _unit_of_measurement?: string | null; + @state() private _error?: string; @state() private _submitting?: boolean; @@ -167,6 +174,13 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { : undefined; const domain = computeDomain(this.entry.entity_id); + + if (domain === "sensor") { + const stateObj: HassEntity | undefined = + this.hass.states[this.entry.entity_id]; + this._unit_of_measurement = stateObj?.attributes?.unit_of_measurement; + } + const deviceClasses: string[][] = OVERRIDE_DEVICE_CLASSES[domain]; if (!deviceClasses) { @@ -269,6 +283,30 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { ` : ""} + ${this._deviceClass && + stateObj.attributes.unit_of_measurement && + OVERRIDE_SENSOR_UNITS[this._deviceClass]?.includes( + stateObj.attributes.unit_of_measurement + ) + ? html` + + ${OVERRIDE_SENSOR_UNITS[this._deviceClass].map( + (unit: string) => html` + ${unit} + ` + )} + + ` + : ""}