Add constant selector (#15783)

This commit is contained in:
Paul Bottein 2023-03-16 14:22:19 +01:00 committed by GitHub
parent 1861547d9b
commit db62e9f922
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -17,6 +17,7 @@ const LOAD_ELEMENTS = {
boolean: () => import("./ha-selector-boolean"),
color_rgb: () => import("./ha-selector-color-rgb"),
config_entry: () => import("./ha-selector-config-entry"),
constant: () => import("./ha-selector-constant"),
date: () => import("./ha-selector-date"),
datetime: () => import("./ha-selector-datetime"),
device: () => import("./ha-selector-device"),

View File

@ -360,10 +360,21 @@ export class HaServiceControl extends LitElement {
if (checked) {
this._checkedKeys.add(key);
const defaultValue = this._getServiceInfo(
const field = this._getServiceInfo(
this._value?.service,
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) {
data = {
...this._value?.data,

View File

@ -13,6 +13,7 @@ export type Selector =
| ColorRGBSelector
| ColorTempSelector
| ConfigEntrySelector
| ConstantSelector
| DateSelector
| DateTimeSelector
| DeviceSelector
@ -88,6 +89,14 @@ export interface ConfigEntrySelector {
} | null;
}
export interface ConstantSelector {
constant: {
value: string | number | boolean;
label?: string;
translation_key?: string;
} | null;
}
export interface DateSelector {
// eslint-disable-next-line @typescript-eslint/ban-types
date: {} | null;