Cached accessories not updated when accessory is modified
Analysis
The cached accessories are not getting updated when the accessory is modified.
Consider the case where two switches are created. Switch Without Sensor is a simple Switch. Switch With Sensor creates a Switch along with a companion Contact Sensor. The following configuration snippet shows the two accessories:
{
"accessoryID": "4260746",
"accessoryName": "Switch Without Sensor",
"accessoryType": "switch",
"accessoryIsStateful": false,
"switch": {
"defaultState": "off",
"hasResetTimer": false,
"hasCompanionSensor": false,
"muteLogging": false
}
},
{
"accessoryID": "7929993",
"accessoryName": "Switch With Sensor",
"accessoryType": "switch",
"accessoryIsStateful": false,
"switch": {
"defaultState": "off",
"hasResetTimer": false,
"hasCompanionSensor": true,
"muteLogging": false
},
"companionSensor": {
"name": "Switch With Sensor Companion Sensor",
"type": "contact"
}
}
After creating the two accessories above and restarting Homebridge, they are shown in the accessories screen. Per the configuration the two Switches are named Switch Without Sensor and Switch With Sensor, and the sensor is named Switch With Sensor Companion Sensor:
Now the two accessories are modified by adding a companion Contact Sensor to the Switch Switch Without Sensor and removing the companion Contact Sensor from the Switch Switch With Sensor. The following updated configuration snippet shows the two accessories:
{
"accessoryID": "4260746",
"accessoryName": "Switch Without Sensor",
"accessoryType": "switch",
"accessoryIsStateful": false,
"switch": {
"defaultState": "off",
"hasResetTimer": false,
"hasCompanionSensor": true,
"muteLogging": false
},
"companionSensor": {
"name": "Switch Without Sensor Companion Sensor",
"type": "contact"
}
},
{
"accessoryID": "7929993",
"accessoryName": "Switch With Sensor",
"accessoryType": "switch",
"accessoryIsStateful": false,
"switch": {
"defaultState": "off",
"hasResetTimer": false,
"hasCompanionSensor": false,
"muteLogging": false
}
}
After updating the two accessories above and restarting Homebridge, they are shown in the accessories screen. Per the configuration the two Switches are named Switch Without Sensor and Switch With Sensor, and the sensor is named Switch Without Sensor Companion Sensor. However, the removed Switch With Sensor Companion Sensor is not removed:
Every time the homebridge-virtual-accessories plugin creates the accessory handler for a restored accessory, it calls API.updatePlatformAccessories([cachedAccessory]); in the event that the cached accessory has changed. However this doesn't appear to properly update the Homebridge cache. When Switch Without Sensor is updated to add a Sensor Service, this is correctly updated in Homebridge. When Switch With Sensor is updated to remove the Sensor Service, this is incorrectly updated in Homebridge.
Expected Behavior
Calls to API.updatePlatformAccessories([cachedAccessory]); properly update the Homebridge cache, both adding and removing Services correctly without Homebridge implementation details bleeding out.
Steps To Reproduce
See above.
Logs
N/A
Configuration
N/A
Environment
- OS: macOS Sequoia
- Homebridge: 1.11.4
- Homebridge UI: v5.21.1-beta.16
- Node: 24.11.0
Process Supervisor
hb-service
Additional Context
N/A
Source: homebridge/homebridge