Dev tools events: Add a clear events button (#21353)

Clear events
This commit is contained in:
Simon Lamon 2024-07-10 15:01:56 +02:00 committed by GitHub
parent cd4937b539
commit 11b490d145
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 108 additions and 73 deletions

View File

@ -1,8 +1,9 @@
import { CSSResultGroup, LitElement, TemplateResult, css, html } from "lit"; import { CSSResultGroup, LitElement, TemplateResult, css, html } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import "@material/mwc-button";
import "../../../components/ha-yaml-editor"; import "../../../components/ha-yaml-editor";
import "../../../components/ha-textfield"; import "../../../components/ha-textfield";
import "../../../components/ha-button";
import "../../../components/ha-card";
import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box"; import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box";
import { documentationUrl } from "../../../util/documentation-url"; import { documentationUrl } from "../../../util/documentation-url";
import "./event-subscribe-card"; import "./event-subscribe-card";
@ -31,12 +32,17 @@ class HaPanelDevEvent extends LitElement {
: "content layout horizontal"} : "content layout horizontal"}
> >
<div class="flex"> <div class="flex">
<ha-card>
<div class="card-content">
<p> <p>
${this.hass.localize( ${this.hass.localize(
"ui.panel.developer-tools.tabs.events.description" "ui.panel.developer-tools.tabs.events.description"
)} )}
<a <a
href=${documentationUrl(this.hass, "/docs/configuration/events/")} href=${documentationUrl(
this.hass,
"/docs/configuration/events/"
)}
target="_blank" target="_blank"
rel="noreferrer" rel="noreferrer"
> >
@ -56,7 +62,9 @@ class HaPanelDevEvent extends LitElement {
@change=${this._eventTypeChanged} @change=${this._eventTypeChanged}
></ha-textfield> ></ha-textfield>
<p> <p>
${this.hass.localize("ui.panel.developer-tools.tabs.events.data")} ${this.hass.localize(
"ui.panel.developer-tools.tabs.events.data"
)}
</p> </p>
</div> </div>
<div class="code-editor"> <div class="code-editor">
@ -66,14 +74,19 @@ class HaPanelDevEvent extends LitElement {
@value-changed=${this._yamlChanged} @value-changed=${this._yamlChanged}
></ha-yaml-editor> ></ha-yaml-editor>
</div> </div>
<mwc-button </div>
<div class="card-actions">
<ha-button
@click=${this._fireEvent} @click=${this._fireEvent}
raised raised
.disabled=${!this._isValid} .disabled=${!this._isValid}
>${this.hass.localize( >${this.hass.localize(
"ui.panel.developer-tools.tabs.events.fire_event" "ui.panel.developer-tools.tabs.events.fire_event"
)}</mwc-button )}</ha-button
> >
</div>
</ha-card>
<event-subscribe-card .hass=${this.hass}></event-subscribe-card> <event-subscribe-card .hass=${this.hass}></event-subscribe-card>
</div> </div>

View File

@ -1,4 +1,3 @@
import "@material/mwc-button";
import { HassEvent } from "home-assistant-js-websocket"; import { HassEvent } from "home-assistant-js-websocket";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit"; import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
@ -7,6 +6,7 @@ import { formatTime } from "../../../common/datetime/format_time";
import "../../../components/ha-card"; import "../../../components/ha-card";
import "../../../components/ha-textfield"; import "../../../components/ha-textfield";
import "../../../components/ha-yaml-editor"; import "../../../components/ha-yaml-editor";
import "../../../components/ha-button";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
@customElement("event-subscribe-card") @customElement("event-subscribe-card")
@ -40,7 +40,6 @@ class EventSubscribeCard extends LitElement {
)} )}
> >
<div class="card-content"> <div class="card-content">
<form>
<ha-textfield <ha-textfield
.label=${this._subscribed .label=${this._subscribed
? this.hass!.localize( ? this.hass!.localize(
@ -53,10 +52,12 @@ class EventSubscribeCard extends LitElement {
.value=${this._eventType} .value=${this._eventType}
@input=${this._valueChanged} @input=${this._valueChanged}
></ha-textfield> ></ha-textfield>
<mwc-button </div>
<div class="card-actions">
<ha-button
raised
.disabled=${this._eventType === ""} .disabled=${this._eventType === ""}
@click=${this._handleSubmit} @click=${this._startOrStopListening}
type="submit"
> >
${this._subscribed ${this._subscribed
? this.hass!.localize( ? this.hass!.localize(
@ -65,8 +66,20 @@ class EventSubscribeCard extends LitElement {
: this.hass!.localize( : this.hass!.localize(
"ui.panel.developer-tools.tabs.events.start_listening" "ui.panel.developer-tools.tabs.events.start_listening"
)} )}
</mwc-button> </ha-button>
</form> <ha-button
raised
.disabled=${this._eventType === ""}
@click=${this._clearEvents}
>
${this.hass!.localize(
"ui.panel.developer-tools.tabs.events.clear_events"
)}
</ha-button>
</div>
</ha-card>
<ha-card>
<div class="card-content">
<div class="events"> <div class="events">
${repeat( ${repeat(
this._events, this._events,
@ -99,7 +112,7 @@ class EventSubscribeCard extends LitElement {
this._eventType = ev.target.value; this._eventType = ev.target.value;
} }
private async _handleSubmit(): Promise<void> { private async _startOrStopListening(): Promise<void> {
if (this._subscribed) { if (this._subscribed) {
this._subscribed(); this._subscribed();
this._subscribed = undefined; this._subscribed = undefined;
@ -121,6 +134,11 @@ class EventSubscribeCard extends LitElement {
} }
} }
private _clearEvents(): void {
this._events = [];
this._eventCount = 0;
}
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
return css` return css`
ha-textfield { ha-textfield {
@ -140,6 +158,9 @@ class EventSubscribeCard extends LitElement {
pre { pre {
font-family: var(--code-font-family, monospace); font-family: var(--code-font-family, monospace);
} }
ha-card {
margin-bottom: 5px;
}
`; `;
} }
} }

View File

@ -6686,6 +6686,7 @@
"subscribe_to": "Event to subscribe to", "subscribe_to": "Event to subscribe to",
"start_listening": "Start listening", "start_listening": "Start listening",
"stop_listening": "Stop listening", "stop_listening": "Stop listening",
"clear_events": "Clear events",
"alert_event_type": "Event type is a mandatory field", "alert_event_type": "Event type is a mandatory field",
"notification_event_fired": "Event {type} successfully fired!" "notification_event_fired": "Event {type} successfully fired!"
}, },