class HueBridge::LightBulp

Represents a light bulp. It provides an interface to turn the light on, off and to toggle it.

Constants

FORBIDDEN_STATS

Attributes

power[R]
state[R]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options LightBulp options @option options [String] :hue_bridge_ip The Hue Bridge's IP @option options [String] :user_id The user id to access the api @option options [Integer] :light_bulp_id The id of the light bulp

# File lib/hue_bridge/light_bulp.rb, line 18
def initialize(options = {})
  @light_bulp_id = options.fetch(:light_bulp_id)
  @user_id = options.fetch(:user_id)
  @ip = options.fetch(:hue_bridge_ip)
end

Public Instance Methods

alert() click to toggle source

Invokes the alert sequence on the light bulp. @return [Boolean] success of the operation

# File lib/hue_bridge/light_bulp.rb, line 55
def alert
  response = put('state', alert: 'lselect')
  response_successful?(response)
end
off() click to toggle source

Turns the light bulp off and returns it's state. @return [Boolean] success of the operation

# 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() click to toggle source

Turns the light bulp on and returns it's state. @return [Boolean] success of the operation

# 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() click to toggle source

Restores the state of the lightbulp. @return [Boolean] success of the operation

# 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 = {}) click to toggle source

Sets the color for the lightbulp. @see Color#initialize @return [Boolean] success of the operation

# 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() click to toggle source

Stores the current state of the lightbulp. @return [Boolean] success of the operation

# 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() click to toggle source

Toggles the light bulp and returns it's state. @return [Boolean] success of the operation

# 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

Private Instance Methods

delete_forbidden_stats() click to toggle source
# File lib/hue_bridge/light_bulp.rb, line 93
def delete_forbidden_stats
  FORBIDDEN_STATS.each do |attr|
    @state.delete(attr)
  end
end
get(resource = '') click to toggle source
# File lib/hue_bridge/light_bulp.rb, line 139
def get(resource = '')
  http.request_get("/#{path}/#{resource}")
end
http() click to toggle source
# File lib/hue_bridge/light_bulp.rb, line 143
def http
  @http ||= Net::HTTP.new(@ip)
end
log_error(msg) click to toggle source
# File lib/hue_bridge/light_bulp.rb, line 99
def log_error(msg)
  $stderr.puts(msg)
end
path() click to toggle source
# File lib/hue_bridge/light_bulp.rb, line 147
def path
  File.join 'api', @user_id, 'lights', @light_bulp_id.to_s
end
put(resource, params) click to toggle source
# File lib/hue_bridge/light_bulp.rb, line 135
def put(resource, params)
  http.request_put("/#{path}/#{resource}", JSON.generate(params))
end
response_successful?(response) click to toggle source
# File lib/hue_bridge/light_bulp.rb, line 119
def response_successful?(response)
  regex = %r((?<state>success|error))
  match = response.body.match(regex) || {}

  case match[:state]
  when nil
    log_error("Don't know how to handle the response")
    log_error(response.body)
    false
  when 'success'
    true
  when 'error'
    false
  end
end
set_power_from_response!(response) click to toggle source
# File lib/hue_bridge/light_bulp.rb, line 103
def set_power_from_response!(response)
  regex = /success.*\/lights\/\d*\/state\/on.*(?<state>true|false)\}\}\]/
  match = response.body.match(regex) || {}

  @power = case match[:state]
           when nil
            log_error('Couldn\'t determin the power state from the response')
            log_error(response.body)
            false
           when 'true'
            true
           when 'false'
            false
           end
end