Class: Huey::Bulb
- Inherits:
-
Object
- Object
- Huey::Bulb
- Defined in:
- lib/huey/bulb.rb
Overview
An actual object for a bulb.
Constant Summary collapse
- ATTRIBUTES =
[:on, :bri, :hue, :sat, :xy, :ct, :name, :transitiontime, :colormode, :effect, :reachable, :alert]
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
Instance Method Summary collapse
- #alert! ⇒ Object
-
#initialize(id, hash) ⇒ Bulb
constructor
A new instance of Bulb.
- #reload(hash = nil) ⇒ Object
- #rgb ⇒ Object
- #rgb=(hex) ⇒ Object
- #save ⇒ Object (also: #commit)
- #update(hash) ⇒ Object
Constructor Details
#initialize(id, hash) ⇒ Bulb
Returns a new instance of Bulb.
30 31 32 33 34 35 36 |
# File 'lib/huey/bulb.rb', line 30 def initialize(id, hash) @id = id.to_i @changes = {} @name = hash['name'] reload(hash) end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/huey/bulb.rb', line 8 def id @id end |
Class Method Details
.all ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/huey/bulb.rb', line 10 def self.all return @all if @all @all = Huey::Group.new Huey::Request.get['lights'].collect do |id, hash| @all.bulbs << Bulb.new(id, hash) end @all end |
.find(id) ⇒ Object
20 21 22 |
# File 'lib/huey/bulb.rb', line 20 def self.find(id) self.all.find {|b| b.id == id || b.name.include?(id.to_s)} end |
Instance Method Details
#alert! ⇒ Object
119 120 121 122 |
# File 'lib/huey/bulb.rb', line 119 def alert! self.update(alert: 'none') if self.alert != 'none' self.update(alert: 'select') end |
#reload(hash = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/huey/bulb.rb', line 38 def reload(hash = nil) hash ||= Huey::Request.get("lights/#{self.id}") (Huey::Bulb::ATTRIBUTES - [:name]).each do |attribute| instance_variable_set("@#{attribute}".to_sym, hash['state'][attribute.to_s]) end self end |
#rgb ⇒ Object
74 75 76 |
# File 'lib/huey/bulb.rb', line 74 def rgb Color::HSL.new(self.hue.to_f / 182.04, self.sat.to_f / 255.0 * 100.0, self.bri.to_f / 255.0 * 100.0).to_rgb.html end |
#rgb=(hex) ⇒ Object
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 |
# File 'lib/huey/bulb.rb', line 78 def rgb=(hex) color = Color::RGB.from_html(hex) # Manual calcuation is necessary here because of an error in the Color library r = color.r g = color.g b = color.b max = [r, g, b].max min = [r, g, b].min delta = max - min v = max * 100 if (max != 0.0) s = delta / max * 100 else s = 0.0 end if (s == 0.0) h = 0.0 else if (r == max) h = (g - b) / delta elsif (g == max) h = 2 + (b - r) / delta elsif (b == max) h = 4 + (r - g) / delta end h *= 60.0 if (h < 0) h += 360.0 end end self.hue = (h * 182.04).round self.sat = (s / 100.0 * 255.0).round self.bri = (v / 100.0 * 255.0).round end |
#save ⇒ Object Also known as: commit
61 62 63 64 65 |
# File 'lib/huey/bulb.rb', line 61 def save Huey::Request.put("lights/#{self.id}/state", body: MultiJson.dump(@changes)) @changes = {} true end |
#update(hash) ⇒ Object
68 69 70 71 72 |
# File 'lib/huey/bulb.rb', line 68 def update(hash) hash.each { |k, v| self.send("#{k}=".to_sym, v) } save end |