Module: LIFX::LightTarget

Included in:
Light, LightCollection
Defined in:
lib/lifx/light_target.rb

Overview

LightTarget is a module that contains Light commands that can work with either a single Light or multiple Lights via a LightCollection

Constant Summary collapse

MSEC_PER_SEC =
1000
NSEC_IN_SEC =
1_000_000_000

Instance Method Summary collapse

Instance Method Details

#refreshLight, LightCollection

Requests light(s) to report their state This method cannot guarantee the message was received.

Returns:



179
180
181
182
# File 'lib/lifx/light_target.rb', line 179

def refresh
  send_message(Protocol::Light::Get.new)
  self
end

#set_color(color, duration: LIFX::Config.default_duration) ⇒ Light, LightCollection

Attempts to set the color of the light(s) to color asynchronously. This method cannot guarantee that the message was received.

Parameters:

  • color (Color)

    The color to be set

  • duration: (Numeric) (defaults to: LIFX::Config.default_duration)

    Transition time in seconds

Returns:



12
13
14
15
16
17
18
19
# File 'lib/lifx/light_target.rb', line 12

def set_color(color, duration: LIFX::Config.default_duration)
  send_message(Protocol::Light::Set.new(
    color: color.to_hsbk,
    duration: (duration * MSEC_PER_SEC).to_i,
    stream: 0,
  ))
  self
end

#set_power(state) ⇒ Light, LightCollection

Attempts to set the power state to state asynchronously. This method cannot guarantee the message was received.

Parameters:

  • state (:on, :off)

Returns:



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/lifx/light_target.rb', line 149

def set_power(state)
  level = case state
  when :on
    1
  when :off
    0
  else
    raise ArgumentError.new("Must pass in either :on or :off")
  end
  send_message(Protocol::Device::SetPower.new(level: level))
  self
end

#turn_offLight, LightCollection

Attempts to turn the light(s) off asynchronously. This method cannot guarantee the message was received.

Returns:



172
173
174
# File 'lib/lifx/light_target.rb', line 172

def turn_off
  set_power(:off)
end

#turn_onLight, LightCollection

Attempts to turn the light(s) on asynchronously. This method cannot guarantee the message was received.

Returns:



165
166
167
# File 'lib/lifx/light_target.rb', line 165

def turn_on
  set_power(:on)
end