Class: LIFX::LAN::LightCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, LightTarget, RequiredKeywordArguments
Defined in:
lib/lifx/lan/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::LAN::LightTarget::MSEC_PER_SEC, LIFX::LAN::LightTarget::NSEC_IN_SEC

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequiredKeywordArguments

#required!

Methods included from LightTarget

#half_sine, #pulse, #refresh, #saw, #set_color, #set_multizone_color, #set_power, #set_tile_colors, #sine, #tile_info, #triangle, #turn_off, #turn_on, #zone_count

Instance Attribute Details

#contextNetworkContext (readonly)

Refers to NetworkContext the instance belongs to

Returns:



17
18
19
# File 'lib/lifx/lan/light_collection.rb', line 17

def context
  @context
end

#tagString (readonly)

Tag of the collection. nil represents all lights

Returns:

  • (String)


21
22
23
# File 'lib/lifx/lan/light_collection.rb', line 21

def tag
  @tag
end

Instance Method Details

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

Returns an Array of LIFX::LAN::Lights considered alive

Parameters:

  • threshold: (defaults to: DEFAULT_ALIVE_THRESHOLD)

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

Returns:

  • (Array<Light>)

    Lights considered alive



90
91
92
# File 'lib/lifx/lan/light_collection.rb', line 90

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

#lightsArray<Light>

Returns an Array of LIFX::LAN::Lights

Returns:



78
79
80
81
82
83
84
# 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

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

Returns an Array of LIFX::LAN::Lights considered stale

Parameters:

  • threshold: (defaults to: DEFAULT_ALIVE_THRESHOLD)

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

Returns:

  • (Array<Light>)

    Lights considered stale



97
98
99
# 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_sString Also known as: inspect

Returns a nice string representation of itself

Returns:

  • (String)


103
104
105
# File 'lib/lifx/lan/light_collection.rb', line 103

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

#with_id(id) ⇒ Light

Returns a LIFX::LAN::Light with device id matching id

Parameters:

  • id (String)

    Device ID

Returns:



50
51
52
# File 'lib/lifx/lan/light_collection.rb', line 50

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

#with_label(label) ⇒ Light

Returns a LIFX::LAN::Light with its label matching label

Parameters:

  • label (String, Regexp)

    Label

Returns:



57
58
59
60
61
62
63
# 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) ⇒ LightCollection

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

Parameters:

  • tag (String)

    Tag

Returns:



68
69
70
71
72
73
74
# 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