Move condition test button into overflow menu (#13478)

This commit is contained in:
Franck Nijhof 2022-08-25 20:57:29 +02:00 committed by GitHub
parent 98352ae7b7
commit ad58d16dfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 30 deletions

View File

@ -5,6 +5,7 @@ import {
mdiContentDuplicate, mdiContentDuplicate,
mdiDelete, mdiDelete,
mdiDotsVertical, mdiDotsVertical,
mdiFlask,
mdiPlayCircleOutline, mdiPlayCircleOutline,
mdiRenameBox, mdiRenameBox,
mdiStopCircleOutline, mdiStopCircleOutline,
@ -15,8 +16,6 @@ import { classMap } from "lit/directives/class-map";
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import { capitalizeFirstLetter } from "../../../../common/string/capitalize-first-letter"; import { capitalizeFirstLetter } from "../../../../common/string/capitalize-first-letter";
import { handleStructError } from "../../../../common/structs/handle-errors"; import { handleStructError } from "../../../../common/structs/handle-errors";
import "../../../../components/buttons/ha-progress-button";
import type { HaProgressButton } from "../../../../components/buttons/ha-progress-button";
import "../../../../components/ha-button-menu"; import "../../../../components/ha-button-menu";
import "../../../../components/ha-card"; import "../../../../components/ha-card";
import "../../../../components/ha-expansion-panel"; import "../../../../components/ha-expansion-panel";
@ -75,6 +74,10 @@ export default class HaAutomationConditionRow extends LitElement {
@state() private _warnings?: string[]; @state() private _warnings?: string[];
@state() private _testing = false;
@state() private _testingResult?: boolean;
protected render() { protected render() {
if (!this.condition) { if (!this.condition) {
return html``; return html``;
@ -100,11 +103,6 @@ export default class HaAutomationConditionRow extends LitElement {
)} )}
</div> </div>
<ha-progress-button slot="icons" @click=${this._testCondition}>
${this.hass.localize(
"ui.panel.config.automation.editor.conditions.test"
)}
</ha-progress-button>
<ha-button-menu <ha-button-menu
slot="icons" slot="icons"
fixed fixed
@ -119,6 +117,12 @@ export default class HaAutomationConditionRow extends LitElement {
> >
</ha-icon-button> </ha-icon-button>
<mwc-list-item graphic="icon">
${this.hass.localize(
"ui.panel.config.automation.editor.conditions.test"
)}
<ha-svg-icon slot="graphic" .path=${mdiFlask}></ha-svg-icon>
</mwc-list-item>
<mwc-list-item graphic="icon"> <mwc-list-item graphic="icon">
${this.hass.localize( ${this.hass.localize(
"ui.panel.config.automation.editor.conditions.rename" "ui.panel.config.automation.editor.conditions.rename"
@ -223,6 +227,21 @@ export default class HaAutomationConditionRow extends LitElement {
></ha-automation-condition-editor> ></ha-automation-condition-editor>
</div> </div>
</ha-expansion-panel> </ha-expansion-panel>
<div
class="testing ${classMap({
active: this._testing,
pass: this._testingResult === true,
error: this._testingResult === false,
})}"
>
${this._testingResult
? this.hass.localize(
"ui.panel.config.automation.editor.conditions.testing_pass"
)
: this.hass.localize(
"ui.panel.config.automation.editor.conditions.testing_error"
)}
</div>
</ha-card> </ha-card>
`; `;
} }
@ -245,23 +264,26 @@ export default class HaAutomationConditionRow extends LitElement {
private async _handleAction(ev: CustomEvent<ActionDetail>) { private async _handleAction(ev: CustomEvent<ActionDetail>) {
switch (ev.detail.index) { switch (ev.detail.index) {
case 0: case 0:
await this._renameCondition(); await this._testCondition();
break; break;
case 1: case 1:
fireEvent(this, "duplicate"); await this._renameCondition();
break; break;
case 2: case 2:
fireEvent(this, "duplicate");
break;
case 3:
this._switchUiMode(); this._switchUiMode();
this.expand(); this.expand();
break; break;
case 3: case 4:
this._switchYamlMode(); this._switchYamlMode();
this.expand(); this.expand();
break; break;
case 4: case 5:
this._onDisable(); this._onDisable();
break; break;
case 5: case 6:
this._onDelete(); this._onDelete();
break; break;
} }
@ -296,14 +318,13 @@ export default class HaAutomationConditionRow extends LitElement {
this._yamlMode = true; this._yamlMode = true;
} }
private async _testCondition(ev) { private async _testCondition() {
ev.preventDefault(); if (this._testing) {
const condition = this.condition;
const button = ev.target as HaProgressButton;
if (button.progress) {
return; return;
} }
button.progress = true; this._testingResult = undefined;
this._testing = true;
const condition = this.condition;
try { try {
const validateResult = await validateConfig(this.hass, { const validateResult = await validateConfig(this.hass, {
@ -312,6 +333,7 @@ export default class HaAutomationConditionRow extends LitElement {
// Abort if condition changed. // Abort if condition changed.
if (this.condition !== condition) { if (this.condition !== condition) {
this._testing = false;
return; return;
} }
@ -322,13 +344,16 @@ export default class HaAutomationConditionRow extends LitElement {
), ),
text: validateResult.condition.error, text: validateResult.condition.error,
}); });
this._testing = false;
return; return;
} }
let result: { result: boolean }; let result: { result: boolean };
try { try {
result = await testCondition(this.hass, condition); result = await testCondition(this.hass, condition);
} catch (err: any) { } catch (err: any) {
if (this.condition !== condition) { if (this.condition !== condition) {
this._testing = false;
return; return;
} }
@ -338,20 +363,15 @@ export default class HaAutomationConditionRow extends LitElement {
), ),
text: err.message, text: err.message,
}); });
this._testing = false;
return; return;
} }
if (this.condition !== condition) { this._testingResult = result.result;
return;
}
if (result.result) {
button.actionSuccess();
} else {
button.actionError();
}
} finally { } finally {
button.progress = false; setTimeout(() => {
this._testing = false;
}, 2500);
} }
} }
@ -392,8 +412,7 @@ export default class HaAutomationConditionRow extends LitElement {
return [ return [
haStyle, haStyle,
css` css`
ha-button-menu, ha-button-menu {
ha-progress-button {
--mdc-theme-text-primary-on-background: var(--primary-text-color); --mdc-theme-text-primary-on-background: var(--primary-text-color);
} }
.disabled { .disabled {
@ -420,6 +439,32 @@ export default class HaAutomationConditionRow extends LitElement {
mwc-list-item[disabled] { mwc-list-item[disabled] {
--mdc-theme-text-primary-on-background: var(--disabled-text-color); --mdc-theme-text-primary-on-background: var(--disabled-text-color);
} }
.testing {
position: absolute;
top: 0px;
right: 0px;
left: 0px;
text-transform: uppercase;
font-weight: bold;
font-size: 14px;
background-color: var(--divider-color, #e0e0e0);
color: var(--text-primary-color);
max-height: 0px;
overflow: hidden;
transition: max-height 0.3s;
text-align: center;
border-top-right-radius: var(--ha-card-border-radius, 4px);
border-top-left-radius: var(--ha-card-border-radius, 4px);
}
.testing.active {
max-height: 100px;
}
.testing.error {
background-color: var(--accent-color);
}
.testing.pass {
background-color: var(--success-color);
}
`, `,
]; ];
} }

View File

@ -1975,6 +1975,8 @@
"learn_more": "Learn more about conditions", "learn_more": "Learn more about conditions",
"add": "Add condition", "add": "Add condition",
"test": "Test", "test": "Test",
"testing_error": "Condition did not pass",
"testing_pass": "Condition passes",
"invalid_condition": "Invalid condition configuration", "invalid_condition": "Invalid condition configuration",
"test_failed": "Error occurred while testing condition", "test_failed": "Error occurred while testing condition",
"duplicate": "[%key:ui::panel::config::automation::editor::triggers::duplicate%]", "duplicate": "[%key:ui::panel::config::automation::editor::triggers::duplicate%]",