mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-26 02:36:37 +00:00
Better secrets support in add-on configuration (#9275)
This commit is contained in:
parent
a9f9fc4ce2
commit
959134df02
@ -3,6 +3,7 @@ import { ActionDetail } from "@material/mwc-list";
|
|||||||
import "@material/mwc-list/mwc-list-item";
|
import "@material/mwc-list/mwc-list-item";
|
||||||
import { mdiDotsVertical } from "@mdi/js";
|
import { mdiDotsVertical } from "@mdi/js";
|
||||||
import "@polymer/iron-autogrow-textarea/iron-autogrow-textarea";
|
import "@polymer/iron-autogrow-textarea/iron-autogrow-textarea";
|
||||||
|
import { DEFAULT_SCHEMA, Type } from "js-yaml";
|
||||||
import {
|
import {
|
||||||
css,
|
css,
|
||||||
CSSResultGroup,
|
CSSResultGroup,
|
||||||
@ -11,7 +12,7 @@ import {
|
|||||||
PropertyValues,
|
PropertyValues,
|
||||||
TemplateResult,
|
TemplateResult,
|
||||||
} from "lit";
|
} from "lit";
|
||||||
import { customElement, property, state, query } from "lit/decorators";
|
import { customElement, property, query, state } from "lit/decorators";
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import { fireEvent } from "../../../../src/common/dom/fire_event";
|
import { fireEvent } from "../../../../src/common/dom/fire_event";
|
||||||
import "../../../../src/components/buttons/ha-progress-button";
|
import "../../../../src/components/buttons/ha-progress-button";
|
||||||
@ -27,6 +28,7 @@ import {
|
|||||||
HassioAddonDetails,
|
HassioAddonDetails,
|
||||||
HassioAddonSetOptionParams,
|
HassioAddonSetOptionParams,
|
||||||
setHassioAddonOption,
|
setHassioAddonOption,
|
||||||
|
validateHassioAddonOption,
|
||||||
} from "../../../../src/data/hassio/addon";
|
} from "../../../../src/data/hassio/addon";
|
||||||
import { extractApiErrorMessage } from "../../../../src/data/hassio/common";
|
import { extractApiErrorMessage } from "../../../../src/data/hassio/common";
|
||||||
import { Supervisor } from "../../../../src/data/supervisor/supervisor";
|
import { Supervisor } from "../../../../src/data/supervisor/supervisor";
|
||||||
@ -38,6 +40,13 @@ import { hassioStyle } from "../../resources/hassio-style";
|
|||||||
|
|
||||||
const SUPPORTED_UI_TYPES = ["string", "select", "boolean", "integer", "float"];
|
const SUPPORTED_UI_TYPES = ["string", "select", "boolean", "integer", "float"];
|
||||||
|
|
||||||
|
const ADDON_YAML_SCHEMA = DEFAULT_SCHEMA.extend([
|
||||||
|
new Type("!secret", {
|
||||||
|
kind: "scalar",
|
||||||
|
construct: (data) => `!secret ${data}`,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
@customElement("hassio-addon-config")
|
@customElement("hassio-addon-config")
|
||||||
class HassioAddonConfig extends LitElement {
|
class HassioAddonConfig extends LitElement {
|
||||||
@property({ attribute: false }) public addon!: HassioAddonDetails;
|
@property({ attribute: false }) public addon!: HassioAddonDetails;
|
||||||
@ -125,6 +134,7 @@ class HassioAddonConfig extends LitElement {
|
|||||||
></ha-form>`
|
></ha-form>`
|
||||||
: html` <ha-yaml-editor
|
: html` <ha-yaml-editor
|
||||||
@value-changed=${this._configChanged}
|
@value-changed=${this._configChanged}
|
||||||
|
.schema=${ADDON_YAML_SCHEMA}
|
||||||
></ha-yaml-editor>`}
|
></ha-yaml-editor>`}
|
||||||
${this._error ? html` <div class="errors">${this._error}</div> ` : ""}
|
${this._error ? html` <div class="errors">${this._error}</div> ` : ""}
|
||||||
${!this._yamlMode ||
|
${!this._yamlMode ||
|
||||||
@ -269,6 +279,14 @@ class HassioAddonConfig extends LitElement {
|
|||||||
this._error = undefined;
|
this._error = undefined;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const validation = await validateHassioAddonOption(
|
||||||
|
this.hass,
|
||||||
|
this.addon.slug,
|
||||||
|
this._editor?.value
|
||||||
|
);
|
||||||
|
if (!validation.valid) {
|
||||||
|
throw Error(validation.message);
|
||||||
|
}
|
||||||
await setHassioAddonOption(this.hass, this.addon.slug, {
|
await setHassioAddonOption(this.hass, this.addon.slug, {
|
||||||
options: this._yamlMode ? this._editor?.value : this._options,
|
options: this._yamlMode ? this._editor?.value : this._options,
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { dump, load } from "js-yaml";
|
import { DEFAULT_SCHEMA, dump, load, Schema } from "js-yaml";
|
||||||
import { html, LitElement, TemplateResult } from "lit";
|
import { html, LitElement, TemplateResult } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import { fireEvent } from "../common/dom/fire_event";
|
import { fireEvent } from "../common/dom/fire_event";
|
||||||
@ -20,6 +20,8 @@ const isEmpty = (obj: Record<string, unknown>): boolean => {
|
|||||||
export class HaYamlEditor extends LitElement {
|
export class HaYamlEditor extends LitElement {
|
||||||
@property() public value?: any;
|
@property() public value?: any;
|
||||||
|
|
||||||
|
@property({ attribute: false }) public yamlSchema: Schema = DEFAULT_SCHEMA;
|
||||||
|
|
||||||
@property() public defaultValue?: any;
|
@property() public defaultValue?: any;
|
||||||
|
|
||||||
@property() public isValid = true;
|
@property() public isValid = true;
|
||||||
@ -30,7 +32,10 @@ export class HaYamlEditor extends LitElement {
|
|||||||
|
|
||||||
public setValue(value): void {
|
public setValue(value): void {
|
||||||
try {
|
try {
|
||||||
this._yaml = value && !isEmpty(value) ? dump(value) : "";
|
this._yaml =
|
||||||
|
value && !isEmpty(value)
|
||||||
|
? dump(value, { schema: this.yamlSchema })
|
||||||
|
: "";
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error(err, value);
|
console.error(err, value);
|
||||||
@ -67,7 +72,7 @@ export class HaYamlEditor extends LitElement {
|
|||||||
|
|
||||||
if (this._yaml) {
|
if (this._yaml) {
|
||||||
try {
|
try {
|
||||||
parsed = load(this._yaml);
|
parsed = load(this._yaml, { schema: this.yamlSchema });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Invalid YAML
|
// Invalid YAML
|
||||||
isValid = false;
|
isValid = false;
|
||||||
|
@ -212,13 +212,15 @@ export const setHassioAddonOption = async (
|
|||||||
|
|
||||||
export const validateHassioAddonOption = async (
|
export const validateHassioAddonOption = async (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
slug: string
|
slug: string,
|
||||||
|
data?: any
|
||||||
): Promise<{ message: string; valid: boolean }> => {
|
): Promise<{ message: string; valid: boolean }> => {
|
||||||
if (atLeastVersion(hass.config.version, 2021, 2, 4)) {
|
if (atLeastVersion(hass.config.version, 2021, 2, 4)) {
|
||||||
return hass.callWS({
|
return hass.callWS({
|
||||||
type: "supervisor/api",
|
type: "supervisor/api",
|
||||||
endpoint: `/addons/${slug}/options/validate`,
|
endpoint: `/addons/${slug}/options/validate`,
|
||||||
method: "post",
|
method: "post",
|
||||||
|
data,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user