Class: LIFX::LightCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, LightTarget
Defined in:
lib/lifx/light_collection.rb

Overview

LightCollection represents a collection of Lights, which can either refer to all lights on a NetworkContext, or lights

Defined Under Namespace

Classes: TagNotFound

Constant Summary collapse

DEFAULT_ALIVE_THRESHOLD =

seconds

30

Constants included from LightTarget

LIFX::LightTarget::MSEC_PER_SEC, LIFX::LightTarget::NSEC_IN_SEC

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LightTarget

#half_sine, #pulse, #reboot!, #refresh, #saw, #set_color, #set_power, #set_site_id, #set_time, #set_waveform, #sine, #triangle, #turn_off, #turn_on

Constructor Details

#initialize(context:, tag: nil) ⇒ LightCollection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a LIFX::LightCollection instance. Should not be used directly.

Parameters:

  • context: (NetworkContext)

    NetworkContext this collection belongs to

  • tag: (String) (defaults to: nil)

    Tag



25
26
27
28
# File 'lib/lifx/light_collection.rb', line 25

def initialize(context:, tag: nil)
  @context = context
  @tag = tag
end

Instance Attribute Details

#contextNetworkContext (readonly)

Refers to NetworkContext the instance belongs to

Returns:



15
16
17
# File 'lib/lifx/light_collection.rb', line 15

def context
  @context
end

#tagString (readonly)

Tag of the collection. nil represents all lights

Returns:

  • (String)


19
20
21
# File 'lib/lifx/light_collection.rb', line 19

def tag
  @tag
end

Instance Method Details

#alive(threshold: DEFAULT_ALIVE_THRESHOLD) ⇒ Array<Light>

Returns an Array of LIFX::Lights considered alive

Parameters:

  • threshold: (defaults to: DEFAULT_ALIVE_THRESHOLD)

    The maximum number of seconds a LIFX::Light was last seen to be considered alive

Returns:

  • (Array<Light>)

    Lights considered alive



87
88
89
# File 'lib/lifx/light_collection.rb', line 87

def alive(threshold: DEFAULT_ALIVE_THRESHOLD)
  lights.select { |l| l.seconds_since_seen <= threshold }
end

#lightsArray<Light>

Returns an Array of LIFX::Lights

Returns:



75
76
77
78
79
80
81
# File 'lib/lifx/light_collection.rb', line 75

def lights
  if tag
    context.all_lights.select { |l| l.tags.include?(tag) }
  else
    context.all_lights
  end
end

#send_message(payload, acknowledge: false) ⇒ LightCollection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Queues a Protocol::Payload to be sent to bulbs in the collection

Parameters:

  • payload (Protocol::Payload)

    Payload to be sent

  • acknowledge: (Boolean) (defaults to: false)

    whether recipients should acknowledge message

Returns:



35
36
37
38
39
40
41
42
# File 'lib/lifx/light_collection.rb', line 35

def send_message(payload, acknowledge: false)
  if tag
    context.send_message(target: Target.new(tag: tag), payload: payload, acknowledge: acknowledge)
  else
    context.send_message(target: Target.new(broadcast: true), payload: payload, acknowledge: acknowledge)
  end
  self
end

#stale(threshold: DEFAULT_ALIVE_THRESHOLD) ⇒ Array<Light>

Returns an Array of LIFX::Lights considered stale

Parameters:

  • threshold: (defaults to: DEFAULT_ALIVE_THRESHOLD)

    The minimum number of seconds since a LIFX::Light was last seen to be considered stale

Returns:

  • (Array<Light>)

    Lights considered stale



94
95
96
# File 'lib/lifx/light_collection.rb', line 94

def stale(threshold: DEFAULT_ALIVE_THRESHOLD)
  lights.select { |l| l.seconds_since_seen > threshold }
end

#to_sString Also known as: inspect

Returns a nice string representation of itself

Returns:

  • (String)


100
101
102
# File 'lib/lifx/light_collection.rb', line 100

def to_s
  %Q{#<#{self.class.name} lights=#{lights}#{tag ? " tag=#{tag}" : ''}>}
end

#with_id(id) ⇒ Light

Returns a LIFX::Light with device id matching id

Parameters:

  • id (String)

    Device ID

Returns:



47
48
49
# File 'lib/lifx/light_collection.rb', line 47

def with_id(id)
  lights.find { |l| l.id == id}
end

#with_label(label) ⇒ Light

Returns a LIFX::Light with its label matching label

Parameters:

  • label (String, Regexp)

    Label

Returns:



54
55
56
57
58
59
60
# File 'lib/lifx/light_collection.rb', line 54

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) ⇒ LightCollection

Returns a LIFX::LightCollection of LIFX::Lights tagged with tag

Parameters:

  • tag (String)

    Tag

Returns:



65
66
67
68
69
70
71
# File 'lib/lifx/light_collection.rb', line 65

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