mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Allow overriding a sensor's precision (#15176)
* Allow overriding a sensor's precision * use a dropdown for choosing precision * Address review comments * Handle undefined state * Loose equality FTW * Apply suggestions from code review
This commit is contained in:
parent
8b4b19cc96
commit
ddfaa67456
@ -30,6 +30,7 @@ export interface ExtEntityRegistryEntry extends EntityRegistryEntry {
|
|||||||
device_class?: string;
|
device_class?: string;
|
||||||
original_device_class?: string;
|
original_device_class?: string;
|
||||||
aliases: string[];
|
aliases: string[];
|
||||||
|
options: EntityRegistryOptions | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UpdateEntityRegistryEntryResult {
|
export interface UpdateEntityRegistryEntryResult {
|
||||||
@ -39,6 +40,7 @@ export interface UpdateEntityRegistryEntryResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface SensorEntityOptions {
|
export interface SensorEntityOptions {
|
||||||
|
precision?: number | null;
|
||||||
unit_of_measurement?: string | null;
|
unit_of_measurement?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,6 +56,12 @@ export interface WeatherEntityOptions {
|
|||||||
wind_speed_unit?: string | null;
|
wind_speed_unit?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface EntityRegistryOptions {
|
||||||
|
number?: NumberEntityOptions;
|
||||||
|
sensor?: SensorEntityOptions;
|
||||||
|
weather?: WeatherEntityOptions;
|
||||||
|
}
|
||||||
|
|
||||||
export interface EntityRegistryEntryUpdateParams {
|
export interface EntityRegistryEntryUpdateParams {
|
||||||
name?: string | null;
|
name?: string | null;
|
||||||
icon?: string | null;
|
icon?: string | null;
|
||||||
|
@ -62,6 +62,7 @@ import {
|
|||||||
EntityRegistryEntry,
|
EntityRegistryEntry,
|
||||||
EntityRegistryEntryUpdateParams,
|
EntityRegistryEntryUpdateParams,
|
||||||
ExtEntityRegistryEntry,
|
ExtEntityRegistryEntry,
|
||||||
|
SensorEntityOptions,
|
||||||
fetchEntityRegistry,
|
fetchEntityRegistry,
|
||||||
removeEntityRegistryEntry,
|
removeEntityRegistryEntry,
|
||||||
updateEntityRegistryEntry,
|
updateEntityRegistryEntry,
|
||||||
@ -125,6 +126,16 @@ const OVERRIDE_WEATHER_UNITS = {
|
|||||||
|
|
||||||
const SWITCH_AS_DOMAINS = ["cover", "fan", "light", "lock", "siren"];
|
const SWITCH_AS_DOMAINS = ["cover", "fan", "light", "lock", "siren"];
|
||||||
|
|
||||||
|
const PRECISIONS = [0, 1, 2, 3, 4, 5, 6];
|
||||||
|
|
||||||
|
function precisionLabel(precision: number, _state?: string) {
|
||||||
|
const state_float =
|
||||||
|
_state === undefined || isNaN(parseFloat(_state))
|
||||||
|
? 0.0
|
||||||
|
: parseFloat(_state);
|
||||||
|
return state_float.toFixed(precision);
|
||||||
|
}
|
||||||
|
|
||||||
@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;
|
||||||
@ -153,6 +164,8 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
|
|
||||||
@state() private _unit_of_measurement?: string | null;
|
@state() private _unit_of_measurement?: string | null;
|
||||||
|
|
||||||
|
@state() private _precision?: number | null;
|
||||||
|
|
||||||
@state() private _precipitation_unit?: string | null;
|
@state() private _precipitation_unit?: string | null;
|
||||||
|
|
||||||
@state() private _pressure_unit?: string | null;
|
@state() private _pressure_unit?: string | null;
|
||||||
@ -250,6 +263,10 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
this._unit_of_measurement = stateObj?.attributes?.unit_of_measurement;
|
this._unit_of_measurement = stateObj?.attributes?.unit_of_measurement;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (domain === "sensor") {
|
||||||
|
this._precision = this.entry.options?.sensor?.precision;
|
||||||
|
}
|
||||||
|
|
||||||
if (domain === "weather") {
|
if (domain === "weather") {
|
||||||
const stateObj: HassEntity | undefined =
|
const stateObj: HassEntity | undefined =
|
||||||
this.hass.states[this.entry.entity_id];
|
this.hass.states[this.entry.entity_id];
|
||||||
@ -467,6 +484,44 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
</ha-select>
|
</ha-select>
|
||||||
`
|
`
|
||||||
: ""}
|
: ""}
|
||||||
|
${domain === "sensor" &&
|
||||||
|
// Allow customizing the precision for a sensor with numerical device class,
|
||||||
|
// a unit of measurement or state class
|
||||||
|
((this._deviceClass &&
|
||||||
|
!["date", "enum", "timestamp"].includes(this._deviceClass)) ||
|
||||||
|
stateObj?.attributes.unit_of_measurement ||
|
||||||
|
stateObj?.attributes.state_class)
|
||||||
|
? html`
|
||||||
|
<ha-select
|
||||||
|
.label=${this.hass.localize(
|
||||||
|
"ui.dialogs.entity_registry.editor.precision"
|
||||||
|
)}
|
||||||
|
.value=${this._precision == null
|
||||||
|
? "default"
|
||||||
|
: this._precision.toString()}
|
||||||
|
naturalMenuWidth
|
||||||
|
fixedMenuPosition
|
||||||
|
@selected=${this._precisionChanged}
|
||||||
|
@closed=${stopPropagation}
|
||||||
|
>
|
||||||
|
<mwc-list-item .value=${"default"}
|
||||||
|
>${this.hass.localize(
|
||||||
|
"ui.dialogs.entity_registry.editor.precision_default"
|
||||||
|
)}</mwc-list-item
|
||||||
|
>
|
||||||
|
${PRECISIONS.map(
|
||||||
|
(precision) => html`
|
||||||
|
<mwc-list-item .value=${precision.toString()}>
|
||||||
|
${precisionLabel(
|
||||||
|
precision,
|
||||||
|
this.hass.states[this.entry.entity_id]?.state
|
||||||
|
)}
|
||||||
|
</mwc-list-item>
|
||||||
|
`
|
||||||
|
)}
|
||||||
|
</ha-select>
|
||||||
|
`
|
||||||
|
: ""}
|
||||||
${domain === "weather"
|
${domain === "weather"
|
||||||
? html`
|
? html`
|
||||||
<ha-select
|
<ha-select
|
||||||
@ -892,6 +947,12 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
this._precipitation_unit = ev.target.value;
|
this._precipitation_unit = ev.target.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _precisionChanged(ev): void {
|
||||||
|
this._error = undefined;
|
||||||
|
this._precision =
|
||||||
|
ev.target.value === "default" ? null : Number(ev.target.value);
|
||||||
|
}
|
||||||
|
|
||||||
private _pressureUnitChanged(ev): void {
|
private _pressureUnitChanged(ev): void {
|
||||||
this._error = undefined;
|
this._error = undefined;
|
||||||
this._pressure_unit = ev.target.value;
|
this._pressure_unit = ev.target.value;
|
||||||
@ -1081,7 +1142,16 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
stateObj?.attributes?.unit_of_measurement !== this._unit_of_measurement
|
stateObj?.attributes?.unit_of_measurement !== this._unit_of_measurement
|
||||||
) {
|
) {
|
||||||
params.options_domain = domain;
|
params.options_domain = domain;
|
||||||
params.options = { unit_of_measurement: this._unit_of_measurement };
|
params.options = this.entry.options?.[domain] || {};
|
||||||
|
params.options.unit_of_measurement = this._unit_of_measurement;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
domain === "sensor" &&
|
||||||
|
this.entry.options?.[domain]?.precision !== this._precision
|
||||||
|
) {
|
||||||
|
params.options_domain = domain;
|
||||||
|
params.options = params.options || this.entry.options?.[domain] || {};
|
||||||
|
(params.options as SensorEntityOptions).precision = this._precision;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
domain === "weather" &&
|
domain === "weather" &&
|
||||||
|
@ -905,6 +905,8 @@
|
|||||||
"entity_id": "Entity ID",
|
"entity_id": "Entity ID",
|
||||||
"unit_of_measurement": "Unit of Measurement",
|
"unit_of_measurement": "Unit of Measurement",
|
||||||
"precipitation_unit": "Precipitation unit",
|
"precipitation_unit": "Precipitation unit",
|
||||||
|
"precision": "Precision",
|
||||||
|
"precision_default": "Use default",
|
||||||
"pressure_unit": "Barometric pressure unit",
|
"pressure_unit": "Barometric pressure unit",
|
||||||
"temperature_unit": "Temperature unit",
|
"temperature_unit": "Temperature unit",
|
||||||
"visibility_unit": "Visibility unit",
|
"visibility_unit": "Visibility unit",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user