Skip to content

Commit 8b36fd1

Browse files
committed
add a devices list type
1 parent d150be6 commit 8b36fd1

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

colorlight/colorlight.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,7 @@ func (cl *Colorlight) SetColor(color colorful.Color) {
196196
func (cl *Colorlight) GetDeviceUrl() string {
197197
return fmt.Sprintf("http://%s/light/%s", cl.addr, cl.id)
198198
}
199+
200+
func (cl *Colorlight) GetAccessory() *accessory.Accessory {
201+
return cl.accessory.Accessory
202+
}

devices/list.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package devices
2+
3+
import (
4+
"github.com/brutella/hc/accessory"
5+
)
6+
7+
type Device interface {
8+
Init()
9+
GetDeviceUrl() string
10+
GetAccessory() *accessory.Accessory
11+
}
12+
13+
type List []Device
14+
15+
func (l List) GetAccessories() []*accessory.Accessory {
16+
accessories := make([]*accessory.Accessory, len(l))
17+
for i := range l {
18+
accessories[i] = l[i].GetAccessory()
19+
}
20+
21+
return accessories
22+
}

main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/pteich/esphomekit/colorlight"
1010
"github.com/pteich/esphomekit/config"
11+
"github.com/pteich/esphomekit/devices"
1112
"github.com/pteich/esphomekit/sensor"
1213
)
1314

@@ -30,7 +31,7 @@ func main() {
3031
bridge := accessory.NewBridge(accessory.Info{Name: "EspHomekit Bridge", ID: accID})
3132

3233
// every esphome device need to have an accessory in HomeKit and and internal service that takes care of updates
33-
accessories := make([]*accessory.Accessory, 0)
34+
deviceList := make(devices.List, 0)
3435
for _, accConfig := range accConfigs {
3536
accID++
3637
switch accConfig.Type {
@@ -45,7 +46,7 @@ func main() {
4546
})
4647
light := colorlight.New(accConfig.ID, accConfig.Addr, acc, httpClient, log)
4748
light.Init()
48-
accessories = append(accessories, acc.Accessory)
49+
deviceList = append(deviceList, light)
4950

5051
case config.TypeTemperature:
5152
acc := accessory.NewTemperatureSensor(accessory.Info{
@@ -58,17 +59,18 @@ func main() {
5859
}, 25, -15, 85, 0.1)
5960
tempsensor := sensor.NewTemperature(accConfig.ID, accConfig.Addr, acc, httpClient, log)
6061
tempsensor.Init()
61-
accessories = append(accessories, acc.Accessory)
62+
deviceList = append(deviceList, tempsensor)
63+
6264
}
6365
}
6466

65-
log.Info().Int("count", len(accessories)).Msg("add accessories")
67+
log.Info().Int("count", len(deviceList)).Msg("add accessories")
6668
// init HomeKit ip connection with pin
6769
hcConfig := hc.Config{
6870
Pin: cfg.Pin,
6971
}
7072

71-
hcTransport, err := hc.NewIPTransport(hcConfig, bridge.Accessory, accessories...)
73+
hcTransport, err := hc.NewIPTransport(hcConfig, bridge.Accessory, deviceList.GetAccessories()...)
7274
if err != nil {
7375
log.Fatal().Err(err).Msg("error creating transport")
7476
}

sensor/temperature.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ func (t *Temperature) UpdateTemp() {
6464
t.log.Info().Str("name", t.id).Float64("temp", apiResponse.Value).Msg("update temp")
6565
t.accessory.TempSensor.CurrentTemperature.SetValue(apiResponse.Value)
6666
}
67+
68+
func (t *Temperature) GetAccessory() *accessory.Accessory {
69+
return t.accessory.Accessory
70+
}

0 commit comments

Comments
 (0)