Browse Source

fix hue light/sensor search and add a separate SensorPresenceEnding event.

pull/1/head
Gisle Aune 4 years ago
parent
commit
5cdb4f3cf5
  1. 4
      internal/drivers/hue/bridge.go
  2. 30
      internal/drivers/hue/state.go
  3. 5
      models/event.go

4
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)
}

30
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,
},
}
}
}
}

5
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 {

Loading…
Cancel
Save