-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
85 lines (72 loc) · 2.65 KB
/
main.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
package main
import (
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
"github.com/pteich/go-timeout-httpclient"
"github.com/pteich/logger"
"github.com/pteich/esphomekit/colorlight"
"github.com/pteich/esphomekit/config"
"github.com/pteich/esphomekit/devices"
"github.com/pteich/esphomekit/sensor"
)
func main() {
// read command line flags and init logger
cfg := config.New()
log := logger.New(logger.WithLogLevel(cfg.LogLevel), logger.WithLogConsole(cfg.LogConsole))
httpClient := timeouthttp.New(timeouthttp.WithTimeout(15))
// read existing esphome devices config from file
accConfigs, err := config.NewEsphomeDevicesFromFile(cfg.ConfigFile)
if err != nil {
log.Fatal().Err(err).Str("file", cfg.ConfigFile).Msg("could not load accessories config")
}
log.Info().Interface("config", cfg).Interface("accessories", accConfigs).Msg("starting esphomekit")
// add bridge
var accID uint64 = 1
bridge := accessory.NewBridge(accessory.Info{Name: "EspHomekit Bridge", ID: accID})
// every esphome device need to have an accessory in HomeKit and and internal service that takes care of updates
deviceList := make(devices.List, 0)
for _, accConfig := range accConfigs {
accID++
switch accConfig.Type {
case config.TypeColorLight:
acc := accessory.NewColoredLightbulb(accessory.Info{
ID: accID,
Name: accConfig.Name,
Manufacturer: accConfig.Manufacturer,
SerialNumber: accConfig.SerialNumber,
FirmwareRevision: accConfig.FirmwareRevision,
Model: accConfig.Model,
})
light := colorlight.New(accConfig.ID, accConfig.Addr, acc, httpClient, log)
light.Init()
deviceList = append(deviceList, light)
case config.TypeTemperature:
acc := accessory.NewTemperatureSensor(accessory.Info{
ID: accID,
Name: accConfig.Name,
Manufacturer: accConfig.Manufacturer,
SerialNumber: accConfig.SerialNumber,
FirmwareRevision: accConfig.FirmwareRevision,
Model: accConfig.Model,
}, 25, -15, 85, 0.1)
tempsensor := sensor.NewTemperature(accConfig.ID, accConfig.Addr, acc, httpClient, log)
tempsensor.Init()
deviceList = append(deviceList, tempsensor)
}
}
log.Info().Int("count", len(deviceList)).Msg("add accessories")
// init HomeKit ip connection with pin
hcConfig := hc.Config{
Pin: cfg.Pin,
StoragePath: cfg.StoragePath,
}
hcTransport, err := hc.NewIPTransport(hcConfig, bridge.Accessory, deviceList.GetAccessories()...)
if err != nil {
log.Fatal().Err(err).Msg("error creating transport")
}
hc.OnTermination(func() {
<-hcTransport.Stop()
})
// start HomeKit connection
hcTransport.Start()
}