Use selectors for add-on configurations (#12234)

This commit is contained in:
Joakim Sørensen 2022-04-06 09:57:17 +02:00 committed by GitHub
parent a1a6a2cd30
commit 351ec08a71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 21 deletions

View File

@ -39,7 +39,14 @@ import type { HomeAssistant } from "../../../../src/types";
import { suggestAddonRestart } from "../../dialogs/suggestAddonRestart"; import { suggestAddonRestart } from "../../dialogs/suggestAddonRestart";
import { hassioStyle } from "../../resources/hassio-style"; import { hassioStyle } from "../../resources/hassio-style";
const SUPPORTED_UI_TYPES = ["string", "select", "boolean", "integer", "float"]; const SUPPORTED_UI_TYPES = [
"string",
"select",
"boolean",
"integer",
"float",
"schema",
];
const ADDON_YAML_SCHEMA = DEFAULT_SCHEMA.extend([ const ADDON_YAML_SCHEMA = DEFAULT_SCHEMA.extend([
new Type("!secret", { new Type("!secret", {
@ -78,16 +85,56 @@ class HassioAddonConfig extends LitElement {
this.addon.translations.en?.configuration?.[entry.name].name || this.addon.translations.en?.configuration?.[entry.name].name ||
entry.name; entry.name;
private _schema = memoizeOne((schema: HaFormSchema[]): HaFormSchema[] => public computeHelper = (entry: HaFormSchema): string =>
// @ts-expect-error supervisor does not implement [string, string] for select.options[] this.addon.translations[this.hass.language]?.configuration?.[entry.name]
schema.map((entry) => ?.description ||
entry.type === "select" this.addon.translations.en?.configuration?.[entry.name].description ||
? { "";
...entry,
options: entry.options.map((option) => [option, option]), private _convertSchema = memoizeOne(
} // Convert supervisor schema to selectors
: entry (schema: Record<string, any>): HaFormSchema[] =>
) schema.map((entry) =>
entry.type === "select"
? {
name: entry.name,
required: entry.required,
selector: { select: { options: entry.options } },
}
: entry.type === "string"
? entry.multiple
? {
name: entry.name,
required: entry.required,
selector: {
select: { options: [], multiple: true, custom_value: true },
},
}
: {
name: entry.name,
required: entry.required,
selector: { text: { type: "text" } },
}
: entry.type === "boolean"
? {
name: entry.name,
required: entry.required,
selector: { boolean: {} },
}
: entry.type === "schema"
? {
name: entry.name,
required: entry.required,
selector: { object: {} },
}
: entry.type === "float" || entry.type === "integer"
? {
name: entry.name,
required: entry.required,
selector: { number: { mode: "box" } },
}
: entry
)
); );
private _filteredShchema = memoizeOne( private _filteredShchema = memoizeOne(
@ -140,7 +187,8 @@ class HassioAddonConfig extends LitElement {
.data=${this._options!} .data=${this._options!}
@value-changed=${this._configChanged} @value-changed=${this._configChanged}
.computeLabel=${this.computeLabel} .computeLabel=${this.computeLabel}
.schema=${this._schema( .computeHelper=${this.computeHelper}
.schema=${this._convertSchema(
this._showOptional this._showOptional
? this.addon.schema! ? this.addon.schema!
: this._filteredShchema( : this._filteredShchema(
@ -197,8 +245,9 @@ class HassioAddonConfig extends LitElement {
protected firstUpdated(changedProps) { protected firstUpdated(changedProps) {
super.firstUpdated(changedProps); super.firstUpdated(changedProps);
this._canShowSchema = !this.addon.schema!.find( this._canShowSchema = !this.addon.schema!.find(
// @ts-ignore (entry) =>
(entry) => !SUPPORTED_UI_TYPES.includes(entry.type) || entry.multiple // @ts-ignore
!SUPPORTED_UI_TYPES.includes(entry.type)
); );
this._yamlMode = !this._canShowSchema; this._yamlMode = !this._canShowSchema;
} }

View File

@ -3,6 +3,7 @@ import { customElement, property } from "lit/decorators";
import { fireEvent } from "../../common/dom/fire_event"; import { fireEvent } from "../../common/dom/fire_event";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import "../ha-yaml-editor"; import "../ha-yaml-editor";
import "../ha-input-helper-text";
@customElement("ha-selector-object") @customElement("ha-selector-object")
export class HaObjectSelector extends LitElement { export class HaObjectSelector extends LitElement {
@ -12,6 +13,8 @@ export class HaObjectSelector extends LitElement {
@property() public label?: string; @property() public label?: string;
@property() public helper?: string;
@property() public placeholder?: string; @property() public placeholder?: string;
@property({ type: Boolean }) public disabled = false; @property({ type: Boolean }) public disabled = false;
@ -20,13 +23,17 @@ export class HaObjectSelector extends LitElement {
protected render() { protected render() {
return html`<ha-yaml-editor return html`<ha-yaml-editor
.hass=${this.hass} .hass=${this.hass}
.readonly=${this.disabled} .readonly=${this.disabled}
.required=${this.required} .label=${this.label}
.placeholder=${this.placeholder} .required=${this.required}
.defaultValue=${this.value} .placeholder=${this.placeholder}
@value-changed=${this._handleChange} .defaultValue=${this.value}
></ha-yaml-editor>`; @value-changed=${this._handleChange}
></ha-yaml-editor>
${this.helper
? html`<ha-input-helper-text>${this.helper}</ha-input-helper-text>`
: ""} `;
} }
private _handleChange(ev) { private _handleChange(ev) {