Update zwave_js log subscription to handle core changes (#9262)

This commit is contained in:
Raman Gupta 2021-06-17 11:19:18 -04:00 committed by GitHub
parent 9fbd594f37
commit ce3c8f264d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 9 deletions

View File

@ -193,6 +193,18 @@ export const getIdentifiersFromDevice = (
};
};
export type ZWaveJSLogUpdate = ZWaveJSLogMessageUpdate | ZWaveJSLogConfigUpdate;
interface ZWaveJSLogMessageUpdate {
type: "log_message";
log_message: ZWaveJSLogMessage;
}
interface ZWaveJSLogConfigUpdate {
type: "log_config";
log_config: ZWaveJSLogConfig;
}
export interface ZWaveJSLogMessage {
timestamp: string;
level: string;
@ -203,10 +215,10 @@ export interface ZWaveJSLogMessage {
export const subscribeZWaveJSLogs = (
hass: HomeAssistant,
entry_id: string,
callback: (message: ZWaveJSLogMessage) => void
callback: (update: ZWaveJSLogUpdate) => void
) =>
hass.connection.subscribeMessage<ZWaveJSLogMessage>(callback, {
type: "zwave_js/subscribe_logs",
hass.connection.subscribeMessage<ZWaveJSLogUpdate>(callback, {
type: "zwave_js/subscribe_log_updates",
entry_id,
});

View File

@ -33,16 +33,20 @@ class ZWaveJSLogs extends SubscribeMixin(LitElement) {
public hassSubscribe(): Array<UnsubscribeFunc | Promise<UnsubscribeFunc>> {
return [
subscribeZWaveJSLogs(this.hass, this.configEntryId, (log) => {
subscribeZWaveJSLogs(this.hass, this.configEntryId, (update) => {
if (!this.hasUpdated) {
return;
}
if (Array.isArray(log.message)) {
for (const line of log.message) {
this._textarea!.value += `${line}\n`;
if (update.type === "log_message") {
if (Array.isArray(update.log_message.message)) {
for (const line of update.log_message.message) {
this._textarea!.value += `${line}\n`;
}
} else {
this._textarea!.value += `${update.log_message.message}\n`;
}
} else {
this._textarea!.value += `${log.message}\n`;
this._logConfig = update.log_config;
}
}).then((unsub) => {
this._textarea!.value += `${this.hass.localize(
@ -141,7 +145,6 @@ class ZWaveJSLogs extends SubscribeMixin(LitElement) {
return;
}
setZWaveJSLogLevel(this.hass!, this.configEntryId, selected);
this._logConfig.level = selected;
this._textarea!.value += `${this.hass.localize(
"ui.panel.config.zwave_js.logs.log_level_changed",
{ level: selected.charAt(0).toUpperCase() + selected.slice(1) }