Make using template rendering optional when using MQTT publish from the config entry page (#14828)

This commit is contained in:
Jan Bouwhuis 2022-12-28 12:12:30 +01:00 committed by GitHub
parent d4d3a1cb65
commit 1d1ff410b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View File

@ -33,6 +33,9 @@ class HaPanelDevMqtt extends LitElement {
@LocalStorage("panel-dev-mqtt-retain-ls", true, false) @LocalStorage("panel-dev-mqtt-retain-ls", true, false)
private _retain = false; private _retain = false;
@LocalStorage("panel-dev-mqtt-allow-template-ls", true, false)
private _allowTemplate = false;
protected render(): TemplateResult { protected render(): TemplateResult {
return html` return html`
<hass-subpage .narrow=${this.narrow} .hass=${this.hass}> <hass-subpage .narrow=${this.narrow} .hass=${this.hass}>
@ -74,7 +77,25 @@ class HaPanelDevMqtt extends LitElement {
></ha-switch> ></ha-switch>
</ha-formfield> </ha-formfield>
</div> </div>
<p>${this.hass.localize("ui.panel.config.mqtt.payload")}</p> <p>
<ha-formfield
.label=${this.hass!.localize(
"ui.panel.config.mqtt.allow_template"
)}
>
<ha-switch
@change=${this._handleAllowTemplate}
.checked=${this._allowTemplate}
></ha-switch>
</ha-formfield>
</p>
<p>
${this._allowTemplate
? this.hass.localize("ui.panel.config.mqtt.payload")
: this.hass.localize(
"ui.panel.config.mqtt.payload_no_template"
)}
</p>
<ha-code-editor <ha-code-editor
mode="jinja2" mode="jinja2"
autocomplete-entities autocomplete-entities
@ -119,13 +140,18 @@ class HaPanelDevMqtt extends LitElement {
this._retain = (ev.target! as any).checked; this._retain = (ev.target! as any).checked;
} }
private _handleAllowTemplate(ev: CustomEvent) {
this._allowTemplate = (ev.target! as any).checked;
}
private _publish(): void { private _publish(): void {
if (!this.hass) { if (!this.hass) {
return; return;
} }
this.hass.callService("mqtt", "publish", { this.hass.callService("mqtt", "publish", {
topic: this._topic, topic: this._topic,
payload_template: this._payload, payload: !this._allowTemplate ? this._payload : undefined,
payload_template: this._allowTemplate ? this._payload : undefined,
qos: parseInt(this._qos), qos: parseInt(this._qos),
retain: this._retain, retain: this._retain,
}); });

View File

@ -3215,6 +3215,8 @@
"description_publish": "Publish a packet", "description_publish": "Publish a packet",
"topic": "Topic", "topic": "Topic",
"payload": "Payload (template allowed)", "payload": "Payload (template allowed)",
"payload_no_template": "Payload",
"allow_template": "Allow template",
"publish": "Publish", "publish": "Publish",
"description_listen": "Listen to a topic", "description_listen": "Listen to a topic",
"json_formatting": "Format JSON content", "json_formatting": "Format JSON content",