class LIFX::LAN::LightCollection

LightCollection represents a collection of {Light}s, which can either refer to all lights on a {NetworkContext}, or lights

Constants

DEFAULT_ALIVE_THRESHOLD

Attributes

context[R]

Refers to {NetworkContext} the instance belongs to @return [NetworkContext]

tag[R]

Tag of the collection. `nil` represents all lights @return [String]

Public Class Methods

new(context: required!(:context), tag: nil) click to toggle source

Creates a {LightCollection} instance. Should not be used directly. @api private @param context: [NetworkContext] NetworkContext this collection belongs to @param tag: [String] Tag

# File lib/lifx/lan/light_collection.rb, line 27
def initialize(context: required!(:context), tag: nil)
  @context = context
  @tag = tag
end

Public Instance Methods

alive(threshold: DEFAULT_ALIVE_THRESHOLD) click to toggle source

@param threshold: The maximum number of seconds a {Light} was last seen to be considered alive @return [Array<Light>] Lights considered alive

# File lib/lifx/lan/light_collection.rb, line 90
def alive(threshold: DEFAULT_ALIVE_THRESHOLD)
  lights.select { |l| l.seconds_since_seen <= threshold }
end
inspect()
Alias for: to_s
lights() click to toggle source

Returns an Array of {Light}s @return [Array<Light>]

# File lib/lifx/lan/light_collection.rb, line 78
def lights
  if tag
    context.all_lights.select { |l| l.tags.include?(tag) }
  else
    context.all_lights
  end
end
send_message(payload, acknowledge: false, at_time: nil) click to toggle source

Queues a {Protocol::Payload} to be sent to bulbs in the collection @param payload [Protocol::Payload] Payload to be sent @param acknowledge: [Boolean] whether recipients should acknowledge message @param at_time: [Integer] Unix epoch in milliseconds to run the payload. Only applicable to certain payload types. @api private @return [LightCollection] self for chaining

# File lib/lifx/lan/light_collection.rb, line 38
def send_message(payload, acknowledge: false, at_time: nil)
  if tag
    context.send_message(target: Target.new(tag: tag), payload: payload, acknowledge: acknowledge, at_time: at_time)
  else
    context.send_message(target: Target.new(broadcast: true), payload: payload, acknowledge: acknowledge, at_time: at_time)
  end
  self
end
stale(threshold: DEFAULT_ALIVE_THRESHOLD) click to toggle source

Returns an Array of {Light}s considered stale @param threshold: The minimum number of seconds since a {Light} was last seen to be considered stale @return [Array<Light>] Lights considered stale

# File lib/lifx/lan/light_collection.rb, line 97
def stale(threshold: DEFAULT_ALIVE_THRESHOLD)
  lights.select { |l| l.seconds_since_seen > threshold }
end
to_s() click to toggle source

Returns a nice string representation of itself @return [String]

# File lib/lifx/lan/light_collection.rb, line 103
def to_s
  %Q{#<#{self.class.name} lights=#{lights}#{tag ? " tag=#{tag}" : ''}>}
end
Also aliased as: inspect
with_id(id) click to toggle source

Returns a {Light} with device id matching `id` @param id [String] Device ID @return [Light]

# File lib/lifx/lan/light_collection.rb, line 50
def with_id(id)
  lights.find { |l| l.id == id}
end
with_label(label) click to toggle source

Returns a {Light} with its label matching `label` @param label [String, Regexp] Label @return [Light]

# File lib/lifx/lan/light_collection.rb, line 57
def with_label(label)
  if label.is_a?(Regexp)
    lights.find { |l| l.label(fetch: false) =~ label }
  else
    lights.find { |l| l.label(fetch: false) == label }
  end
end
with_tag(tag) click to toggle source

Returns a {LightCollection} of {Light}s tagged with `tag` @param tag [String] Tag @return [LightCollection]

# File lib/lifx/lan/light_collection.rb, line 68
def with_tag(tag)
  if context.tags.include?(tag)
    self.class.new(context: context, tag: tag)
  else
    raise TagNotFound.new("No such tag '#{tag}'")
  end
end