+
+ ${this._traces === undefined
+ ? "Loading…"
+ : this._traces.length === 0
+ ? "No traces found"
+ : this._trace === undefined
+ ? "Loading…"
+ : html`
-
- `
- : ""}
+ `}
+
`;
@@ -90,10 +119,22 @@ export class HaAutomationTrace extends LitElement {
super.updated(changedProps);
if (changedProps.has("automationId")) {
+ this._traces = undefined;
this._entityId = undefined;
+ this._runId = undefined;
this._trace = undefined;
this._logbookEntries = undefined;
- this._loadTrace();
+ if (this.automationId) {
+ this._loadTraces();
+ }
+ }
+
+ if (changedProps.has("_runId") && this._runId) {
+ this._trace = undefined;
+ this._logbookEntries = undefined;
+ if (this._runId) {
+ this._loadTrace();
+ }
}
if (
@@ -101,24 +142,44 @@ export class HaAutomationTrace extends LitElement {
this.automationId &&
!this._entityId
) {
- this._setEntityId();
+ const automation = this.automations.find(
+ (entity: AutomationEntity) => entity.attributes.id === this.automationId
+ );
+ this._entityId = automation?.entity_id;
+ }
+ }
+
+ private _pickTrace(ev) {
+ this._runId = ev.target.value;
+ }
+
+ private async _loadTraces() {
+ this._traces = await loadAutomationTraces(this.hass, this.automationId);
+ // Newest will be on top.
+ this._traces.reverse();
+
+ // Check if current run ID still exists
+ if (
+ this._runId &&
+ !this._traces.some((trace) => trace.run_id === this._runId)
+ ) {
+ await showAlertDialog(this, {
+ text: "Chosen trace is no longer available",
+ });
+ this._runId = undefined;
+ }
+
+ // See if we can set a default runID
+ if (!this._runId && this._traces.length > 0) {
+ this._runId = this._traces[0].run_id;
}
}
private async _loadTrace() {
- const traces = await loadAutomationTraces(this.hass);
- const automationTraces = traces[this.automationId];
-
- if (!automationTraces || automationTraces.length === 0) {
- // TODO no trace found.
- alert("NO traces found");
- return;
- }
-
const trace = await loadAutomationTrace(
this.hass,
this.automationId,
- automationTraces[automationTraces.length - 1].run_id
+ this._runId!
);
this._logbookEntries = await getLogbookDataForContext(
this.hass,
@@ -129,20 +190,15 @@ export class HaAutomationTrace extends LitElement {
this._trace = trace;
}
- private _setEntityId() {
- const automation = this.automations.find(
- (entity: AutomationEntity) => entity.attributes.id === this.automationId
- );
- this._entityId = automation?.entity_id;
- }
-
private _backTapped(): void {
history.back();
}
private _downloadTrace() {
const aEl = document.createElement("a");
- aEl.download = `trace-${this._entityId}.json`;
+ aEl.download = `trace ${this._entityId} ${
+ this._trace!.timestamp.start
+ }.json`;
aEl.href = `data:application/json;charset=utf-8,${encodeURI(
JSON.stringify(
{
diff --git a/src/translations/en.json b/src/translations/en.json
index 6eab53161e..c523f09742 100755
--- a/src/translations/en.json
+++ b/src/translations/en.json
@@ -1180,7 +1180,9 @@
"no_automations": "We couldn’t find any editable automations",
"add_automation": "Add automation",
"only_editable": "Only automations defined in automations.yaml are editable.",
+ "dev_only_editable": "Only automations defined in automations.yaml are debuggable.",
"edit_automation": "Edit automation",
+ "dev_automation": "Debug automation",
"show_info_automation": "Show info about automation",
"delete_automation": "Delete automation",
"delete_confirm": "Are you sure you want to delete this automation?",