Subscribe to zwave_js node status updates in device panel (#12916)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Raman Gupta 2022-06-19 15:22:02 -04:00 committed by GitHub
parent 6c5cf2a0ec
commit 529e27992e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View File

@ -275,6 +275,12 @@ export interface ZWaveJSRouteStatistics {
route_failed_between: [string, string] | null; route_failed_between: [string, string] | null;
} }
export interface ZWaveJSNodeStatusUpdatedMessage {
event: "ready" | "wake up" | "sleep" | "dead" | "alive";
ready: boolean;
status: NodeStatus;
}
export interface ZWaveJSRemovedNode { export interface ZWaveJSRemovedNode {
node_id: number; node_id: number;
manufacturer: string; manufacturer: string;
@ -473,6 +479,19 @@ export const fetchZwaveNodeStatus = (
device_id, device_id,
}); });
export const subscribeZwaveNodeStatus = (
hass: HomeAssistant,
device_id: string,
callbackFunction: (message: ZWaveJSNodeStatusUpdatedMessage) => void
): Promise<UnsubscribeFunc> =>
hass.connection.subscribeMessage(
(message: any) => callbackFunction(message),
{
type: "zwave_js/subscribe_node_status",
device_id,
}
);
export const fetchZwaveNodeMetadata = ( export const fetchZwaveNodeMetadata = (
hass: HomeAssistant, hass: HomeAssistant,
device_id: string device_id: string

View File

@ -8,6 +8,7 @@ import {
} from "lit"; } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import "../../../../../../components/ha-expansion-panel"; import "../../../../../../components/ha-expansion-panel";
import { UnsubscribeFunc } from "home-assistant-js-websocket";
import { import {
ConfigEntry, ConfigEntry,
getConfigEntries, getConfigEntries,
@ -17,13 +18,15 @@ import {
fetchZwaveNodeStatus, fetchZwaveNodeStatus,
nodeStatus, nodeStatus,
SecurityClass, SecurityClass,
subscribeZwaveNodeStatus,
ZWaveJSNodeStatus, ZWaveJSNodeStatus,
} from "../../../../../../data/zwave_js"; } from "../../../../../../data/zwave_js";
import { haStyle } from "../../../../../../resources/styles"; import { haStyle } from "../../../../../../resources/styles";
import { HomeAssistant } from "../../../../../../types"; import { HomeAssistant } from "../../../../../../types";
import { SubscribeMixin } from "../../../../../../mixins/subscribe-mixin";
@customElement("ha-device-info-zwave_js") @customElement("ha-device-info-zwave_js")
export class HaDeviceInfoZWaveJS extends LitElement { export class HaDeviceInfoZWaveJS extends SubscribeMixin(LitElement) {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass!: HomeAssistant;
@property({ attribute: false }) public device!: DeviceRegistryEntry; @property({ attribute: false }) public device!: DeviceRegistryEntry;
@ -41,6 +44,21 @@ export class HaDeviceInfoZWaveJS extends LitElement {
} }
} }
public hassSubscribe(): Array<UnsubscribeFunc | Promise<UnsubscribeFunc>> {
return [
subscribeZwaveNodeStatus(this.hass, this.device!.id, (message) => {
if (!this._node) {
return;
}
this._node = {
...this._node,
status: message.status,
ready: message.ready,
};
}),
];
}
protected async _fetchNodeDetails() { protected async _fetchNodeDetails() {
if (!this.device) { if (!this.device) {
return; return;