Class: NLHue::Group

Inherits:
Target show all
Defined in:
lib/nlhue/group.rb

Overview

A class representing a designated group of lights on a Hue bridge. Recommended use is to call NLHue::Bridge#groups.

Instance Attribute Summary

Attributes inherited from Target

#bridge, #id, #name, #transitiontime, #type

Instance Method Summary collapse

Methods inherited from Target

#alert, #alert!, #alert=, #bri, #bri=, #clear_alert, #colormode, #ct, #ct=, #defer, #effect, #effect=, #hue, #hue=, #nodefer, #off!, #on!, #on=, #on?, #sat, #sat=, #send_changes, #submit, #to_h, #to_json, #update, #x, #x=, #xy, #xy=, #y, #y=

Constructor Details

#initialize(bridge, id, info = nil) ⇒ Group

bridge - The Bridge that controls this light. id - The group’s ID (integer >= 0). info - The Hash parsed from the JSON description of the group, if available. The group’s lights will be unknown until the JSON from the bridge (/api//groups/) is passed here or to #handle_json.



14
15
16
17
18
# File 'lib/nlhue/group.rb', line 14

def initialize(bridge, id, info=nil)
	@lights ||= Set.new

	super bridge, id, info, :groups, 'action'
end

Instance Method Details

#handle_json(info) ⇒ Object

Updates this Group with data parsed from the Hue bridge.



21
22
23
24
25
26
27
28
# File 'lib/nlhue/group.rb', line 21

def handle_json(info)
	super info

	# A group returns no lights for a short time after creation
	if @info['lights'].is_a?(Array) && !@info['lights'].empty?
		@lights.replace @info['lights'].map(&:to_i)
	end
end

#light_idsObject

An array containing the IDs of the lights belonging to this group.



44
45
46
# File 'lib/nlhue/group.rb', line 44

def light_ids
	@lights.to_a
end

#lightsObject

Returns an array containing this group’s corresponding Light objects from the Bridge.



37
38
39
40
# File 'lib/nlhue/group.rb', line 37

def lights
	lights = @bridge.lights
	@lights.map{|id| lights[id]}
end

#recall_scene(scene) ⇒ Object

Recalls scene, which may be a Scene object or a scene ID String, on this group only. Any raw ID String will not be verified. If NLHue::Target#defer was called, the scene will not be recalled until #submit is called.



73
74
75
76
77
# File 'lib/nlhue/group.rb', line 73

def recall_scene(scene)
	scene = scene.id if scene.is_a?(Scene)
	raise 'Scene ID to recall must be a String' unless scene.is_a?(String)
	set('scene' => scene)
end

#stateObject

Returns a Hash containing the group’s info and most recently set state, with symbolized key names and hue scaled to 0..360. Example:

:id => 0,
:name => 'Lightset 0',
:type => 'LightGroup',
:lights => [0, 1, 2],
:on => false,
:bri => 220,
:ct => 500,
:x => 0.5,
:y => 0.5,
:hue => 193.5,
:sat => 255,
:colormode => 'hs'



65
66
67
# File 'lib/nlhue/group.rb', line 65

def state
	{lights: light_ids}.merge!(super)
end

#to_sObject

“Group: [ID]: [name] ([num] lights)”



31
32
33
# File 'lib/nlhue/group.rb', line 31

def to_s
	"Group: #{@id}: #{@name} (#{@lights.length} lights}"
end