Class: HueBridge::LightBulp
- Inherits:
-
Object
- Object
- HueBridge::LightBulp
- Defined in:
- lib/hue_bridge/light_bulp.rb
Overview
Represents a light bulp. It provides an interface to turn the light on, off and to toggle it.
Constant Summary collapse
- FORBIDDEN_STATS =
%w(colormode reachable).freeze
Instance Attribute Summary collapse
-
#power ⇒ Object
readonly
Returns the value of attribute power.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
-
#alert ⇒ Boolean
Invokes the alert sequence on the light bulp.
-
#initialize(options = {}) ⇒ LightBulp
constructor
A new instance of LightBulp.
-
#off ⇒ Boolean
Turns the light bulp off and returns it’s state.
-
#on ⇒ Boolean
Turns the light bulp on and returns it’s state.
-
#restore_state ⇒ Boolean
Restores the state of the lightbulp.
-
#set_color(opts = {}) ⇒ Boolean
Sets the color for the lightbulp.
-
#store_state ⇒ Boolean
Stores the current state of the lightbulp.
-
#toggle ⇒ Boolean
Toggles the light bulp and returns it’s state.
Constructor Details
#initialize(options = {}) ⇒ LightBulp
Returns a new instance of LightBulp.
18 19 20 21 22 |
# File 'lib/hue_bridge/light_bulp.rb', line 18 def initialize( = {}) @light_bulp_id = .fetch(:light_bulp_id) @user_id = .fetch(:user_id) @ip = .fetch(:hue_bridge_ip) end |
Instance Attribute Details
#power ⇒ Object (readonly)
Returns the value of attribute power.
11 12 13 |
# File 'lib/hue_bridge/light_bulp.rb', line 11 def power @power end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
11 12 13 |
# File 'lib/hue_bridge/light_bulp.rb', line 11 def state @state end |
Instance Method Details
#alert ⇒ Boolean
Invokes the alert sequence on the light bulp.
55 56 57 58 |
# File 'lib/hue_bridge/light_bulp.rb', line 55 def alert response = put('state', alert: 'lselect') response_successful?(response) end |
#off ⇒ Boolean
Turns the light bulp off and returns it’s state.
46 47 48 49 50 |
# File 'lib/hue_bridge/light_bulp.rb', line 46 def off response = put('state', on: false) set_power_from_response!(response) response_successful?(response) end |
#on ⇒ Boolean
Turns the light bulp on and returns it’s state.
37 38 39 40 41 |
# File 'lib/hue_bridge/light_bulp.rb', line 37 def on response = put('state', on: true) set_power_from_response!(response) response_successful?(response) end |
#restore_state ⇒ Boolean
Restores the state of the lightbulp.
86 87 88 89 |
# File 'lib/hue_bridge/light_bulp.rb', line 86 def restore_state response = put('state', state) success = !!!(response.body =~ %r(error)) end |
#set_color(opts = {}) ⇒ Boolean
Sets the color for the lightbulp.
64 65 66 67 68 69 |
# File 'lib/hue_bridge/light_bulp.rb', line 64 def set_color(opts = {}) color = Color.new(opts) response = put('state', color.to_h) response_successful?(response) end |
#store_state ⇒ Boolean
Stores the current state of the lightbulp.
74 75 76 77 78 79 80 81 |
# File 'lib/hue_bridge/light_bulp.rb', line 74 def store_state response = get success = !!(response.body =~ %r(state)) data = JSON.parse(response.body) if success @state = data.fetch('state') delete_forbidden_stats success end |
#toggle ⇒ Boolean
Toggles the light bulp and returns it’s state.
27 28 29 30 31 32 |
# File 'lib/hue_bridge/light_bulp.rb', line 27 def toggle @power ||= false response = put('state', on: !@power) set_power_from_response!(response) response_successful?(response) end |