Class: BulbState

Inherits:
Object
  • Object
show all
Defined in:
lib/lights/bulbstate.rb

Defined Under Namespace

Modules: Alert, ColorMode, Effect

Constant Summary collapse

MAX_CT =
500
MIN_CT =
153
MAX_BRI =
255
MIN_BRI =
0
MAX_SAT =
255
MIN_SAT =
0
MAX_HUE =
65535
MIN_HUE =
0
MIN_TRANSITION_TIME =
0
MAX_XY =
1.0
MIN_XY =
0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ BulbState

Returns a new instance of BulbState.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lights/bulbstate.rb', line 36

def initialize( data = {} ) 
  data = {} if data == nil 
  @on = data["on"] 
  set_bri data["bri"]
  set_hue data["hue"]
  set_sat data["sat"]
  set_xy data["xy"] 
  set_ct data["ct"]
  set_alert data["alert"] 
  set_effect data["effect"] 
  set_color_mode data["colormode"] 
  @reachable = data["reachable"] 
  set_transition_time data["transitiontime"]
end

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



32
33
34
# File 'lib/lights/bulbstate.rb', line 32

def alert
  @alert
end

#briObject

Returns the value of attribute bri.



32
33
34
# File 'lib/lights/bulbstate.rb', line 32

def bri
  @bri
end

#color_modeObject

Returns the value of attribute color_mode.



32
33
34
# File 'lib/lights/bulbstate.rb', line 32

def color_mode
  @color_mode
end

#ctObject

Returns the value of attribute ct.



32
33
34
# File 'lib/lights/bulbstate.rb', line 32

def ct
  @ct
end

#effectObject

Returns the value of attribute effect.



32
33
34
# File 'lib/lights/bulbstate.rb', line 32

def effect
  @effect
end

#hueObject

Returns the value of attribute hue.



32
33
34
# File 'lib/lights/bulbstate.rb', line 32

def hue
  @hue
end

#onObject

Returns the value of attribute on.



32
33
34
# File 'lib/lights/bulbstate.rb', line 32

def on
  @on
end

#reachableObject (readonly)

Returns the value of attribute reachable.



32
33
34
# File 'lib/lights/bulbstate.rb', line 32

def reachable
  @reachable
end

#satObject

Returns the value of attribute sat.



32
33
34
# File 'lib/lights/bulbstate.rb', line 32

def sat
  @sat
end

#transition_timeObject

Returns the value of attribute transition_time.



32
33
34
# File 'lib/lights/bulbstate.rb', line 32

def transition_time
  @transition_time
end

#xyObject

Returns the value of attribute xy.



32
33
34
# File 'lib/lights/bulbstate.rb', line 32

def xy
  @xy
end

Instance Method Details

#dataObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/lights/bulbstate.rb', line 163

def data 
  data = {} 
  data["on"] = @on if (@on!=nil) 
  data["bri"] = @bri if @bri 
  data["hue"] = @hue if @hue 
  data["sat"] = @sat if @sat 
  data["xy"] = @xy if @xy 
  data["ct"] = @ct if @ct 
  data["alert"] = @alert if @alert 
  data["effect"] = @effect if @effect 
  data["colormode"] = @color_mode if @color_mode 
  data["reachable"] = @reachable if @reachable 
  data["transitiontime"] = @transition_time if @transition_time
  data 
end

#set_alert(value) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/lights/bulbstate.rb', line 63

def set_alert(value)
  if value.nil? || value == Alert::NONE \
      || value == Alert::SELECT \
      || value == Alert::LSELECT
    @alert = value
  else
    raise BulbStateValueTypeException, "Value has incorrect type. Requires 'none', 'select', or 'lselect'"
  end
end

#set_bri(value) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/lights/bulbstate.rb', line 93

def set_bri(value)
  if value.nil? || value.between?(MIN_BRI,MAX_BRI)
    @bri = value
  else
    raise BulbStateValueOutOfRangeException, "Value out of range. Must be [#{MIN_BRI},#{MAX_BRI}]"
  end
end

#set_color_mode(value) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/lights/bulbstate.rb', line 52

def set_color_mode(value)
  if value.nil? || value == ColorMode::XY \
      || value == ColorMode::HS \
      || value == ColorMode::CT
    @color_mode = value
  else
    raise BulbStateValueTypeException, "Value has incorrect type. Requires 'hs', 'xy', or 'ct'"
  end
end

#set_ct(value) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/lights/bulbstate.rb', line 102

def set_ct(value)
  if !value.nil? && (!value.is_a? Integer)
    raise BulbStateValueTypeException, "Value has incorrect type. Requires integer, got #{value.class}"
  elsif value.nil? || value.between?(MIN_CT,MAX_CT)
    @ct = value
  else
    raise BulbStateValueOutOfRangeException, "Value out of range. Must be [#{MIN_CT},#{MAX_CT}]"
  end
end

#set_effect(value) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/lights/bulbstate.rb', line 74

def set_effect(value)
  if value.nil? || value == Effect::NONE || value == Effect::COLORLOOP
    @effect = value
  else
    raise BulbStateValueTypeException, "Value has incorrect type. Requires 'none' or 'colorloop'"
  end
end

#set_hue(value) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/lights/bulbstate.rb', line 124

def set_hue(value)
  if !value.nil? && (!value.is_a? Integer)
    raise BulbStateValueTypeException, "Value has incorrect type. Requires integer, got #{value.class}"
  elsif value.nil? || value.between?(MIN_HUE,MAX_HUE)
    @hue = value
  else
    raise BulbStateValueOutOfRangeException, "Value out of range. Must be [#{MIN_HUE},#{MAX_HUE}]"
  end
end

#set_on(value) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/lights/bulbstate.rb', line 83

def set_on(value)
  # Tests if value is boolean
  if !!value == value
    @on = value
  else
    raise BulbStateValueTypeException, "Value has incorrect type. Requires boolean, got #{value.class}"
  end
end

#set_sat(value) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/lights/bulbstate.rb', line 113

def set_sat(value)
  if !value.nil? && (!value.is_a? Integer)
    raise BulbStateValueTypeException, "Value has incorrect type. Requires integer, got #{value.class}"
  elsif value.nil? || value.between?(MIN_SAT,MAX_SAT)
    @sat = value
  else
    raise BulbStateValueOutOfRangeException, "Value out of range. Must be [#{MIN_SAT},#{MAX_SAT}]"
  end
end

#set_transition_time(value) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/lights/bulbstate.rb', line 135

def set_transition_time(value)
  if !value.nil? && (!value.is_a? Numeric)
    raise BulbStateValueTypeException, "Value has incorrect type. Requires decimal, got #{value.class}"
  elsif value.nil? || value >= MIN_TRANSITION_TIME
    @transition_time = value
  else
    raise BulbStateValueOutOfRangeException, "Value out of range. Must be > #{MIN_TRANSITION_TIME}"
  end
end

#set_xy(value) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/lights/bulbstate.rb', line 146

def set_xy(value)
  if !value.nil? && (!value.is_a? Array)
    raise BulbStateValueTypeException, "Value has incorrect type. Requires array, got #{value.class}"
  elsif value.nil?
    return
  elsif value.length == 2 && value[0].to_f.is_a?(Numeric) \
          && value[1].to_f.is_a?(Numeric) &&  value[0].to_f >= MIN_XY \
          && value[0].to_f <= MAX_XY && value[1].to_f >= MIN_XY \
          && value[1].to_f <= MAX_XY
    @xy = []
    @xy[0] = value[0].to_f
    @xy[1] = value[1].to_f
  else
    raise BulbStateValueOutOfRangeException, "Value out of range. Must be [#{MIN_XY},#{MAX_XY}]"
  end
end

#to_jsonObject



179
180
181
# File 'lib/lights/bulbstate.rb', line 179

def to_json
  data.to_json
end