Class: HueColoredBulb

Inherits:
Object
  • Object
show all
Defined in:
lib/lita/hue_colored_bulb.rb

Overview

Exposes basic on, off, and recolor commands for a single named Hue bulb

on the local network.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'Bloom') ⇒ HueColoredBulb

Note that the initializer only cares about a single named bulb

and does not look around for other bulbs to care about.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lita/hue_colored_bulb.rb', line 11

def initialize(name='Bloom')
  @client = Hue::Client.new

  # Your client likely has multiple bulbs attached, but here you're only
  #   going to want to find a single bulb that matches the supplied name.
  @light = @client.lights.select do |light|
    light.name == name
  end.first

  # No point continuing if the bulb can't be found by name.
  raise ArgumentError if @light.nil?
end

Instance Attribute Details

#clientObject (readonly)

The named light itself and the Hue client object are both worth reusing

as instance variables


26
27
28
# File 'lib/lita/hue_colored_bulb.rb', line 26

def client
  @client
end

#lightObject (readonly)

The named light itself and the Hue client object are both worth reusing

as instance variables


26
27
28
# File 'lib/lita/hue_colored_bulb.rb', line 26

def light
  @light
end

Instance Method Details

#colorsObject



77
78
79
80
81
82
83
84
# File 'lib/lita/hue_colored_bulb.rb', line 77

def colors
  [
    'red', 'orange', 'yellow',           # red is 0
    'chartreuse', 'green', 'aquamarine', # green is 21,000
    'cyan', 'azure', 'blue',             # blue is 44,000
    'violet', 'magenta', 'rose'          # rose is about 60,000
  ]
end

#demo(sleep_seconds = 0.25) ⇒ Object

Fun demo to spin through all named colors, one color every quarter second.



41
42
43
44
45
46
# File 'lib/lita/hue_colored_bulb.rb', line 41

def demo(sleep_seconds=0.25)
  colors.each do |color_name|
    self.set_color color_name
    sleep sleep_seconds
  end
end

#hue_for_color(name) ⇒ Object

START:color_backend RGB color wheel from 0 to 65535:

red is 0 (and 65535 because the wheel starts over at the end)
green is ~21000
blue is ~44000


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lita/hue_colored_bulb.rb', line 102

def hue_for_color(name)
  # green has an index of 4 in the colors array above
  color_index = colors.find_index(name)

  # each color is 65535 / 12 points "wide",
  #   which is 5461 points on this RGB color wheel.
  color_width = (max_color / colors.count).to_i

  # green's hue is thus 4 * 5461 = 21845.
  color_index * color_width
end

#off!Object



36
37
38
# File 'lib/lita/hue_colored_bulb.rb', line 36

def off!
  light.off!
end

#on!Object

START:basics on! and off! methods are passed right through this API. They’re plenty

simple for your purposes as is.


32
33
34
# File 'lib/lita/hue_colored_bulb.rb', line 32

def on!
  light.on!
end

#set_color(name) ⇒ Object

Take a color name like cyan, look up its hue on the 65000 point scale,

and pass that number to the light's hue= method to recolor the light.


88
89
90
91
92
93
94
# File 'lib/lita/hue_colored_bulb.rb', line 88

def set_color(name)
  unless colors.include? name.downcase
    raise ArgumentError.new("I don't know that color!")
  end

  light.hue = hue_for_color(name)
end