-
Notifications
You must be signed in to change notification settings - Fork 25
/
sensor_ht.go
59 lines (50 loc) · 1.38 KB
/
sensor_ht.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package migateway
const (
MODEL_SENSORHT = "sensor_ht"
FIELD_SENSORHT_TEMPERATURE = "temperature"
FIELD_SENSORHT_HUMIDITY = "humidity"
)
type SensorHT struct {
*Device
State SensorHTState
}
type SensorHTState struct {
Temperature float64
Humidity float64
Battery float32
}
type SensorHTStateChange struct {
ID string
From SensorHTState
To SensorHTState
}
func (s SensorHTStateChange) IsChanged() bool {
return s.From.Temperature != s.To.Temperature || s.From.Humidity != s.To.Humidity || s.From.Battery != s.To.Battery
}
func NewSensorHt(dev *Device) *SensorHT {
return &SensorHT{
Device: dev,
State: SensorHTState{
Temperature: dev.GetDataAsFloat64(FIELD_SENSORHT_TEMPERATURE) / 100,
Humidity: dev.GetDataAsFloat64(FIELD_SENSORHT_HUMIDITY) / 100,
Battery: dev.GetBatteryLevel(0),
},
}
}
func (s *SensorHT) Set(dev *Device) {
change := &SensorHTStateChange{ID: s.Sid, From: s.State, To: s.State}
if dev.hasField(FIELD_SENSORHT_TEMPERATURE) {
s.State.Temperature = dev.GetDataAsFloat64(FIELD_SENSORHT_TEMPERATURE) / 100
}
if dev.hasField(FIELD_SENSORHT_HUMIDITY) {
s.State.Humidity = dev.GetDataAsFloat64(FIELD_SENSORHT_HUMIDITY) / 100
}
s.State.Battery = dev.GetBatteryLevel(s.State.Battery)
change.To = s.State
if change.IsChanged() || s.shouldPushUpdates() {
s.Aqara.StateMessages <- change
}
if dev.Token != "" {
s.Token = dev.Token
}
}