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 {