Add constant selector (#15783)
This commit is contained in:
parent
1861547d9b
commit
db62e9f922
|
@ -0,0 +1,38 @@
|
||||||
|
import { LitElement, nothing } from "lit";
|
||||||
|
import { customElement, property } from "lit/decorators";
|
||||||
|
import { ConstantSelector } from "../../data/selector";
|
||||||
|
|
||||||
|
@customElement("ha-selector-constant")
|
||||||
|
export class HaSelectorConstant extends LitElement {
|
||||||
|
@property() public selector!: ConstantSelector;
|
||||||
|
|
||||||
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
|
@property() public localizeValue?: (key: string) => string;
|
||||||
|
|
||||||
|
protected render() {
|
||||||
|
if (this.disabled) {
|
||||||
|
return nothing;
|
||||||
|
}
|
||||||
|
|
||||||
|
const translationKey = this.selector.constant?.translation_key;
|
||||||
|
|
||||||
|
const translatedLabel =
|
||||||
|
translationKey && this.localizeValue
|
||||||
|
? this.localizeValue(`${translationKey}.value`)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
return (
|
||||||
|
translatedLabel ??
|
||||||
|
this.selector.constant?.label ??
|
||||||
|
this.selector.constant?.value ??
|
||||||
|
nothing
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface HTMLElementTagNameMap {
|
||||||
|
"ha-selector-constant": HaSelectorConstant;
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,7 @@ const LOAD_ELEMENTS = {
|
||||||
boolean: () => import("./ha-selector-boolean"),
|
boolean: () => import("./ha-selector-boolean"),
|
||||||
color_rgb: () => import("./ha-selector-color-rgb"),
|
color_rgb: () => import("./ha-selector-color-rgb"),
|
||||||
config_entry: () => import("./ha-selector-config-entry"),
|
config_entry: () => import("./ha-selector-config-entry"),
|
||||||
|
constant: () => import("./ha-selector-constant"),
|
||||||
date: () => import("./ha-selector-date"),
|
date: () => import("./ha-selector-date"),
|
||||||
datetime: () => import("./ha-selector-datetime"),
|
datetime: () => import("./ha-selector-datetime"),
|
||||||
device: () => import("./ha-selector-device"),
|
device: () => import("./ha-selector-device"),
|
||||||
|
|
|
@ -360,10 +360,21 @@ export class HaServiceControl extends LitElement {
|
||||||
|
|
||||||
if (checked) {
|
if (checked) {
|
||||||
this._checkedKeys.add(key);
|
this._checkedKeys.add(key);
|
||||||
const defaultValue = this._getServiceInfo(
|
const field = this._getServiceInfo(
|
||||||
this._value?.service,
|
this._value?.service,
|
||||||
this.hass.services
|
this.hass.services
|
||||||
)?.fields.find((field) => field.key === key)?.default;
|
)?.fields.find((_field) => _field.key === key);
|
||||||
|
|
||||||
|
let defaultValue = field?.default;
|
||||||
|
|
||||||
|
if (
|
||||||
|
defaultValue == null &&
|
||||||
|
field?.selector &&
|
||||||
|
"constant" in field.selector
|
||||||
|
) {
|
||||||
|
defaultValue = field.selector.constant?.value;
|
||||||
|
}
|
||||||
|
|
||||||
if (defaultValue != null) {
|
if (defaultValue != null) {
|
||||||
data = {
|
data = {
|
||||||
...this._value?.data,
|
...this._value?.data,
|
||||||
|
|
|
@ -13,6 +13,7 @@ export type Selector =
|
||||||
| ColorRGBSelector
|
| ColorRGBSelector
|
||||||
| ColorTempSelector
|
| ColorTempSelector
|
||||||
| ConfigEntrySelector
|
| ConfigEntrySelector
|
||||||
|
| ConstantSelector
|
||||||
| DateSelector
|
| DateSelector
|
||||||
| DateTimeSelector
|
| DateTimeSelector
|
||||||
| DeviceSelector
|
| DeviceSelector
|
||||||
|
@ -88,6 +89,14 @@ export interface ConfigEntrySelector {
|
||||||
} | null;
|
} | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ConstantSelector {
|
||||||
|
constant: {
|
||||||
|
value: string | number | boolean;
|
||||||
|
label?: string;
|
||||||
|
translation_key?: string;
|
||||||
|
} | null;
|
||||||
|
}
|
||||||
|
|
||||||
export interface DateSelector {
|
export interface DateSelector {
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
date: {} | null;
|
date: {} | null;
|
||||||
|
|
Loading…
Reference in New Issue