forked from 1lann/go-hass
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdevice.go
45 lines (40 loc) · 1.08 KB
/
device.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
package hass
import (
"errors"
"strings"
)
// Device is a generic interface for interacting with devices
type Device interface {
On() error
Off() error
Toggle() error
EntityID() string
Domain() string
}
// GetDevice returns a Device object from an State object
func (a *Access) GetDevice(state State) (Device, error) {
dom := strings.TrimSuffix(strings.SplitAfter(state.EntityID, ".")[0], ".")
switch dom {
case "light":
return a.NewLight(state.EntityID), nil
case "switch":
return a.NewSwitch(state.EntityID), nil
case "lock":
return a.NewLock(state.EntityID), nil
}
return nil, errors.New("Device type not supported yet")
}
// SupportedDeviceTypes returns a list of supported device types
func (a *Access) SupportedDeviceTypes() []string {
return []string{"light", "switch", "lock"}
}
// IsSupportedDevice returns true if an entityID is a supported device
func (a *Access) IsSupportedDevice(id string) bool {
dom := strings.TrimSuffix(strings.SplitAfter(id, ".")[0], ".")
for _, d := range a.SupportedDeviceTypes() {
if dom == d {
return true
}
}
return false
}