mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-19 07:16:39 +00:00
Fetch number units from core (#15175)
This commit is contained in:
parent
624bfbbaf1
commit
5e9ae36577
12
src/data/number.ts
Normal file
12
src/data/number.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { HomeAssistant } from "../types";
|
||||
|
||||
export type NumberDeviceClassUnits = { units: string[] };
|
||||
|
||||
export const getNumberDeviceClassConvertibleUnits = (
|
||||
hass: HomeAssistant,
|
||||
deviceClass: string
|
||||
): Promise<NumberDeviceClassUnits> =>
|
||||
hass.callWS({
|
||||
type: "number/device_class_convertible_units",
|
||||
device_class: deviceClass,
|
||||
});
|
@ -67,6 +67,7 @@ import {
|
||||
updateEntityRegistryEntry,
|
||||
} from "../../../data/entity_registry";
|
||||
import { domainToName } from "../../../data/integration";
|
||||
import { getNumberDeviceClassConvertibleUnits } from "../../../data/number";
|
||||
import { getSensorDeviceClassConvertibleUnits } from "../../../data/sensor";
|
||||
import { showOptionsFlowDialog } from "../../../dialogs/config-flow/show-dialog-options-flow";
|
||||
import {
|
||||
@ -114,10 +115,6 @@ const OVERRIDE_DEVICE_CLASSES = {
|
||||
],
|
||||
};
|
||||
|
||||
const OVERRIDE_NUMBER_UNITS = {
|
||||
temperature: ["°C", "°F", "K"],
|
||||
};
|
||||
|
||||
const OVERRIDE_WEATHER_UNITS = {
|
||||
precipitation: ["mm", "in"],
|
||||
pressure: ["hPa", "mbar", "mmHg", "inHg"],
|
||||
@ -172,6 +169,8 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
||||
|
||||
@state() private _cameraPrefs?: CameraPreferences;
|
||||
|
||||
@state() private _numberDeviceClassConvertibleUnits?: string[];
|
||||
|
||||
@state() private _sensorDeviceClassConvertibleUnits?: string[];
|
||||
|
||||
private _origEntityId!: string;
|
||||
@ -281,6 +280,15 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
||||
if (changedProps.has("_deviceClass")) {
|
||||
const domain = computeDomain(this.entry.entity_id);
|
||||
|
||||
if (domain === "number" && this._deviceClass) {
|
||||
const { units } = await getNumberDeviceClassConvertibleUnits(
|
||||
this.hass,
|
||||
this._deviceClass
|
||||
);
|
||||
this._numberDeviceClassConvertibleUnits = units;
|
||||
} else {
|
||||
this._numberDeviceClassConvertibleUnits = [];
|
||||
}
|
||||
if (domain === "sensor" && this._deviceClass) {
|
||||
const { units } = await getSensorDeviceClassConvertibleUnits(
|
||||
this.hass,
|
||||
@ -412,7 +420,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
||||
${domain === "number" &&
|
||||
this._deviceClass &&
|
||||
stateObj?.attributes.unit_of_measurement &&
|
||||
OVERRIDE_NUMBER_UNITS[this._deviceClass]?.includes(
|
||||
this._numberDeviceClassConvertibleUnits?.includes(
|
||||
stateObj?.attributes.unit_of_measurement
|
||||
)
|
||||
? html`
|
||||
@ -426,7 +434,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
|
||||
@selected=${this._unitChanged}
|
||||
@closed=${stopPropagation}
|
||||
>
|
||||
${OVERRIDE_NUMBER_UNITS[this._deviceClass].map(
|
||||
${this._numberDeviceClassConvertibleUnits.map(
|
||||
(unit: string) => html`
|
||||
<mwc-list-item .value=${unit}>${unit}</mwc-list-item>
|
||||
`
|
||||
|
Loading…
x
Reference in New Issue
Block a user