I've been updating a simple HomeBridge Plugin, and as I can see the data for the HomeKit Accessory Information in the the API response from the connected advice, I'd like to populate that data into HomeKit.
The problem is AccessoryInformation only seems to accept data when I first create the service. Once getServices()
has been called by HomeBridge, further calls to updateCharacteristic()
on the AccessoryInformation service are ignored. The obvious problem here is, as the data is returned async, I don't yet have it at the point where HomeBridge calls getServices()
.
How do I force the AccessoryInformation to update once I have the data available? Or alternatively how do I delay HomeBridge calling getServices()
until after the data has loaded?
This is my code.
setDeviceInfo(responseData) {
// Property names match HK API, values device API
const deviceInfo = {
Model: 'MODEL',
Name: 'UPSNAME',
SerialNumber: 'SERIALNO',
SoftwareRevision: 'VERSION',
FirmwareRevision: 'FIRMWARE',
}
Object.entries(deviceInfo).forEach(([key, value]) => {
const info = responseData[value];
this.log.info(`${key}:`, info);
this.informationService.updateCharacteristic(Characteristic[key], info);
})
The service is created as follows:
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, config.manufacturer || DEFAULT_MANIFACTURER)
And passed to HomeBridge here
getServices() {claims to have
const services = [this.informationService, this.contactSensor, this.batteryService];
if (this.temperatureService) {
services.push(this.temperatureService);
}
return services;
}
The whole file is on GitHub https://github.com/cr3ative/homebridge-apcaccess/blob/ce5b53e44f7ba66c235495c9e14d66ed5cd8fa6d/index.js