From db62e9f922af1385535582f5bb741c3c9f535fe1 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Thu, 16 Mar 2023 14:22:19 +0100 Subject: [PATCH] Add constant selector (#15783) --- .../ha-selector/ha-selector-constant.ts | 38 +++++++++++++++++++ src/components/ha-selector/ha-selector.ts | 1 + src/components/ha-service-control.ts | 15 +++++++- src/data/selector.ts | 9 +++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/components/ha-selector/ha-selector-constant.ts diff --git a/src/components/ha-selector/ha-selector-constant.ts b/src/components/ha-selector/ha-selector-constant.ts new file mode 100644 index 0000000000..27fcc8f7f6 --- /dev/null +++ b/src/components/ha-selector/ha-selector-constant.ts @@ -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; + } +} diff --git a/src/components/ha-selector/ha-selector.ts b/src/components/ha-selector/ha-selector.ts index b613245545..f387867025 100644 --- a/src/components/ha-selector/ha-selector.ts +++ b/src/components/ha-selector/ha-selector.ts @@ -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"), diff --git a/src/components/ha-service-control.ts b/src/components/ha-service-control.ts index 295ff0aeef..383e0a5065 100644 --- a/src/components/ha-service-control.ts +++ b/src/components/ha-service-control.ts @@ -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, diff --git a/src/data/selector.ts b/src/data/selector.ts index 3ac4661a87..12168d435e 100644 --- a/src/data/selector.ts +++ b/src/data/selector.ts @@ -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;