Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
feat: remove devices from HomeKit if they go offline
Browse files Browse the repository at this point in the history
fixes #45
  • Loading branch information
sman591 committed Apr 20, 2021
1 parent cc1c17b commit 409f061
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -17,6 +18,8 @@ export class LgAirConditionerPlatformAccessory {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private characteristics: Array<AbstractCharacteristic<any, any, any>>

deregisterAccessory?: () => void = undefined

getDevice(): Unpacked<GetDashboardResponse['result']['item']> | undefined {
return this.accessory.context.device
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 409f061

Please sign in to comment.