Skip to content

Commit

Permalink
Refresh services list on existing accessory
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleBoyer committed Jul 27, 2024
1 parent 70e860c commit fef54c9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions src/accessory/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ export class BaseBlaQAccessory implements BaseBlaQAccessoryInterface {
this.accessory.addService(service as Service);
}

protected removeService(service: WithUUID<typeof Service> | Service): void{
const foundService = this.accessory.getService(service as WithUUID<typeof Service>);
if(foundService){
this.accessory.removeService(foundService);
}
}

handleStateEvent(stateEvent: StateUpdateMessageEvent){
try {
const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord;
Expand Down
3 changes: 3 additions & 0 deletions src/accessory/garage-door.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
6 changes: 4 additions & 2 deletions src/platform.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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]);
Expand Down

0 comments on commit fef54c9

Please sign in to comment.