/
device.go
165 lines (141 loc) · 4.34 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package hive
import (
"fmt"
"time"
)
// Device represents a reference to a single device. It can be a light, sensor
// thermostat or anything else.
type Device struct {
entity *jsonEntity
client *Client
}
// Do sends the request to apply the given change to this device.
func (d *Device) Do(c *Change) error {
return d.client.modifyDeviceState(d, &c.state)
}
// ID returns the unique ID of this device.
func (d *Device) ID() string {
return d.entity.ID
}
// Type returns the type of this device.
func (d *Device) Type() string {
return d.entity.Type
}
// Name returns the user-given name of this device, or an empty string if no
// name is given.
func (d *Device) Name() string {
if d.entity.State.Name == nil {
return ""
}
return *d.entity.State.Name
}
// String returns a string representation of this device, containing the ID,
// name and type.
func (d *Device) String() string {
name := d.Name()
if name == "" {
name = "(unnamed)"
}
return fmt.Sprintf("[%s] %s (%s)", d.ID(), name, d.Type())
}
// Created returns the time when this device was added.
func (d *Device) Created() time.Time {
return time.Time(d.entity.Created)
}
// LastSeen returns the time when this device was last online.
func (d *Device) LastSeen() time.Time {
return time.Time(d.entity.LastSeen)
}
// IsOnline returns true if this device is currently powered on and connected,
// false otherwise.
func (d *Device) IsOnline() bool {
return d.entity.Props.Online != nil && *d.entity.Props.Online
}
// Getters specific to motion sensors
// IsMotionSensor checks if this device is a motion sensor.
func (d *Device) IsMotionSensor() bool {
return d.Type() == typeMotionSensor
}
// HasMotion returns true if this device is a motion sensor and is currently
// detecting motion.
func (d *Device) HasMotion() bool {
if d.entity.Props.Motion == nil {
return false
}
return d.entity.Props.Motion.Status
}
// LastMotionStart returns the start time of the last detected motion by this
// device, if it's a motion sensor.
func (d *Device) LastMotionStart() time.Time {
if d.entity.Props.Motion == nil {
return time.Time{}
}
return time.Time(d.entity.Props.Motion.Start)
}
// LastMotionEnd returns the end time of the last detected motion by this
// device, if it's a motion sensor.
func (d *Device) LastMotionEnd() time.Time {
if d.entity.Props.Motion == nil {
return time.Time{}
}
return time.Time(d.entity.Props.Motion.End)
}
// Getters specific to lights
// IsLight returns true if this device is a light bulb of any kind.
func (d *Device) IsLight() bool {
return d.Type() == typeWarmWhiteLight || d.IsColorLight()
}
// IsColorLight returns true if this device is a color light bulb.
func (d *Device) IsColorLight() bool {
return d.Type() == typeColourLight
}
// IsOn returns true if this device is a light bulb and is currently turned on.
func (d *Device) IsOn() bool {
return d.entity.State.Status != nil && *d.entity.State.Status == statusON
}
// Brightness returns the current brightness level of this light bulb, between
// 0 and 100.
func (d *Device) Brightness() int {
if d.entity.State.Brightness == nil {
return 0
}
return *d.entity.State.Brightness
}
// Color returns the current color set on this colored light bulb. Will return
// the last used color if the device is not currently in color mode or turned
// off.
func (d *Device) Color() HSV {
if d.entity.State.Hue == nil || d.entity.State.Saturation == nil || d.entity.State.Value == nil {
return HSV{}
}
return HSV{
*d.entity.State.Hue,
*d.entity.State.Saturation,
*d.entity.State.Value,
}
}
// ColorTemperature returns the current temperature of this colored light bulb
// in kelvins. Will return the last temperature value if the device is off or
// in color mode.
func (d *Device) ColorTemperature() int {
if d.entity.State.ColourTemperature == nil {
return 0
}
return *d.entity.State.ColourTemperature
}
// ColorTemperaturePercent returns the current temperature of this colored light
// bulb in percents between 0 and 100, with 100 being the warmest possible
// setting. Will return the last temperature value if the device is off or in
// color mode.
func (d *Device) ColorTemperaturePercent() int {
if d.entity.State.ColourTemperature == nil {
return 0
}
return temperatureToPercent(d.ColorTemperature())
}
func (d *Device) Mode() string {
if d.entity.State.Mode == nil {
return ""
}
return *d.entity.State.Mode
}