From 409f06134ead51fb9e36f9ee1e93a4b552f8ccc6 Mon Sep 17 00:00:00 2001 From: Stuart Olivera Date: Mon, 19 Apr 2021 23:20:45 -0700 Subject: [PATCH] feat: remove devices from HomeKit if they go offline fixes #45 --- src/platformAccessory.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/platformAccessory.ts b/src/platformAccessory.ts index 2c9b779..52ff4ba 100644 --- a/src/platformAccessory.ts +++ b/src/platformAccessory.ts @@ -1,5 +1,6 @@ import type { Service, PlatformAccessory } from 'homebridge' +import { PLATFORM_NAME, PLUGIN_NAME } from './settings' import { HomebridgeLgThinqPlatform } from './platform' import { GetDashboardResponse } from './thinq/apiTypes' import AbstractCharacteristic from './characteristic/abstractCharacteristic' @@ -17,6 +18,8 @@ export class LgAirConditionerPlatformAccessory { // eslint-disable-next-line @typescript-eslint/no-explicit-any private characteristics: Array> + deregisterAccessory?: () => void = undefined + getDevice(): Unpacked | undefined { return this.accessory.context.device } @@ -88,14 +91,23 @@ export class LgAirConditionerPlatformAccessory { this.platform.log.debug('Getting device status', this.getDeviceId()) const device = await this.platform.thinqApi.getDevice(this.getDeviceId()!) this.platform.log.debug('device response', device) - this.platform.log.debug( - 'device response.result.snapshot', - device.result.snapshot, - ) + const snapshot = device.result.snapshot + this.platform.log.debug('device response.result.snapshot', snapshot) + + if (!snapshot.online) { + this.platform.log.info( + 'Removing offline device from HomeKit. If you need this device again, please restart Homebridge.', + ) + this.platform.api.unregisterPlatformAccessories( + PLUGIN_NAME, + PLATFORM_NAME, + [this.accessory], + ) + } for (const characteristic of this.characteristics) { try { - characteristic.handleUpdatedSnapshot(device.result.snapshot) + characteristic.handleUpdatedSnapshot(snapshot) } catch (error) { this.platform.log.error( 'Error updating characteristic ' + characteristic.constructor.name,