-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessory.go
136 lines (113 loc) · 3.72 KB
/
accessory.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package hksensibo
import (
"github.com/brutella/hc/accessory"
"github.com/brutella/hc/characteristic"
"github.com/brutella/hc/log"
"github.com/brutella/hc/service"
"github.com/llun/hksensibo/actions"
"github.com/llun/sensibo-golang"
ba "github.com/llun/hkbridge/accessories"
"net"
"time"
)
type Sensibo struct {
*accessory.Accessory
Thermostat *service.Thermostat
HumiditySensor *service.HumiditySensor
state sensibo.AcState
measurement sensibo.Measurement
worker *ba.Worker
pollingCh <-chan time.Time
pod sensibo.Pod
api *sensibo.Sensibo
}
func (s *Sensibo) PollingState() {
s.worker.AddAction(actions.NewGetAcState(s.api, s.pod, s))
s.worker.AddAction(actions.NewGetMeasurement(s.api, s.pod, s))
for range s.pollingCh {
s.worker.AddAction(actions.NewGetAcState(s.api, s.pod, s))
s.worker.AddAction(actions.NewGetMeasurement(s.api, s.pod, s))
}
}
func (s *Sensibo) UpdateAcState(state sensibo.AcState) {
s.state = state
targetState := characteristic.TargetHeatingCoolingStateOff
currentState := characteristic.CurrentHeatingCoolingStateOff
if state.On {
switch state.Mode {
case DRY_MODE:
targetState = characteristic.TargetHeatingCoolingStateHeat
currentState = characteristic.CurrentHeatingCoolingStateHeat
case COOL_MODE:
targetState = characteristic.TargetHeatingCoolingStateCool
currentState = characteristic.TargetHeatingCoolingStateCool
default:
targetState = characteristic.TargetHeatingCoolingStateAuto
currentState = characteristic.TargetHeatingCoolingStateCool
}
}
s.Thermostat.CurrentHeatingCoolingState.UpdateValue(currentState)
s.Thermostat.TargetHeatingCoolingState.UpdateValue(targetState)
s.Thermostat.TargetTemperature.UpdateValue(float64(state.TargetTemperature))
}
func (s *Sensibo) UpdateMeasurement(measurement sensibo.Measurement) {
s.measurement = measurement
s.Thermostat.CurrentTemperature.UpdateValue(measurement.Temperature)
s.HumiditySensor.CurrentRelativeHumidity.UpdateValue(measurement.Humidity)
}
func (s *Sensibo) CurrentAcState() sensibo.AcState {
return s.state
}
func (s *Sensibo) CurrentMeasurement() sensibo.Measurement {
return s.measurement
}
func NewSensibo(pod sensibo.Pod, api *sensibo.Sensibo, worker *ba.Worker) *Sensibo {
info := accessory.Info{
Name: "Sensibo",
Manufacturer: "Sensibo",
SerialNumber: pod.ID,
Model: pod.Room.Name,
}
acc := Sensibo{
Thermostat: service.NewThermostat(),
HumiditySensor: service.NewHumiditySensor(),
pollingCh: time.Tick(60 * time.Second),
worker: worker,
api: api,
pod: pod,
}
acc.Accessory = accessory.New(info, accessory.TypeThermostat)
acc.AddService(acc.Thermostat.Service)
acc.AddService(acc.HumiditySensor.Service)
acc.Thermostat.TargetTemperature.OnValueRemoteUpdate(acc.onTargetTemperatureUpdate)
acc.Thermostat.TargetHeatingCoolingState.OnValueRemoteUpdate(acc.onHeatingCoolingStateUpdate)
go acc.PollingState()
return &acc
}
func Lookup(key string, worker *ba.Worker) []*Sensibo {
api := sensibo.NewSensibo(key)
pods, err := api.GetPods()
if err != nil {
log.Info.Fatal(err)
return nil
}
var services []*Sensibo = make([]*Sensibo, len(pods))
for index, pod := range pods {
services[index] = NewSensibo(pod, api, worker)
}
return services
}
func AllAccessories(config ba.AccessoryConfig, iface *net.Interface, worker *ba.Worker) []*accessory.Accessory {
option := config.Option
key, ok := option["key"].(string)
if !ok {
log.Info.Println("Cannot read sensibo key")
return nil
}
sensibos := Lookup(key, worker)
sensiboAccessories := make([]*accessory.Accessory, len(sensibos))
for idx, sensibo := range sensibos {
sensiboAccessories[idx] = sensibo.Accessory
}
return sensiboAccessories
}