From 5cdb4f3cf5bf0bf4f0e61fdf5074d5bda0cd2876 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Thu, 3 Jun 2021 23:30:23 +0200 Subject: [PATCH] fix hue light/sensor search and add a separate SensorPresenceEnding event. --- internal/drivers/hue/bridge.go | 4 ++++ internal/drivers/hue/state.go | 30 +++++++++++++++++++++--------- models/event.go | 5 +++-- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/internal/drivers/hue/bridge.go b/internal/drivers/hue/bridge.go index 00a1c44..88d7439 100644 --- a/internal/drivers/hue/bridge.go +++ b/internal/drivers/hue/bridge.go @@ -232,6 +232,10 @@ func (b *Bridge) post(ctx context.Context, resource string, body interface{}, ta } defer res.Body.Close() + if target == nil { + return nil + } + return json.NewDecoder(res.Body).Decode(target) } diff --git a/internal/drivers/hue/state.go b/internal/drivers/hue/state.go index 665ede5..03e6ffd 100644 --- a/internal/drivers/hue/state.go +++ b/internal/drivers/hue/state.go @@ -87,11 +87,12 @@ func (s *hueLightState) CheckStaleness(state LightState) { } type hueSensorState struct { - index int - externalID int - uniqueID string - prevData *SensorData - prevTime time.Time + index int + externalID int + uniqueID string + prevData *SensorData + prevTime time.Time + presenceCooldown bool } func (state *hueSensorState) Update(newData SensorData) *models.Event { @@ -113,13 +114,14 @@ func (state *hueSensorState) Update(newData SensorData) *models.Event { if state.prevData != nil && newData.Type != state.prevData.Type { return nil } - if time.Since(stateTime) > time.Second*3 { - return nil - } switch newData.Type { case "ZLLSwitch": { + if time.Since(stateTime) > time.Second*3 { + return nil + } + pe := state.prevData.State.ButtonEvent ce := newData.State.ButtonEvent @@ -153,7 +155,8 @@ func (state *hueSensorState) Update(newData SensorData) *models.Event { if state.prevData != nil && state.prevData.State.Presence != newData.State.Presence { name := models.ENSensorPresenceStarted if !newData.State.Presence { - name = models.ENSensorPresenceEnded + name = models.ENSensorPresenceEnding + state.presenceCooldown = true } return &models.Event{ @@ -163,6 +166,15 @@ func (state *hueSensorState) Update(newData SensorData) *models.Event { "deviceInternalId": newData.UniqueID, }, } + } else if state.presenceCooldown && !newData.State.Presence && time.Since(stateTime) > time.Minute { + state.presenceCooldown = false + return &models.Event{ + Name: models.ENSensorPresenceEnded, + Payload: map[string]string{ + "deviceId": strconv.Itoa(state.externalID), + "deviceInternalId": newData.UniqueID, + }, + } } } } diff --git a/models/event.go b/models/event.go index 22a3f2d..33672be 100644 --- a/models/event.go +++ b/models/event.go @@ -23,8 +23,9 @@ var ( ENBridgeConnected = "BridgeConnected" ENBridgeDisconnected = "BridgeDisconnected" ENButtonPressed = "ButtonPressed" - ENSensorPresenceStarted = "SensorPresenceStart" - ENSensorPresenceEnded = "SensorPresenceEnd" + ENSensorPresenceStarted = "SensorPresenceStarted" + ENSensorPresenceEnding = "SensorPresenceEnding" + ENSensorPresenceEnded = "SensorPresenceEnded" ) func BridgeConnectedEvent(bridge Bridge) Event {