mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Add QoS option for MQTT subscribe (#14565)
Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
parent
00c2cb731b
commit
239d3ca00c
@ -37,11 +37,13 @@ export interface MQTTDeviceDebugInfo {
|
|||||||
export const subscribeMQTTTopic = (
|
export const subscribeMQTTTopic = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
topic: string,
|
topic: string,
|
||||||
callback: (message: MQTTMessage) => void
|
callback: (message: MQTTMessage) => void,
|
||||||
|
qos?: number
|
||||||
) =>
|
) =>
|
||||||
hass.connection.subscribeMessage<MQTTMessage>(callback, {
|
hass.connection.subscribeMessage<MQTTMessage>(callback, {
|
||||||
type: "mqtt/subscribe",
|
type: "mqtt/subscribe",
|
||||||
topic,
|
topic,
|
||||||
|
qos,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const fetchMQTTDebugInfo = (
|
export const fetchMQTTDebugInfo = (
|
||||||
|
@ -1,17 +1,26 @@
|
|||||||
import "@material/mwc-button";
|
import "@material/mwc-button";
|
||||||
import "@polymer/paper-input/paper-input";
|
|
||||||
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";
|
||||||
import { formatTime } from "../../../../../common/datetime/format_time";
|
|
||||||
import "../../../../../components/ha-card";
|
import "../../../../../components/ha-card";
|
||||||
|
import "../../../../../components/ha-select";
|
||||||
|
import "../../../../../components/ha-textfield";
|
||||||
|
import { formatTime } from "../../../../../common/datetime/format_time";
|
||||||
import { MQTTMessage, subscribeMQTTTopic } from "../../../../../data/mqtt";
|
import { MQTTMessage, subscribeMQTTTopic } from "../../../../../data/mqtt";
|
||||||
import { HomeAssistant } from "../../../../../types";
|
import { HomeAssistant } from "../../../../../types";
|
||||||
|
import "@material/mwc-list/mwc-list-item";
|
||||||
|
import { LocalStorage } from "../../../../../common/decorators/local-storage";
|
||||||
|
|
||||||
|
const qosLevel = ["0", "1", "2"];
|
||||||
|
|
||||||
@customElement("mqtt-subscribe-card")
|
@customElement("mqtt-subscribe-card")
|
||||||
class MqttSubscribeCard extends LitElement {
|
class MqttSubscribeCard extends LitElement {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
|
|
||||||
@state() private _topic = "";
|
@LocalStorage("panel-dev-mqtt-topic-subscribe", true, false)
|
||||||
|
private _topic = "";
|
||||||
|
|
||||||
|
@LocalStorage("panel-dev-mqtt-qos-subscribe", true, false)
|
||||||
|
private _qos = "0";
|
||||||
|
|
||||||
@state() private _subscribed?: () => void;
|
@state() private _subscribed?: () => void;
|
||||||
|
|
||||||
@ -38,14 +47,23 @@ class MqttSubscribeCard extends LitElement {
|
|||||||
header=${this.hass.localize("ui.panel.config.mqtt.description_listen")}
|
header=${this.hass.localize("ui.panel.config.mqtt.description_listen")}
|
||||||
>
|
>
|
||||||
<form>
|
<form>
|
||||||
<paper-input
|
<ha-textfield
|
||||||
.label=${this._subscribed
|
.label=${this._subscribed
|
||||||
? this.hass.localize("ui.panel.config.mqtt.listening_to")
|
? this.hass.localize("ui.panel.config.mqtt.listening_to")
|
||||||
: this.hass.localize("ui.panel.config.mqtt.subscribe_to")}
|
: this.hass.localize("ui.panel.config.mqtt.subscribe_to")}
|
||||||
.disabled=${this._subscribed !== undefined}
|
.disabled=${this._subscribed !== undefined}
|
||||||
.value=${this._topic}
|
.value=${this._topic}
|
||||||
@value-changed=${this._valueChanged}
|
@change=${this._handleTopic}
|
||||||
></paper-input>
|
></ha-textfield>
|
||||||
|
<ha-select
|
||||||
|
.label=${this.hass.localize("ui.panel.config.mqtt.qos")}
|
||||||
|
.disabled=${this._subscribed !== undefined}
|
||||||
|
.value=${this._qos}
|
||||||
|
@selected=${this._handleQos}
|
||||||
|
>${qosLevel.map(
|
||||||
|
(qos) => html`<mwc-list-item .value=${qos}>${qos}</mwc-list-item>`
|
||||||
|
)}
|
||||||
|
</ha-select>
|
||||||
<mwc-button
|
<mwc-button
|
||||||
.disabled=${this._topic === ""}
|
.disabled=${this._topic === ""}
|
||||||
@click=${this._handleSubmit}
|
@click=${this._handleSubmit}
|
||||||
@ -82,8 +100,15 @@ class MqttSubscribeCard extends LitElement {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _valueChanged(ev: CustomEvent): void {
|
private _handleTopic(ev): void {
|
||||||
this._topic = ev.detail.value;
|
this._topic = ev.target.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _handleQos(ev: CustomEvent): void {
|
||||||
|
const newValue = (ev.target! as any).value;
|
||||||
|
if (newValue >= 0 && newValue !== this._qos) {
|
||||||
|
this._qos = newValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _handleSubmit(): Promise<void> {
|
private async _handleSubmit(): Promise<void> {
|
||||||
@ -94,7 +119,8 @@ class MqttSubscribeCard extends LitElement {
|
|||||||
this._subscribed = await subscribeMQTTTopic(
|
this._subscribed = await subscribeMQTTTopic(
|
||||||
this.hass!,
|
this.hass!,
|
||||||
this._topic,
|
this._topic,
|
||||||
(message) => this._handleMessage(message)
|
(message) => this._handleMessage(message),
|
||||||
|
parseInt(this._qos)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,10 +151,6 @@ class MqttSubscribeCard extends LitElement {
|
|||||||
display: block;
|
display: block;
|
||||||
padding: 16px;
|
padding: 16px;
|
||||||
}
|
}
|
||||||
paper-input {
|
|
||||||
display: inline-block;
|
|
||||||
width: 200px;
|
|
||||||
}
|
|
||||||
.events {
|
.events {
|
||||||
margin: -16px 0;
|
margin: -16px 0;
|
||||||
padding: 0 16px;
|
padding: 0 16px;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user