Introduce SetCharacteristicValueFunc to the Mapping

This function allows either replacing or modifying the usual
Exposed->Characteristic value propagation.

It also adds a CurrentValue to the mapping to monitor for
changes/updates, regardless of whether the value was updated into the
Characteristic. This will be the source of truth.
This commit is contained in:
Darell Tan
2024-11-24 15:52:40 +08:00
parent 8d5dc24d7d
commit e9b0e4b0f9
2 changed files with 70 additions and 3 deletions

View File

@@ -697,9 +697,34 @@ func (br *Bridge) UpdateAccessoryState(devName string, payload []byte) {
if BRIDGE_DEVMODE {
log.Printf("updating %q to %+v", prop, newVal)
}
_, errCode := mapping.SetCharacteristicValue(newVal)
if errCode != 0 {
log.Printf("unable to update characteristic value for %q: %d", prop, errCode)
// convert to Characteristic value to determine if it has changed
// since we don't know what the Exposed -> Characteristic mapping would be
newCv, err := mapping.ToCharacteristicValue(newVal)
if err != nil {
log.Printf("unable to convert characteristic value for %q: %v", prop, err)
continue
}
oldCv := mapping.SetCurrentValue(newCv)
changed := oldCv != newCv
// call the Set func if defined
doDefault := true
setFunc := mapping.SetCharacteristicValueFunc
if setFunc != nil {
doDefault, err = setFunc(mapping, newCv, changed)
if err != nil {
log.Printf("SetCharacteristicValueFunc for %s error: %+v", mapping, err)
continue
}
}
if doDefault {
_, errCode := mapping.Characteristic.SetValueRequest(newCv, nil)
if errCode != 0 {
log.Printf("unable to update characteristic value for %q: %d", prop, errCode)
}
}
}
}