mirror of
https://github.com/nikdoof/hapz2m.git
synced 2026-01-30 09:18:17 +00:00
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:
31
bridge.go
31
bridge.go
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user