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.
24 25 26 27 28 29 30 |
# File 'lib/huey/bulb.rb', line 24 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 |
# File 'lib/huey/bulb.rb', line 10 def self.all @all ||= Huey::Request.get['lights'].collect do |id, hash| Bulb.new(id, hash) end end |
.find(id) ⇒ Object
16 17 18 |
# File 'lib/huey/bulb.rb', line 16 def self.find(id) self.all.find {|b| b.id == id || b.name.include?(id.to_s)} end |
.find_all(id) ⇒ Object
20 21 22 |
# File 'lib/huey/bulb.rb', line 20 def self.find_all(id) self.all.select {|b| b.id == id || b.name.include?(id.to_s)} end |
Instance Method Details
#alert! ⇒ Object
113 114 115 116 |
# File 'lib/huey/bulb.rb', line 113 def alert! self.update(alert: 'none') if self.alert != 'none' self.update(alert: 'select') end |
#reload(hash = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/huey/bulb.rb', line 32 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
68 69 70 |
# File 'lib/huey/bulb.rb', line 68 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
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 |
# File 'lib/huey/bulb.rb', line 72 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
55 56 57 58 59 |
# File 'lib/huey/bulb.rb', line 55 def save Huey::Request.put("lights/#{self.id}/state", body: MultiJson.dump(@changes)) @changes = {} true end |
#update(hash) ⇒ Object
62 63 64 65 66 |
# File 'lib/huey/bulb.rb', line 62 def update(hash) hash.each { |k, v| self.send("#{k}=".to_sym, v) } save end |