From 0864aeb9c649aca18cb16931dd944e6a7949b028 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 10 Jun 2020 21:21:04 +0200 Subject: [PATCH] Convert config server control to Lit (#6141) --- src/data/core.ts | 8 + .../ha-config-section-server-control.js | 267 ----------------- .../ha-config-server-control.js | 72 ----- .../ha-config-server-control.ts | 269 ++++++++++++++++++ src/translations/en.json | 7 +- 5 files changed, 283 insertions(+), 340 deletions(-) delete mode 100644 src/panels/config/server_control/ha-config-section-server-control.js delete mode 100644 src/panels/config/server_control/ha-config-server-control.js create mode 100644 src/panels/config/server_control/ha-config-server-control.ts diff --git a/src/data/core.ts b/src/data/core.ts index 646b4b5098..667a90cd61 100644 --- a/src/data/core.ts +++ b/src/data/core.ts @@ -12,6 +12,11 @@ export interface ConfigUpdateValues { internal_url?: string | null; } +export interface CheckConfigResult { + result: "valid" | "invalid"; + errors: string | null; +} + export const saveCoreConfig = ( hass: HomeAssistant, values: Partial @@ -25,3 +30,6 @@ export const detectCoreConfig = (hass: HomeAssistant) => hass.callWS>({ type: "config/core/detect", }); + +export const checkCoreConfig = (hass: HomeAssistant) => + hass.callApi("POST", "config/core/check_config"); diff --git a/src/panels/config/server_control/ha-config-section-server-control.js b/src/panels/config/server_control/ha-config-section-server-control.js deleted file mode 100644 index 97afdccba9..0000000000 --- a/src/panels/config/server_control/ha-config-section-server-control.js +++ /dev/null @@ -1,267 +0,0 @@ -import "@material/mwc-button"; -import "@polymer/paper-input/paper-input"; -import { html } from "@polymer/polymer/lib/utils/html-tag"; -/* eslint-plugin-disable lit */ -import { PolymerElement } from "@polymer/polymer/polymer-element"; -import { isComponentLoaded } from "../../../common/config/is_component_loaded"; -import "../../../components/buttons/ha-call-service-button"; -import "../../../components/ha-card"; -import LocalizeMixin from "../../../mixins/localize-mixin"; -import "../../../styles/polymer-ha-style"; -import "../ha-config-section"; - -/* - * @appliesMixin LocalizeMixin - */ -class HaConfigSectionServerControl extends LocalizeMixin(PolymerElement) { - static get template() { - return html` - - - [[localize('ui.panel.config.server_control.caption')]] - [[localize('ui.panel.config.server_control.description')]] - - - -
- [[localize('ui.panel.config.server_control.section.server_management.introduction')]] -
-
- [[localize('ui.panel.config.server_control.section.server_management.restart')]] - - [[localize('ui.panel.config.server_control.section.server_management.stop')]] - -
-
-
- `; - } - - static get properties() { - return { - hass: { - type: Object, - }, - - isWide: { - type: Boolean, - value: false, - }, - - validating: { - type: Boolean, - value: false, - }, - - isValid: { - type: Boolean, - value: null, - }, - - validateLog: { - type: String, - value: "", - }, - - showAdvanced: Boolean, - }; - } - - groupLoaded(hass) { - return isComponentLoaded(hass, "group"); - } - - automationLoaded(hass) { - return isComponentLoaded(hass, "automation"); - } - - scriptLoaded(hass) { - return isComponentLoaded(hass, "script"); - } - - sceneLoaded(hass) { - return isComponentLoaded(hass, "scene"); - } - - personLoaded(hass) { - return isComponentLoaded(hass, "person"); - } - - validateConfig() { - this.validating = true; - this.validateLog = ""; - this.isValid = null; - - this.hass.callApi("POST", "config/core/check_config").then((result) => { - this.validating = false; - this.isValid = result.result === "valid"; - - if (!this.isValid) { - this.validateLog = result.errors; - } - }); - } -} - -customElements.define( - "ha-config-section-server-control", - HaConfigSectionServerControl -); diff --git a/src/panels/config/server_control/ha-config-server-control.js b/src/panels/config/server_control/ha-config-server-control.js deleted file mode 100644 index 2cd4e8f87e..0000000000 --- a/src/panels/config/server_control/ha-config-server-control.js +++ /dev/null @@ -1,72 +0,0 @@ -import "@polymer/app-layout/app-header/app-header"; -import "@polymer/app-layout/app-toolbar/app-toolbar"; -import { html } from "@polymer/polymer/lib/utils/html-tag"; -/* eslint-plugin-disable lit */ -import { PolymerElement } from "@polymer/polymer/polymer-element"; -import "../../../layouts/hass-tabs-subpage"; -import LocalizeMixin from "../../../mixins/localize-mixin"; -import "../../../styles/polymer-ha-style"; -import { configSections } from "../ha-panel-config"; -import "./ha-config-section-server-control"; - -/* - * @appliesMixin LocalizeMixin - */ -class HaConfigServerControl extends LocalizeMixin(PolymerElement) { - static get template() { - return html` - - - -
- -
-
- `; - } - - static get properties() { - return { - hass: Object, - isWide: Boolean, - narrow: Boolean, - route: Object, - showAdvanced: Boolean, - }; - } - - _computeTabs() { - return configSections.general; - } - - computeClasses(isWide) { - return isWide ? "content" : "content narrow"; - } -} - -customElements.define("ha-config-server-control", HaConfigServerControl); diff --git a/src/panels/config/server_control/ha-config-server-control.ts b/src/panels/config/server_control/ha-config-server-control.ts new file mode 100644 index 0000000000..413c49abe7 --- /dev/null +++ b/src/panels/config/server_control/ha-config-server-control.ts @@ -0,0 +1,269 @@ +import "@polymer/app-layout/app-header/app-header"; +import "@polymer/app-layout/app-toolbar/app-toolbar"; +import "../../../layouts/hass-tabs-subpage"; +import { configSections } from "../ha-panel-config"; +import { + LitElement, + property, + customElement, + html, + css, + CSSResult, + TemplateResult, +} from "lit-element"; +import { HomeAssistant, Route } from "../../../types"; + +import "@material/mwc-button"; +import "@polymer/paper-input/paper-input"; +import { isComponentLoaded } from "../../../common/config/is_component_loaded"; +import "../../../components/buttons/ha-call-service-button"; +import "../../../components/ha-card"; +import "../ha-config-section"; +import { haStyle } from "../../../resources/styles"; +import { checkCoreConfig } from "../../../data/core"; + +const reloadableDomains = [ + "group", + "automation", + "scripts", + "scene", + "person", + "zone", + "input_boolean", + "input_text", + "input_number", + "input_datetime", + "input_select", +]; + +@customElement("ha-config-server-control") +export class HaConfigServerControl extends LitElement { + @property() public hass!: HomeAssistant; + + @property() public isWide!: boolean; + + @property() public narrow!: boolean; + + @property() public route!: Route; + + @property() public showAdvanced!: boolean; + + @property() private _validating = false; + + private _validateLog = ""; + + private _isValid: boolean | null = null; + + protected render(): TemplateResult { + return html` + + + ${this.hass.localize( + "ui.panel.config.server_control.caption" + )} + ${this.hass.localize( + "ui.panel.config.server_control.description" + )} + + ${this.showAdvanced + ? html` +
+ ${this.hass.localize( + "ui.panel.config.server_control.section.validation.introduction" + )} + ${!this._validateLog + ? html` +
+ ${!this._validating + ? html` + ${this._isValid + ? html`
+ ${this.hass.localize( + "ui.panel.config.server_control.section.validation.valid" + )} +
` + : ""} + + ${this.hass.localize( + "ui.panel.config.server_control.section.validation.check_config" + )} + + ` + : html` `} +
+ ` + : html` +
+ + ${this.hass.localize( + "ui.panel.config.server_control.section.validation.invalid" + )} + + + ${this.hass.localize( + "ui.panel.config.server_control.section.validation.check_config" + )} + +
+
+ ${this._validateLog} +
+ `} +
+
` + : ""} + + +
+ ${this.hass.localize( + "ui.panel.config.server_control.section.server_management.introduction" + )} +
+
+ ${this.hass.localize( + "ui.panel.config.server_control.section.server_management.restart" + )} + + ${this.hass.localize( + "ui.panel.config.server_control.section.server_management.stop" + )} + +
+
+ + ${this.showAdvanced + ? html` + +
+ ${this.hass.localize( + "ui.panel.config.server_control.section.reloading.introduction" + )} +
+
+ ${this.hass.localize( + "ui.panel.config.server_control.section.reloading.core" + )} + +
+ ${reloadableDomains.map((domain) => + isComponentLoaded(this.hass, domain) + ? html`
+ ${this.hass.localize( + `ui.panel.config.server_control.section.reloading.${domain}` + )} + +
` + : "" + )} +
+ ` + : ""} +
+
+ `; + } + + private async _validateConfig() { + this._validating = true; + this._validateLog = ""; + this._isValid = null; + + const configCheck = await checkCoreConfig(this.hass); + this._validating = false; + this._isValid = configCheck.result === "valid"; + + if (configCheck.errors) { + this._validateLog = configCheck.errors; + } + } + + static get styles(): CSSResult[] { + return [ + haStyle, + css` + .validate-container { + height: 140px; + } + + .validate-result { + color: var(--google-green-500); + font-weight: 500; + margin-bottom: 1em; + } + + .config-invalid { + margin: 1em 0; + } + + .config-invalid .text { + color: var(--google-red-500); + font-weight: 500; + } + + .config-invalid mwc-button { + float: right; + } + + .validate-log { + white-space: pre-wrap; + direction: ltr; + } + `, + ]; + } +} diff --git a/src/translations/en.json b/src/translations/en.json index 251c4f1330..a57584da22 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -737,7 +737,12 @@ "script": "Reload scripts", "scene": "Reload scenes", "person": "Reload persons", - "zone": "Reload zones" + "zone": "Reload zones", + "input_boolean": "Reload input booleans", + "input_text": "Reload input texts", + "input_number": "Reload input numbers", + "input_datetime": "Reload input date times", + "input_select": "Reload input selects" }, "server_management": { "heading": "Server management",