-
Notifications
You must be signed in to change notification settings - Fork 12
/
mdns.go
47 lines (39 loc) · 1007 Bytes
/
mdns.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
package main
import (
"log"
"net"
"github.com/grandcat/zeroconf"
"github.com/rs/xid"
)
func publishHomeAssistant() {
var err error
unique := xid.New()
hostURL := "http://" + getOutboundIP().String() + ":8123"
params := []string{
"location_name=Home Assistant",
"uuid=",
"version=0.0.0",
"external_url=",
"internal_url=" + hostURL,
"base_url=" + hostURL,
// Always needs authentication
"requires_api_password=True",
}
log.Printf("Publish %s to _home-assistant._tcp", hostURL)
mdns, err = zeroconf.Register("homeassistant-"+unique.String(), "_home-assistant._tcp", "local.", 8123, params, nil)
if err != nil {
log.Fatal(err)
}
}
// Get preferred outbound ip of this machine
// https://stackoverflow.com/a/37382208
// Can be removed after Supervisor v248 with new lookup
func getOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}