Compare commits

...
3 changed files with 229 additions and 98 deletions

No files matched your search

@@ -1,4 +1,5 @@
import {
mdiContentCopy,
mdiRestore,
mdiTrashCanOutline,
mdiViewSplitHorizontal,
@@ -12,24 +13,29 @@ import { customElement, property, state } from "lit/decorators";
import type { HASSDomTargetEvent } from "../../../../common/dom/fire_event";
import { stopPropagation } from "../../../../common/dom/stop_propagation";
import type { LocalizeKeys } from "../../../../common/translations/localize";
import { copyToClipboard } from "../../../../common/util/copy-clipboard";
import { debounce } from "../../../../common/util/debounce";
import "../../../../components/ha-alert";
import "../../../../components/ha-card";
import "../../../../components/ha-code-editor";
import "../../../../components/ha-expansion-panel";
import "../../../../components/ha-icon-button";
import type { HaIconButtonToolbarItem } from "../../../../components/ha-icon-button-toolbar";
import "../../../../components/ha-label";
import "../../../../components/ha-spinner";
import "../../../../components/ha-split-panel";
import type { HaSplitPanel } from "../../../../components/ha-split-panel";
import "../../../../components/ha-svg-icon";
import "../../../../components/ha-tip";
import type { RenderTemplateResult } from "../../../../data/ws-templates";
import type {
RenderTemplateResult,
TemplateListeners,
} from "../../../../data/ws-templates";
import { subscribeRenderTemplate } from "../../../../data/ws-templates";
import { showConfirmationDialog } from "../../../../dialogs/generic/show-dialog-box";
import { haStyle, haStyleScrollbar } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
import { documentationUrl } from "../../../../util/documentation-url";
import { showToast } from "../../../../util/toast";
const DEMO_TEMPLATE = `{## Imitate available variables: ##}
{% set my_test_json = {
@@ -138,7 +144,7 @@ class HaPanelDevTemplate extends LitElement {
: type;
const editorCard = this._renderEditorCard();
const resultCard = this._renderResultCard(type, resultType);
const resultCard = this._renderResultCard(resultType);
// On narrow viewports side-by-side is too cramped, so force the (still
// resizable) stacked layout and hide the orientation toggle.
@@ -300,7 +306,7 @@ class HaPanelDevTemplate extends LitElement {
`;
}
private _renderResultCard(type: string, resultType: string) {
private _renderResultCard(resultType: string) {
const showEmptyState =
!this._error && !this._rendering && !this._template?.trim();
@@ -337,85 +343,26 @@ class HaPanelDevTemplate extends LitElement {
</div>`
: this._templateResult
? html`
<ha-label dense>
${this.hass.localize(
"ui.panel.config.tools.tabs.templates.result_type"
)}:
${resultType}
</ha-label>
<pre class="rendered">
${
type === "object"
? JSON.stringify(this._templateResult.result, null, 2)
: this._templateResult.result
}</pre>
${
this._templateResult.listeners.time
? html`
<p>
${this.hass.localize(
"ui.panel.config.tools.tabs.templates.time"
)}
</p>
`
: ""
}
${
!this._templateResult.listeners
? nothing
: this._templateResult.listeners.all
? html`
<p class="all_listeners">
${this.hass.localize(
"ui.panel.config.tools.tabs.templates.all_listeners"
)}
</p>
`
: this._templateResult.listeners.domains.length ||
this._templateResult.listeners.entities.length
? html`
<p>
${this.hass.localize(
"ui.panel.config.tools.tabs.templates.listeners"
)}
</p>
<ul>
${this._templateResult.listeners.domains
.sort()
.map(
(domain) => html`
<li>
<b
>${this.hass.localize(
"ui.panel.config.tools.tabs.templates.domain"
)}</b
>: ${domain}
</li>
`
)}
${this._templateResult.listeners.entities
.sort()
.map(
(entity_id) => html`
<li>
<b
>${this.hass.localize(
"ui.panel.config.tools.tabs.templates.entity"
)}</b
>: ${entity_id}
</li>
`
)}
</ul>
`
: !this._templateResult.listeners.time
? html`<span class="all_listeners">
${this.hass.localize(
"ui.panel.config.tools.tabs.templates.no_listeners"
)}
</span>`
: nothing
}
<div class="result-output">
<div class="result-header">
<span class="result-type">
${this.hass.localize(
"ui.panel.config.tools.tabs.templates.result_type"
)}:
${resultType}
</span>
<ha-icon-button
.path=${mdiContentCopy}
.label=${this.hass.localize(
"ui.panel.config.tools.tabs.templates.copy_result"
)}
@click=${this._copyResult}
></ha-icon-button>
</div>
<!-- prettier-ignore -->
<pre class="rendered">${this._resultText()}</pre>
</div>
${this._renderListeners(this._templateResult.listeners)}
`
: nothing
}
@@ -424,6 +371,87 @@ ${
`;
}
private _renderListeners(listeners?: TemplateListeners) {
if (!listeners) {
return nothing;
}
const watched = [
{
key: "ui.panel.config.tools.tabs.templates.domain" as const,
values: [...listeners.domains].sort(),
},
{
key: "ui.panel.config.tools.tabs.templates.entity" as const,
values: [...listeners.entities].sort(),
},
].filter((group) => group.values.length);
return html`
<div class="listeners">
${
listeners.time
? html`<p>
${this.hass.localize(
"ui.panel.config.tools.tabs.templates.time"
)}
</p>`
: nothing
}
${
listeners.all
? html`<ha-alert alert-type="warning">
${this.hass.localize(
"ui.panel.config.tools.tabs.templates.all_listeners"
)}
</ha-alert>`
: watched.length
? html`
<p>
${this.hass.localize(
"ui.panel.config.tools.tabs.templates.listeners"
)}
</p>
<dl class="listener-list">
${watched.map(
(group) => html`
<dt>${this.hass.localize(group.key)}</dt>
<dd>
${group.values.map(
(value) => html`<code>${value}</code>`
)}
</dd>
`
)}
</dl>
`
: listeners.time
? nothing
: html`<ha-alert alert-type="info">
${this.hass.localize(
"ui.panel.config.tools.tabs.templates.no_listeners"
)}
</ha-alert>`
}
</div>
`;
}
private _resultText() {
const result: unknown = this._templateResult?.result;
return typeof result === "object"
? JSON.stringify(result, null, 2)
: String(result);
}
private async _copyResult() {
await copyToClipboard(this._resultText());
showToast(this, {
message: this.hass.localize("ui.common.copied_clipboard"),
});
}
private _splitRepositioned(ev: HASSDomTargetEvent<HaSplitPanel>) {
this._splitPosition = (ev.target as HaSplitPanel).position;
this._storeSplitPosition();
@@ -581,7 +609,7 @@ ${
overflow: auto;
display: flex;
flex-direction: column;
gap: var(--ha-space-2);
gap: var(--ha-space-4);
}
.edit-pane {
@@ -600,8 +628,38 @@ ${
display: block;
}
.render-pane ha-label {
align-self: flex-start;
.result-output {
flex: none;
border: 1px solid var(--divider-color);
border-radius: var(--ha-border-radius-sm);
overflow: hidden;
background-color: var(--ha-color-fill-neutral-quiet-resting);
}
.result-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--ha-space-2);
height: 28px;
padding-inline: var(--ha-space-3) var(--ha-space-1);
border-bottom: 1px solid var(--divider-color);
background-color: var(
--code-editor-gutter-color,
var(--secondary-background-color)
);
}
.result-type {
color: var(--secondary-text-color);
font-size: var(--ha-font-size-s);
font-weight: var(--ha-font-weight-medium);
}
.result-header ha-icon-button {
--ha-icon-button-size: 24px;
--mdc-icon-size: 18px;
color: var(--secondary-text-color);
}
.empty {
@@ -619,17 +677,49 @@ ${
font-family: var(--ha-font-family-code);
-webkit-font-smoothing: var(--ha-font-smoothing);
-moz-osx-font-smoothing: var(--ha-moz-osx-font-smoothing);
clear: both;
white-space: pre-wrap;
background-color: var(--secondary-background-color);
border-radius: var(--ha-border-radius-md);
padding: var(--ha-space-2);
margin-top: 0;
margin-bottom: 0;
overflow-wrap: anywhere;
padding: var(--ha-space-3);
margin: 0;
direction: ltr;
}
p,
p {
margin-block: 0;
}
.listeners {
display: flex;
flex-direction: column;
gap: var(--ha-space-2);
color: var(--secondary-text-color);
font-size: var(--ha-font-size-s);
}
.listener-list {
display: grid;
grid-template-columns: auto minmax(0, 1fr);
gap: var(--ha-space-3) var(--ha-space-4);
margin: 0;
}
.listener-list dt {
font-weight: var(--ha-font-weight-medium);
}
.listener-list dd {
display: flex;
flex-direction: column;
gap: var(--ha-space-1);
margin: 0;
}
.listener-list code {
font-family: var(--ha-font-family-code);
color: var(--primary-text-color);
overflow-wrap: anywhere;
}
ul {
margin-block: 0;
}
@@ -652,10 +742,6 @@ ${
color: var(--secondary-text-color);
}
.all_listeners {
color: var(--warning-color);
}
ha-tip {
padding: 0 var(--ha-space-4) var(--ha-space-4);
display: block;
@@ -1,12 +1,17 @@
import { mdiContentCopy } from "@mdi/js";
import type { CSSResultGroup, TemplateResult, PropertyValues } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { componentsWithService } from "../../../../common/config/components_with_service";
import type { HASSDomCurrentTargetEvent } from "../../../../common/dom/fire_event";
import { stringCompare } from "../../../../common/string/compare";
import { copyToClipboard } from "../../../../common/util/copy-clipboard";
import "../../../../components/buttons/ha-call-service-button";
import "../../../../components/ha-alert";
import "../../../../components/ha-button";
import "../../../../components/ha-card";
import "../../../../components/ha-icon-button";
import type { HaIconButton } from "../../../../components/ha-icon-button";
import "../../../../components/ha-spinner";
import type { CheckConfigResult } from "../../../../data/core";
import { checkCoreConfig } from "../../../../data/core";
@@ -14,6 +19,7 @@ import { domainToName } from "../../../../data/integration";
import { showRestartDialog } from "../../../../dialogs/restart/show-dialog-restart";
import { haStyle } from "../../../../resources/styles";
import type { HomeAssistant, Route, TranslationDict } from "../../../../types";
import { showToast } from "../../../../util/toast";
type ReloadableDomain = Exclude<
keyof TranslationDict["ui"]["panel"]["config"]["tools"]["tabs"]["yaml"]["section"]["reloading"],
@@ -133,6 +139,15 @@ export class ToolsYamlConfig extends LitElement {
<pre class="validate-log">${
this._validateResult.errors
}</pre>
<ha-icon-button
slot="action"
.path=${mdiContentCopy}
.label=${this.hass.localize(
"ui.panel.config.tools.tabs.yaml.section.validation.copy_errors"
)}
data-value=${this._validateResult.errors}
@click=${this._copyLog}
></ha-icon-button>
</ha-alert>`
: ""
}
@@ -148,6 +163,15 @@ export class ToolsYamlConfig extends LitElement {
<pre class="validate-log">${
this._validateResult.warnings
}</pre>
<ha-icon-button
slot="action"
.path=${mdiContentCopy}
.label=${this.hass.localize(
"ui.panel.config.tools.tabs.yaml.section.validation.copy_warnings"
)}
data-value=${this._validateResult.warnings}
@click=${this._copyLog}
></ha-icon-button>
</ha-alert>`
: ""
}
@@ -216,6 +240,19 @@ export class ToolsYamlConfig extends LitElement {
`;
}
private async _copyLog(ev: HASSDomCurrentTargetEvent<HaIconButton>) {
const value = ev.currentTarget.dataset.value;
if (value === undefined) {
return;
}
await copyToClipboard(value);
showToast(this, {
message: this.hass.localize("ui.common.copied_clipboard"),
});
}
private async _validateConfig() {
this._validating = true;
this._validateResult = undefined;
@@ -257,6 +294,11 @@ export class ToolsYamlConfig extends LitElement {
margin: 0;
}
ha-alert + ha-alert {
display: block;
margin-top: var(--ha-space-2);
}
.content {
padding: var(--ha-space-7) var(--ha-content-gutter) var(--ha-space-4);
max-width: var(--ha-content-width-narrow);
+4 -1
View File
@@ -4604,6 +4604,7 @@
"editor": "Template editor",
"result": "Result",
"result_placeholder": "Your template result will appear here.",
"copy_result": "Copy result",
"layout_stacked": "Drag to resize, click for stacked view",
"layout_side_by_side": "Drag to resize, click for side-by-side view",
"reset": "Reset to demo template",
@@ -4782,7 +4783,9 @@
"valid": "Configuration will not prevent Home Assistant from starting!",
"invalid": "Configuration invalid!",
"warnings": "Configuration warnings",
"errors": "Configuration errors"
"errors": "Configuration errors",
"copy_warnings": "Copy warnings",
"copy_errors": "Copy errors"
},
"reloading": {
"all": "All YAML configuration",