From d2805b655c7894e335b61e0a0b1e9680792b4ab1 Mon Sep 17 00:00:00 2001 From: Matthias Hochgatterer Date: Wed, 29 Apr 2020 11:05:44 +0200 Subject: [PATCH] Add property to ignore updates of same characteristic values #128 #166 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. --- characteristic/characteristic.go | 6 +++++- characteristic/characteristic_test.go | 23 +++++++++++++++------ characteristic/programmable_switch_event.go | 2 ++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/characteristic/characteristic.go b/characteristic/characteristic.go index fc72b339..74952f22 100644 --- a/characteristic/characteristic.go +++ b/characteristic/characteristic.go @@ -30,6 +30,7 @@ type Characteristic struct { // unused Events bool `json:"-"` + updateOnSameValue bool // if true the update notifications connValueUpdateFuncs []ConnChangeFunc valueChangeFuncs []ChangeFunc valueGetFunc GetFunc @@ -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 } diff --git a/characteristic/characteristic_test.go b/characteristic/characteristic_test.go index f5279e49..ca6e1519 100644 --- a/characteristic/characteristic_test.go +++ b/characteristic/characteristic_test.go @@ -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) diff --git a/characteristic/programmable_switch_event.go b/characteristic/programmable_switch_event.go index d0b926d5..80262af1 100644 --- a/characteristic/programmable_switch_event.go +++ b/characteristic/programmable_switch_event.go @@ -20,5 +20,7 @@ func NewProgrammableSwitchEvent() *ProgrammableSwitchEvent { char.SetValue(0) + char.updateOnSameValue = true + return &ProgrammableSwitchEvent{char} }