mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Uses websocket to get sensor device class units (#15014)
* Uses websocket to get sensor device class units * Only show convertible units * Update endpoint
This commit is contained in:
parent
1aa23d75b0
commit
2eb5335a68
@ -1,2 +1,15 @@
|
|||||||
|
import { HomeAssistant } from "../types";
|
||||||
|
|
||||||
export const SENSOR_DEVICE_CLASS_BATTERY = "battery";
|
export const SENSOR_DEVICE_CLASS_BATTERY = "battery";
|
||||||
export const SENSOR_DEVICE_CLASS_TIMESTAMP = "timestamp";
|
export const SENSOR_DEVICE_CLASS_TIMESTAMP = "timestamp";
|
||||||
|
|
||||||
|
export type SensorDeviceClassUnits = { units: string[] };
|
||||||
|
|
||||||
|
export const getSensorDeviceClassConvertibleUnits = (
|
||||||
|
hass: HomeAssistant,
|
||||||
|
deviceClass: string
|
||||||
|
): Promise<SensorDeviceClassUnits> =>
|
||||||
|
hass.callWS({
|
||||||
|
type: "sensor/device_class_convertible_units",
|
||||||
|
device_class: deviceClass,
|
||||||
|
});
|
||||||
|
@ -67,6 +67,7 @@ import {
|
|||||||
updateEntityRegistryEntry,
|
updateEntityRegistryEntry,
|
||||||
} from "../../../data/entity_registry";
|
} from "../../../data/entity_registry";
|
||||||
import { domainToName } from "../../../data/integration";
|
import { domainToName } from "../../../data/integration";
|
||||||
|
import { getSensorDeviceClassConvertibleUnits } from "../../../data/sensor";
|
||||||
import { showOptionsFlowDialog } from "../../../dialogs/config-flow/show-dialog-options-flow";
|
import { showOptionsFlowDialog } from "../../../dialogs/config-flow/show-dialog-options-flow";
|
||||||
import {
|
import {
|
||||||
showAlertDialog,
|
showAlertDialog,
|
||||||
@ -117,58 +118,6 @@ const OVERRIDE_NUMBER_UNITS = {
|
|||||||
temperature: ["°C", "°F", "K"],
|
temperature: ["°C", "°F", "K"],
|
||||||
};
|
};
|
||||||
|
|
||||||
const OVERRIDE_SENSOR_UNITS = {
|
|
||||||
current: ["A", "mA"],
|
|
||||||
data_rate: [
|
|
||||||
"bit/s",
|
|
||||||
"kbit/s",
|
|
||||||
"Mbit/s",
|
|
||||||
"Gbit/s",
|
|
||||||
"B/s",
|
|
||||||
"kB/s",
|
|
||||||
"MB/s",
|
|
||||||
"GB/s",
|
|
||||||
"KiB/s",
|
|
||||||
"MiB/s",
|
|
||||||
"GiB/s",
|
|
||||||
],
|
|
||||||
data_size: [
|
|
||||||
"bit",
|
|
||||||
"kbit",
|
|
||||||
"Mbit",
|
|
||||||
"Gbit",
|
|
||||||
"B",
|
|
||||||
"kB",
|
|
||||||
"MB",
|
|
||||||
"GB",
|
|
||||||
"TB",
|
|
||||||
"PB",
|
|
||||||
"EB",
|
|
||||||
"ZB",
|
|
||||||
"YB",
|
|
||||||
"KiB",
|
|
||||||
"MiB",
|
|
||||||
"GiB",
|
|
||||||
"TiB",
|
|
||||||
"PiB",
|
|
||||||
"EiB",
|
|
||||||
"ZiB",
|
|
||||||
"YiB",
|
|
||||||
],
|
|
||||||
distance: ["cm", "ft", "in", "km", "m", "mi", "mm", "yd"],
|
|
||||||
gas: ["CCF", "ft³", "m³"],
|
|
||||||
precipitation: ["cm", "in", "mm"],
|
|
||||||
precipitation_intensity: ["in/d", "in/h", "mm/d", "mm/h"],
|
|
||||||
pressure: ["hPa", "Pa", "kPa", "bar", "cbar", "mbar", "mmHg", "inHg", "psi"],
|
|
||||||
speed: ["ft/s", "in/d", "in/h", "km/h", "kn", "m/s", "mm/d", "mm/h", "mph"],
|
|
||||||
temperature: ["°C", "°F", "K"],
|
|
||||||
voltage: ["V", "mV"],
|
|
||||||
volume: ["CCF", "fl. oz.", "ft³", "gal", "L", "mL", "m³"],
|
|
||||||
water: ["CCF", "ft³", "gal", "L", "m³"],
|
|
||||||
weight: ["g", "kg", "lb", "mg", "oz", "st", "µg"],
|
|
||||||
wind_speed: ["ft/s", "km/h", "kn", "mph", "m/s"],
|
|
||||||
};
|
|
||||||
|
|
||||||
const OVERRIDE_WEATHER_UNITS = {
|
const OVERRIDE_WEATHER_UNITS = {
|
||||||
precipitation: ["mm", "in"],
|
precipitation: ["mm", "in"],
|
||||||
pressure: ["hPa", "mbar", "mmHg", "inHg"],
|
pressure: ["hPa", "mbar", "mmHg", "inHg"],
|
||||||
@ -223,6 +172,8 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
|
|
||||||
@state() private _cameraPrefs?: CameraPreferences;
|
@state() private _cameraPrefs?: CameraPreferences;
|
||||||
|
|
||||||
|
@state() private _sensorDeviceClassConvertibleUnits?: string[];
|
||||||
|
|
||||||
private _origEntityId!: string;
|
private _origEntityId!: string;
|
||||||
|
|
||||||
private _deviceLookup?: Record<string, DeviceRegistryEntry>;
|
private _deviceLookup?: Record<string, DeviceRegistryEntry>;
|
||||||
@ -326,6 +277,22 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async updated(changedProps: PropertyValues): Promise<void> {
|
||||||
|
if (changedProps.has("_deviceClass")) {
|
||||||
|
const domain = computeDomain(this.entry.entity_id);
|
||||||
|
|
||||||
|
if (domain === "sensor" && this._deviceClass) {
|
||||||
|
const { units } = await getSensorDeviceClassConvertibleUnits(
|
||||||
|
this.hass,
|
||||||
|
this._deviceClass
|
||||||
|
);
|
||||||
|
this._sensorDeviceClassConvertibleUnits = units;
|
||||||
|
} else {
|
||||||
|
this._sensorDeviceClassConvertibleUnits = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
if (this.entry.entity_id !== this._origEntityId) {
|
if (this.entry.entity_id !== this._origEntityId) {
|
||||||
return html``;
|
return html``;
|
||||||
@ -470,7 +437,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
${domain === "sensor" &&
|
${domain === "sensor" &&
|
||||||
this._deviceClass &&
|
this._deviceClass &&
|
||||||
stateObj?.attributes.unit_of_measurement &&
|
stateObj?.attributes.unit_of_measurement &&
|
||||||
OVERRIDE_SENSOR_UNITS[this._deviceClass]?.includes(
|
this._sensorDeviceClassConvertibleUnits?.includes(
|
||||||
stateObj?.attributes.unit_of_measurement
|
stateObj?.attributes.unit_of_measurement
|
||||||
)
|
)
|
||||||
? html`
|
? html`
|
||||||
@ -484,7 +451,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
|||||||
@selected=${this._unitChanged}
|
@selected=${this._unitChanged}
|
||||||
@closed=${stopPropagation}
|
@closed=${stopPropagation}
|
||||||
>
|
>
|
||||||
${OVERRIDE_SENSOR_UNITS[this._deviceClass].map(
|
${this._sensorDeviceClassConvertibleUnits.map(
|
||||||
(unit: string) => html`
|
(unit: string) => html`
|
||||||
<mwc-list-item .value=${unit}>${unit}</mwc-list-item>
|
<mwc-list-item .value=${unit}>${unit}</mwc-list-item>
|
||||||
`
|
`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user