Skip to content

Commit

Permalink
Add property to ignore updates of same characteristic values #128 #166
Browse files Browse the repository at this point in the history
If a characteristic value is updated with the same value, it is ignored.
Only exception is the programmable switch event characteristic, which
requires that clients are notified even when updating with the same value.
  • Loading branch information
brutella committed Apr 29, 2020
1 parent 51f7e04 commit d2805b6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
6 changes: 5 additions & 1 deletion characteristic/characteristic.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Characteristic struct {
// unused
Events bool `json:"-"`

updateOnSameValue bool // if true the update notifications
connValueUpdateFuncs []ConnChangeFunc
valueChangeFuncs []ChangeFunc
valueGetFunc GetFunc
Expand Down Expand Up @@ -124,8 +125,11 @@ func (c *Characteristic) updateValue(value interface{}, conn net.Conn, checkPerm
value = c.clampInt(value.(int))
}

if c.Value == value && !c.updateOnSameValue {
return
}

// Ignore new values from remote when permissions don't allow write and checkPerms is true
// Ignore value when not writable
if checkPerms && !c.isWritable() {
return
}
Expand Down
23 changes: 17 additions & 6 deletions characteristic/characteristic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,32 @@ func TestCharacteristicRemoteDelegate(t *testing.T) {
}
}

func TestValueChange(t *testing.T) {
c := NewBrightness()
c.Value = 5
func TestValueChangeNoUPdate(t *testing.T) {
c := NewOn()
c.Value = true

changed := false
c.OnValueUpdateFromConn(func(conn net.Conn, c *Characteristic, new, old interface{}) {
c.OnValueUpdate(func(c *Characteristic, new, old interface{}) {
changed = true
})

c.UpdateValue(true)

if is, want := changed, false; is != want {
t.Fatalf("%v != %v", is, want)
}
}

func TestValueChange(t *testing.T) {
c := NewProgrammableSwitchEvent()
c.Value = ProgrammableSwitchEventSinglePress

changed := false
c.OnValueUpdate(func(c *Characteristic, new, old interface{}) {
changed = true
})

c.UpdateValue(5)
c.UpdateValueFromConnection(5, TestConn)
c.UpdateValue(ProgrammableSwitchEventSinglePress)

if is, want := changed, true; is != want {
t.Fatalf("%v != %v", is, want)
Expand Down
2 changes: 2 additions & 0 deletions characteristic/programmable_switch_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ func NewProgrammableSwitchEvent() *ProgrammableSwitchEvent {

char.SetValue(0)

char.updateOnSameValue = true

return &ProgrammableSwitchEvent{char}
}

0 comments on commit d2805b6

Please sign in to comment.