From 677d9aa9b1e1b7a2e387ffc9bfc8d18c1c4230a2 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Fri, 4 Jun 2021 09:57:21 +0200 Subject: [PATCH] add support for hue temp sensor, fix saturation not working for hue bulbs. --- internal/drivers/hue/data.go | 1 + internal/drivers/hue/driver.go | 3 +++ internal/drivers/hue/state.go | 19 +++++++++++++++++-- models/device.go | 19 +++++++++++-------- models/event.go | 1 + 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/internal/drivers/hue/data.go b/internal/drivers/hue/data.go index a0c492b..962f4d1 100644 --- a/internal/drivers/hue/data.go +++ b/internal/drivers/hue/data.go @@ -132,6 +132,7 @@ type SensorData struct { ButtonEvent int `json:"buttonevent"` LastUpdated string `json:"lastupdated"` Presence bool `json:"presence"` + Temperature int } `json:"state"` Config struct { On bool `json:"on"` diff --git a/internal/drivers/hue/driver.go b/internal/drivers/hue/driver.go index 485810b..fc327b5 100644 --- a/internal/drivers/hue/driver.go +++ b/internal/drivers/hue/driver.go @@ -266,6 +266,9 @@ func (d *Driver) ListDevices(ctx context.Context, bridge models.Bridge) ([]model case "ZLLPresence": device.Capabilities = append(device.Capabilities, models.DCPresence) device.Icon = "sensor" + case "ZLLTemperature": + device.Capabilities = append(device.Capabilities, models.DCTemperatureSensor) + device.Icon = "thermometer" case "Daylight": continue } diff --git a/internal/drivers/hue/state.go b/internal/drivers/hue/state.go index 03e6ffd..2883b81 100644 --- a/internal/drivers/hue/state.go +++ b/internal/drivers/hue/state.go @@ -31,7 +31,7 @@ func (s *hueLightState) Update(state models.DeviceState) { s.stale = true } - input.Sat = ptrInt(int(state.Color.Hue * 255)) + input.Sat = ptrInt(int(state.Color.Saturation * 255)) if *input.Sat > 254 { *input.Sat = 254 } @@ -118,6 +118,7 @@ func (state *hueSensorState) Update(newData SensorData) *models.Event { switch newData.Type { case "ZLLSwitch": { + // Ignore old events. if time.Since(stateTime) > time.Second*3 { return nil } @@ -151,7 +152,6 @@ func (state *hueSensorState) Update(newData SensorData) *models.Event { } case "ZLLPresence": { - // TODO: Test this! if state.prevData != nil && state.prevData.State.Presence != newData.State.Presence { name := models.ENSensorPresenceStarted if !newData.State.Presence { @@ -177,6 +177,21 @@ func (state *hueSensorState) Update(newData SensorData) *models.Event { } } } + case "ZLLTemperature": + if time.Since(stateTime) > (time.Minute * 15) { + return nil + } + + if !state.prevTime.Equal(stateTime) { + return &models.Event{ + Name: models.ENSensorTemperature, + Payload: map[string]string{ + "temperature": strconv.FormatFloat(float64(newData.State.Temperature)/100, 'f', 2, 64), + "deviceId": strconv.Itoa(state.externalID), + "deviceInternalId": newData.UniqueID, + }, + } + } } return nil diff --git a/models/device.go b/models/device.go index 3347056..e75f891 100644 --- a/models/device.go +++ b/models/device.go @@ -48,13 +48,14 @@ type DeviceRepository interface { } var ( - DCPower DeviceCapability = "Power" - DCColorHS DeviceCapability = "ColorHS" - DCColorKelvin DeviceCapability = "ColorKelvin" - DCButtons DeviceCapability = "Buttons" - DCPresence DeviceCapability = "Presence" - DCIntensity DeviceCapability = "Intensity" - DCTemperature DeviceCapability = "Temperature" + DCPower DeviceCapability = "Power" + DCColorHS DeviceCapability = "ColorHS" + DCColorKelvin DeviceCapability = "ColorKelvin" + DCButtons DeviceCapability = "Buttons" + DCPresence DeviceCapability = "Presence" + DCIntensity DeviceCapability = "Intensity" + DCTemperatureControl DeviceCapability = "TemperatureControl" + DCTemperatureSensor DeviceCapability = "TemperatureSensor" ) var Capabilities = []DeviceCapability{ @@ -62,8 +63,10 @@ var Capabilities = []DeviceCapability{ DCColorHS, DCColorKelvin, DCButtons, + DCPresence, DCIntensity, - DCTemperature, + DCTemperatureControl, + DCTemperatureSensor, } func (d *Device) Validate() error { diff --git a/models/event.go b/models/event.go index 33672be..6101bb0 100644 --- a/models/event.go +++ b/models/event.go @@ -26,6 +26,7 @@ var ( ENSensorPresenceStarted = "SensorPresenceStarted" ENSensorPresenceEnding = "SensorPresenceEnding" ENSensorPresenceEnded = "SensorPresenceEnded" + ENSensorTemperature = "SensorTemperature" ) func BridgeConnectedEvent(bridge Bridge) Event {