diff --git a/src/panels/config/devices/device-detail/integration-elements/zwave_js/device-alerts.ts b/src/panels/config/devices/device-detail/integration-elements/zwave_js/device-alerts.ts new file mode 100644 index 0000000000..c7510dad68 --- /dev/null +++ b/src/panels/config/devices/device-detail/integration-elements/zwave_js/device-alerts.ts @@ -0,0 +1,20 @@ +import { DeviceRegistryEntry } from "../../../../../../data/device_registry"; +import { fetchZwaveNodeComments } from "../../../../../../data/zwave_js"; +import { HomeAssistant } from "../../../../../../types"; +import { DeviceAlert } from "../../../ha-config-device-page"; + +export const getZwaveDeviceAlerts = async ( + hass: HomeAssistant, + device: DeviceRegistryEntry +): Promise => { + const nodeComments = await fetchZwaveNodeComments(hass, device.id); + + if (!nodeComments?.comments?.length) { + return []; + } + + return nodeComments.comments.map((comment) => ({ + level: comment.level, + text: comment.text, + })); +}; diff --git a/src/panels/config/devices/device-detail/integration-elements/zwave_js/ha-device-alerts-zwave_js.ts b/src/panels/config/devices/device-detail/integration-elements/zwave_js/ha-device-alerts-zwave_js.ts deleted file mode 100644 index f99683a9c9..0000000000 --- a/src/panels/config/devices/device-detail/integration-elements/zwave_js/ha-device-alerts-zwave_js.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { html, LitElement, PropertyValues, TemplateResult } from "lit"; -import { customElement, property, state } from "lit/decorators"; -import { DeviceRegistryEntry } from "../../../../../../data/device_registry"; -import { - ZwaveJSNodeComments, - fetchZwaveNodeComments, -} from "../../../../../../data/zwave_js"; -import { HomeAssistant } from "../../../../../../types"; - -@customElement("ha-device-alerts-zwave_js") -export class HaDeviceAlertsZWaveJS extends LitElement { - @property({ attribute: false }) public hass!: HomeAssistant; - - @property({ attribute: false }) public device!: DeviceRegistryEntry; - - @state() private _nodeComments?: ZwaveJSNodeComments; - - protected willUpdate(changedProperties: PropertyValues) { - super.willUpdate(changedProperties); - if (changedProperties.has("device")) { - this._fetchNodeDetails(); - } - } - - private async _fetchNodeDetails() { - this._nodeComments = await fetchZwaveNodeComments( - this.hass, - this.device.id - ); - } - - protected render(): TemplateResult { - if (this._nodeComments && this._nodeComments.comments?.length > 0) { - return html` -
- ${this._nodeComments.comments.map( - (comment) => html` - ${comment.text} - ` - )} -
- `; - } - return html``; - } -} - -declare global { - interface HTMLElementTagNameMap { - "ha-device-alerts-zwave_js": HaDeviceAlertsZWaveJS; - } -} diff --git a/src/panels/config/devices/ha-config-device-page.ts b/src/panels/config/devices/ha-config-device-page.ts index 305575ebe4..cfca8b845e 100644 --- a/src/panels/config/devices/ha-config-device-page.ts +++ b/src/panels/config/devices/ha-config-device-page.ts @@ -83,6 +83,11 @@ export interface DeviceAction { classes?: string; } +export interface DeviceAlert { + level: "warning" | "error" | "info"; + text: string; +} + @customElement("ha-config-device-page") export class HaConfigDevicePage extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @@ -114,6 +119,8 @@ export class HaConfigDevicePage extends LitElement { @state() private _deviceActions?: DeviceAction[]; + @state() private _deviceAlerts?: DeviceAlert[]; + private _logbookTime = { recent: 86400 }; private _device = memoizeOne( @@ -215,12 +222,14 @@ export class HaConfigDevicePage extends LitElement { this._diagnosticDownloadLinks = undefined; this._deleteButtons = undefined; this._deviceActions = undefined; + this._deviceAlerts = undefined; } if ( (this._diagnosticDownloadLinks && this._deleteButtons && - this._deviceActions) || + this._deviceActions && + this._deviceAlerts) || !this.devices || !this.deviceId || !this.entries @@ -231,9 +240,11 @@ export class HaConfigDevicePage extends LitElement { this._diagnosticDownloadLinks = Math.random(); this._deleteButtons = []; // To prevent re-rendering if no delete buttons this._deviceActions = []; + this._deviceAlerts = []; this._getDiagnosticButtons(this._diagnosticDownloadLinks); this._getDeleteActions(); this._getDeviceActions(); + this._getDeviceAlerts(); } protected firstUpdated(changedProps) { @@ -279,7 +290,6 @@ export class HaConfigDevicePage extends LitElement { const area = this._computeArea(this.areas, device); const deviceInfo: TemplateResult[] = []; - const deviceAlerts: TemplateResult[] = []; const actions = [...(this._deviceActions || [])]; if (Array.isArray(this._diagnosticDownloadLinks)) { @@ -320,7 +330,7 @@ export class HaConfigDevicePage extends LitElement { ); } - this._renderIntegrationInfo(device, integrations, deviceInfo, deviceAlerts); + this._renderIntegrationInfo(device, integrations, deviceInfo); return html`
${ - deviceAlerts.length - ? html`
${deviceAlerts}
` + this._deviceAlerts?.length + ? html` +
+ ${this._deviceAlerts.map( + (alert) => + html` + + ${alert.text} + + ` + )} +
+ ` : "" } int.domain + ); + + if (domains.includes("zwave_js")) { + const zwave = await import( + "./device-detail/integration-elements/zwave_js/device-alerts" + ); + + const alerts = await zwave.getZwaveDeviceAlerts(this.hass, device); + deviceAlerts.push(...alerts); + } + + if (deviceAlerts.length) { + this._deviceAlerts = deviceAlerts; + } + } + private _computeEntityName(entity: EntityRegistryEntry) { if (entity.name) { return entity.name; @@ -1023,8 +1071,7 @@ export class HaConfigDevicePage extends LitElement { private _renderIntegrationInfo( device: DeviceRegistryEntry, integrations: ConfigEntry[], - deviceInfo: TemplateResult[], - deviceAlerts: TemplateResult[] + deviceInfo: TemplateResult[] ) { const domains = integrations.map((int) => int.domain); if (domains.includes("zha")) { @@ -1037,18 +1084,9 @@ export class HaConfigDevicePage extends LitElement { `); } if (domains.includes("zwave_js")) { - import( - "./device-detail/integration-elements/zwave_js/ha-device-alerts-zwave_js" - ); import( "./device-detail/integration-elements/zwave_js/ha-device-info-zwave_js" ); - deviceAlerts.push(html` - - `); deviceInfo.push(html`