From fef54c9704938ebedbbd227bca01655eaa66a5ed Mon Sep 17 00:00:00 2001 From: kyleboyer Date: Sat, 27 Jul 2024 11:12:07 -0500 Subject: [PATCH] Refresh services list on existing accessory --- package-lock.json | 4 ++-- package.json | 2 +- src/accessory/base.ts | 7 +++++++ src/accessory/garage-door.ts | 3 +++ src/platform.ts | 6 ++++-- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 35bae1d..e3e3809 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "homebridge-blaq", - "version": "0.2.22", + "version": "0.2.23", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "homebridge-blaq", - "version": "0.2.22", + "version": "0.2.23", "funding": [ { "type": "github", diff --git a/package.json b/package.json index 1c4fcda..9a933fe 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "private": false, "displayName": "Konnected BlaQ", "name": "homebridge-blaq", - "version": "0.2.22", + "version": "0.2.23", "description": "Control and view your garage door(s) remotely with real-time updates using Konnected's BlaQ hardware", "license": "Apache-2.0", "type": "module", diff --git a/src/accessory/base.ts b/src/accessory/base.ts index 3963b46..99e07e8 100644 --- a/src/accessory/base.ts +++ b/src/accessory/base.ts @@ -76,6 +76,13 @@ export class BaseBlaQAccessory implements BaseBlaQAccessoryInterface { this.accessory.addService(service as Service); } + protected removeService(service: WithUUID | Service): void{ + const foundService = this.accessory.getService(service as WithUUID); + if(foundService){ + this.accessory.removeService(foundService); + } + } + handleStateEvent(stateEvent: StateUpdateMessageEvent){ try { const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord; diff --git a/src/accessory/garage-door.ts b/src/accessory/garage-door.ts index 4f0f040..2e39f87 100644 --- a/src/accessory/garage-door.ts +++ b/src/accessory/garage-door.ts @@ -44,6 +44,9 @@ export class BlaQGarageDoorAccessory extends BaseBlaQAccessory { this.garageDoorService = this.getOrAddService( args.type === 'garage' ? this.platform.service.GarageDoorOpener : this.platform.service.WindowCovering, ); + this.removeService( + args.type !== 'garage' ? this.platform.service.GarageDoorOpener : this.platform.service.WindowCovering, + ); // Set the service name. This is what is displayed as the name on the Home // app. We use what we stored in `accessory.context` in `discoverDevices`. diff --git a/src/platform.ts b/src/platform.ts index b567ea0..c2a60d2 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -1,4 +1,4 @@ -import { API, Characteristic, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service } from 'homebridge'; +import { API, Categories, Characteristic, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service } from 'homebridge'; import * as Bonjour from 'bonjour-service'; import { BlaQHub } from './hub.js'; @@ -128,13 +128,15 @@ export class BlaQHomebridgePluginPlatform implements DynamicPlatformPlugin { const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid); const configDeviceKey = this.getDeviceKey(configDevice); if (existingAccessory) { + // refresh services this.logger.info('Restoring existing accessory from cache:', existingAccessory.displayName); existingAccessory.context.device = configDevice; this.api.updatePlatformAccessories(this.hubAccessories[configDeviceKey] || [existingAccessory]); + existingAccessory.services.forEach(service => existingAccessory.removeService(service)); return { platform: this, accessory: existingAccessory }; } else { this.logger.info(`Adding new accessory: ${model} #${serialnumber}`); - const accessory = new this.api.platformAccessory(configDevice.displayName, uuid); + const accessory = new this.api.platformAccessory(configDevice.displayName, uuid, Categories.GARAGE_DOOR_OPENER); accessory.context.device = configDevice; this.hubAccessories[this.getDeviceKey(configDevice)].push(accessory); this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, this.hubAccessories[configDeviceKey]);