mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-08 18:06:36 +00:00
Allow Sensor Units to be updated via Entity Registry (#12143)
This commit is contained in:
parent
396791b805
commit
bad776b979
@ -41,6 +41,10 @@ export interface EntityRegistryEntryUpdateParams {
|
|||||||
disabled_by?: string | null;
|
disabled_by?: string | null;
|
||||||
hidden_by: string | null;
|
hidden_by: string | null;
|
||||||
new_entity_id?: string;
|
new_entity_id?: string;
|
||||||
|
options_domain?: string;
|
||||||
|
options?: {
|
||||||
|
unit_of_measurement?: string | null;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const findBatteryEntity = (
|
export const findBatteryEntity = (
|
||||||
|
@ -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")
|
@customElement("entity-registry-settings")
|
||||||
export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
@ -107,6 +112,8 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
|
|
||||||
@state() private _helperConfigEntry?: ConfigEntry;
|
@state() private _helperConfigEntry?: ConfigEntry;
|
||||||
|
|
||||||
|
@state() private _unit_of_measurement?: string | null;
|
||||||
|
|
||||||
@state() private _error?: string;
|
@state() private _error?: string;
|
||||||
|
|
||||||
@state() private _submitting?: boolean;
|
@state() private _submitting?: boolean;
|
||||||
@ -167,6 +174,13 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
const domain = computeDomain(this.entry.entity_id);
|
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];
|
const deviceClasses: string[][] = OVERRIDE_DEVICE_CLASSES[domain];
|
||||||
|
|
||||||
if (!deviceClasses) {
|
if (!deviceClasses) {
|
||||||
@ -269,6 +283,30 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
</ha-select>
|
</ha-select>
|
||||||
`
|
`
|
||||||
: ""}
|
: ""}
|
||||||
|
${this._deviceClass &&
|
||||||
|
stateObj.attributes.unit_of_measurement &&
|
||||||
|
OVERRIDE_SENSOR_UNITS[this._deviceClass]?.includes(
|
||||||
|
stateObj.attributes.unit_of_measurement
|
||||||
|
)
|
||||||
|
? html`
|
||||||
|
<ha-select
|
||||||
|
.label=${this.hass.localize(
|
||||||
|
"ui.dialogs.entity_registry.editor.unit_of_measurement"
|
||||||
|
)}
|
||||||
|
.value=${stateObj.attributes.unit_of_measurement}
|
||||||
|
naturalMenuWidth
|
||||||
|
fixedMenuPosition
|
||||||
|
@selected=${this._unitChanged}
|
||||||
|
@closed=${stopPropagation}
|
||||||
|
>
|
||||||
|
${OVERRIDE_SENSOR_UNITS[this._deviceClass].map(
|
||||||
|
(unit: string) => html`
|
||||||
|
<mwc-list-item .value=${unit}>${unit}</mwc-list-item>
|
||||||
|
`
|
||||||
|
)}
|
||||||
|
</ha-select>
|
||||||
|
`
|
||||||
|
: ""}
|
||||||
<ha-textfield
|
<ha-textfield
|
||||||
error-message="Domain needs to stay the same"
|
error-message="Domain needs to stay the same"
|
||||||
.value=${this._entityId}
|
.value=${this._entityId}
|
||||||
@ -469,6 +507,11 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
this._deviceClass = ev.target.value;
|
this._deviceClass = ev.target.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _unitChanged(ev): void {
|
||||||
|
this._error = undefined;
|
||||||
|
this._unit_of_measurement = ev.target.value;
|
||||||
|
}
|
||||||
|
|
||||||
private _areaPicked(ev: CustomEvent) {
|
private _areaPicked(ev: CustomEvent) {
|
||||||
this._error = undefined;
|
this._error = undefined;
|
||||||
this._areaId = ev.detail.value;
|
this._areaId = ev.detail.value;
|
||||||
@ -509,6 +552,11 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
device_class: this._deviceClass || null,
|
device_class: this._deviceClass || null,
|
||||||
new_entity_id: this._entityId.trim(),
|
new_entity_id: this._entityId.trim(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const stateObj: HassEntity | undefined =
|
||||||
|
this.hass.states[this.entry.entity_id];
|
||||||
|
const domain = computeDomain(this.entry.entity_id);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.entry.disabled_by !== this._disabledBy &&
|
this.entry.disabled_by !== this._disabledBy &&
|
||||||
(this._disabledBy === null || this._disabledBy === "user")
|
(this._disabledBy === null || this._disabledBy === "user")
|
||||||
@ -521,6 +569,13 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
) {
|
) {
|
||||||
params.hidden_by = this._hiddenBy;
|
params.hidden_by = this._hiddenBy;
|
||||||
}
|
}
|
||||||
|
if (
|
||||||
|
domain === "sensor" &&
|
||||||
|
stateObj?.attributes?.unit_of_measurement !== this._unit_of_measurement
|
||||||
|
) {
|
||||||
|
params.options_domain = "sensor";
|
||||||
|
params.options = { unit_of_measurement: this._unit_of_measurement };
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const result = await updateEntityRegistryEntry(
|
const result = await updateEntityRegistryEntry(
|
||||||
this.hass!,
|
this.hass!,
|
||||||
|
@ -786,6 +786,7 @@
|
|||||||
"icon": "Icon",
|
"icon": "Icon",
|
||||||
"icon_error": "Icons should be in the format 'prefix:iconname', e.g. 'mdi:home'",
|
"icon_error": "Icons should be in the format 'prefix:iconname', e.g. 'mdi:home'",
|
||||||
"entity_id": "Entity ID",
|
"entity_id": "Entity ID",
|
||||||
|
"unit_of_measurement": "Unit of Measurement",
|
||||||
"device_class": "Show as",
|
"device_class": "Show as",
|
||||||
"device_classes": {
|
"device_classes": {
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user