Skip to content

Commit

Permalink
Add support for warnings in check_config (#18489)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten authored Nov 16, 2023
1 parent 98cdbb4 commit f5edee1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 54 deletions.
1 change: 1 addition & 0 deletions src/data/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface ConfigUpdateValues {
export interface CheckConfigResult {
result: "valid" | "invalid";
errors: string | null;
warnings: string | null;
}

export const saveCoreConfig = (
Expand Down
124 changes: 71 additions & 53 deletions src/panels/developer-tools/yaml_configuration/developer-yaml-config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import "@material/mwc-button";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import {
css,
CSSResultGroup,
html,
LitElement,
nothing,
TemplateResult,
} from "lit";
import { customElement, property, state } from "lit/decorators";
import { componentsWithService } from "../../../common/config/components_with_service";
import "../../../components/buttons/ha-call-service-button";
import "../../../components/ha-card";
import "../../../components/ha-circular-progress";
import { checkCoreConfig } from "../../../data/core";
import { CheckConfigResult, checkCoreConfig } from "../../../data/core";
import { domainToName } from "../../../data/integration";
import { showRestartDialog } from "../../../dialogs/restart/show-dialog-restart";
import { haStyle } from "../../../resources/styles";
Expand All @@ -32,14 +39,11 @@ export class DeveloperYamlConfig extends LitElement {

@state() private _reloadableDomains: ReloadableDomain[] = [];

@state() private _isValid: boolean | null = null;

private _validateLog = "";
@state() private _validateResult?: CheckConfigResult;

public disconnectedCallback() {
super.disconnectedCallback();
this._isValid = null;
this._validateLog = "";
this._validateResult = undefined;
}

protected updated(changedProperties) {
Expand Down Expand Up @@ -68,34 +72,57 @@ export class DeveloperYamlConfig extends LitElement {
${this.hass.localize(
"ui.panel.developer-tools.tabs.yaml.section.validation.introduction"
)}
${!this._validateLog
? html`
<div class="validate-container layout vertical center-center">
${!this._validating
? html`
${this._isValid
? html`<div class="validate-result" id="result">
${this.hass.localize(
"ui.panel.developer-tools.tabs.yaml.section.validation.valid"
)}
</div>`
: ""}
`
: html`
<ha-circular-progress active></ha-circular-progress>
`}
</div>
`
${!this._validateResult
? this._validating
? html`<div
class="validate-container layout vertical center-center"
>
<ha-circular-progress active></ha-circular-progress>
</div> `
: nothing
: html`
<div class="config-invalid">
<span class="text">
${this.hass.localize(
"ui.panel.developer-tools.tabs.yaml.section.validation.invalid"
)}
</span>
</div>
<div id="configLog" class="validate-log">
${this._validateLog}
<div class="validate-result ${
this._validateResult.result === "invalid" ? "invalid" : ""
}">
${
this._validateResult.result === "valid"
? this.hass.localize(
"ui.panel.developer-tools.tabs.yaml.section.validation.valid"
)
: this.hass.localize(
"ui.panel.developer-tools.tabs.yaml.section.validation.invalid"
)
}
</div>
${
this._validateResult.errors
? html`<ha-alert
alert-type="error"
.title=${this.hass.localize(
"ui.panel.developer-tools.tabs.yaml.section.validation.errors"
)}
>
<span class="validate-log"
>${this._validateResult.errors}</span
>
</ha-alert>`
: ""
}
${
this._validateResult.warnings
? html`<ha-alert
alert-type="warning"
.title=${this.hass.localize(
"ui.panel.developer-tools.tabs.yaml.section.validation.warnings"
)}
>
<span class="validate-log"
>${this._validateResult.warnings}</span
>
</ha-alert>`
: ""
}
</div>
`}
</div>
Expand All @@ -108,7 +135,7 @@ export class DeveloperYamlConfig extends LitElement {
<mwc-button
class="warning"
@click=${this._restart}
.disabled=${!!this._validateLog}
.disabled=${this._validateResult?.result === "invalid"}
>
${this.hass.localize(
"ui.panel.developer-tools.tabs.yaml.section.server_management.restart"
Expand Down Expand Up @@ -173,22 +200,17 @@ export class DeveloperYamlConfig extends LitElement {

private async _validateConfig() {
this._validating = true;
this._validateLog = "";
this._isValid = null;
this._validateResult = undefined;

const configCheck = await checkCoreConfig(this.hass);
this._validateResult = await checkCoreConfig(this.hass);
this._validating = false;
if (!this.isConnected) {
return;
}
this._isValid = configCheck.result === "valid";

if (configCheck.errors) {
this._validateLog = configCheck.errors;
}
}

private _restart() {
private async _restart() {
await this._validateConfig();
if (this._validateResult?.result === "invalid") {
return;
}
showRestartDialog(this);
}

Expand All @@ -203,20 +225,16 @@ export class DeveloperYamlConfig extends LitElement {
.validate-result {
color: var(--success-color);
font-weight: 500;
}
.config-invalid {
margin: 1em 0;
text-align: center;
}
.config-invalid .text {
.validate-result.invalid {
color: var(--error-color);
font-weight: 500;
}
.validate-log {
white-space: pre-line;
white-space: pre;
direction: ltr;
}
Expand Down
4 changes: 3 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5844,7 +5844,9 @@
"introduction": "A basic validation of the configuration is automatically done before restarting. The basic validation ensures the YAML configuration doesn't have errors which will prevent Home Assistant or any integration from starting. It's also possible to only do the basic validation check without restarting.",
"check_config": "Check configuration",
"valid": "Configuration will not prevent Home Assistant from starting!",
"invalid": "Configuration invalid!"
"invalid": "Configuration invalid!",
"warnings": "Configuration warnings",
"errors": "Configuration errors"
},
"reloading": {
"all": "All YAML configuration",
Expand Down

0 comments on commit f5edee1

Please sign in to comment.