Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/data/entity_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
55 changes: 55 additions & 0 deletions src/panels/config/entities/entity-registry-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -269,6 +283,30 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
</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
error-message="Domain needs to stay the same"
.value=${this._entityId}
Expand Down Expand Up @@ -469,6 +507,11 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
this._deviceClass = ev.target.value;
}

private _unitChanged(ev): void {
this._error = undefined;
this._unit_of_measurement = ev.target.value;
}

private _areaPicked(ev: CustomEvent) {
this._error = undefined;
this._areaId = ev.detail.value;
Expand Down Expand Up @@ -509,6 +552,11 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
device_class: this._deviceClass || null,
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 (
this.entry.disabled_by !== this._disabledBy &&
(this._disabledBy === null || this._disabledBy === "user")
Expand All @@ -521,6 +569,13 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
) {
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 {
const result = await updateEntityRegistryEntry(
this.hass!,
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@
"icon": "Icon",
"icon_error": "Icons should be in the format 'prefix:iconname', e.g. 'mdi:home'",
"entity_id": "Entity ID",
"unit_of_measurement": "Unit of Measurement",
"device_class": "Show as",
"device_classes": {
"binary_sensor": {
Expand Down