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,49 +32,61 @@ class HaPanelDevEvent extends LitElement {
: "content layout horizontal"} : "content layout horizontal"}
> >
<div class="flex"> <div class="flex">
<p> <ha-card>
${this.hass.localize( <div class="card-content">
"ui.panel.developer-tools.tabs.events.description" <p>
)} ${this.hass.localize(
<a "ui.panel.developer-tools.tabs.events.description"
href=${documentationUrl(this.hass, "/docs/configuration/events/")} )}
target="_blank" <a
rel="noreferrer" href=${documentationUrl(
> this.hass,
${this.hass.localize( "/docs/configuration/events/"
"ui.panel.developer-tools.tabs.events.documentation" )}
)} target="_blank"
</a> rel="noreferrer"
</p> >
<div class="inputs"> ${this.hass.localize(
<ha-textfield "ui.panel.developer-tools.tabs.events.documentation"
.label=${this.hass.localize( )}
"ui.panel.developer-tools.tabs.events.type" </a>
)} </p>
autofocus <div class="inputs">
required <ha-textfield
.value=${this._eventType} .label=${this.hass.localize(
@change=${this._eventTypeChanged} "ui.panel.developer-tools.tabs.events.type"
></ha-textfield> )}
<p> autofocus
${this.hass.localize("ui.panel.developer-tools.tabs.events.data")} required
</p> .value=${this._eventType}
</div> @change=${this._eventTypeChanged}
<div class="code-editor"> ></ha-textfield>
<ha-yaml-editor <p>
.value=${this._eventData} ${this.hass.localize(
.error=${!this._isValid} "ui.panel.developer-tools.tabs.events.data"
@value-changed=${this._yamlChanged} )}
></ha-yaml-editor> </p>
</div> </div>
<mwc-button <div class="code-editor">
@click=${this._fireEvent} <ha-yaml-editor
raised .value=${this._eventData}
.disabled=${!this._isValid} .error=${!this._isValid}
>${this.hass.localize( @value-changed=${this._yamlChanged}
"ui.panel.developer-tools.tabs.events.fire_event" ></ha-yaml-editor>
)}</mwc-button </div>
> </div>
<div class="card-actions">
<ha-button
@click=${this._fireEvent}
raised
.disabled=${!this._isValid}
>${this.hass.localize(
"ui.panel.developer-tools.tabs.events.fire_event"
)}</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,33 +40,46 @@ 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( "ui.panel.developer-tools.tabs.events.listening_to"
"ui.panel.developer-tools.tabs.events.listening_to" )
) : this.hass!.localize(
: this.hass!.localize( "ui.panel.developer-tools.tabs.events.subscribe_to"
"ui.panel.developer-tools.tabs.events.subscribe_to" )}
)} .disabled=${this._subscribed !== undefined}
.disabled=${this._subscribed !== undefined} .value=${this._eventType}
.value=${this._eventType} @input=${this._valueChanged}
@input=${this._valueChanged} ></ha-textfield>
></ha-textfield> </div>
<mwc-button <div class="card-actions">
.disabled=${this._eventType === ""} <ha-button
@click=${this._handleSubmit} raised
type="submit" .disabled=${this._eventType === ""}
> @click=${this._startOrStopListening}
${this._subscribed >
? this.hass!.localize( ${this._subscribed
"ui.panel.developer-tools.tabs.events.stop_listening" ? this.hass!.localize(
) "ui.panel.developer-tools.tabs.events.stop_listening"
: this.hass!.localize( )
"ui.panel.developer-tools.tabs.events.start_listening" : this.hass!.localize(
)} "ui.panel.developer-tools.tabs.events.start_listening"
</mwc-button> )}
</form> </ha-button>
<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!"
}, },