Class: HueBridge::LightBulp

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LightBulp

Returns a new instance of LightBulp.

Parameters:

  • options (Hash) (defaults to: {})

    LightBulp options

Options Hash (options):

  • :hue_bridge_ip (String)

    The Hue Bridge’s IP

  • :user_id (String)

    The user id to access the api

  • :light_bulp_id (Integer)

    The id of the light bulp



18
19
20
21
22
# 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

Instance Attribute Details

#powerObject (readonly)

Returns the value of attribute power.



11
12
13
# File 'lib/hue_bridge/light_bulp.rb', line 11

def power
  @power
end

#stateObject (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

#alertBoolean

Invokes the alert sequence on the light bulp.

Returns:

  • (Boolean)

    success of the operation



55
56
57
58
# File 'lib/hue_bridge/light_bulp.rb', line 55

def alert
  response = put('state', alert: 'lselect')
  response_successful?(response)
end

#offBoolean

Turns the light bulp off and returns it’s state.

Returns:

  • (Boolean)

    success of the operation



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

#onBoolean

Turns the light bulp on and returns it’s state.

Returns:

  • (Boolean)

    success of the operation



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_stateBoolean

Restores the state of the lightbulp.

Returns:

  • (Boolean)

    success of the operation



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.

Returns:

  • (Boolean)

    success of the operation

See Also:



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_stateBoolean

Stores the current state of the lightbulp.

Returns:

  • (Boolean)

    success of the operation



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

#toggleBoolean

Toggles the light bulp and returns it’s state.

Returns:

  • (Boolean)

    success of the operation



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