Class: NLHue::Scene

Inherits:
Object
  • Object
show all
Defined in:
lib/nlhue/scene.rb

Overview

Represents a preset scene on a Hue bridge, with a list of included lights. The actual values recalled by the scene are not available. TODO: Support creating new scenes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bridge, id, info) ⇒ Scene

bridge - The Bridge that contains this scene. id - The scene’s ID (a String). info - A Hash with scene info from the bridge.



14
15
16
17
18
19
20
21
# File 'lib/nlhue/scene.rb', line 14

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

	handle_json(info)
end

Instance Attribute Details

#bridgeObject (readonly)

Returns the value of attribute bridge.



9
10
11
# File 'lib/nlhue/scene.rb', line 9

def bridge
  @bridge
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/nlhue/scene.rb', line 9

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/nlhue/scene.rb', line 9

def name
  @name
end

Instance Method Details

#handle_json(info) ⇒ Object

Updates this scene with any changes from the bridge.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nlhue/scene.rb', line 24

def handle_json(info)
	raise "Scene info must be a Hash" unless info.is_a?(Hash)

	info['id'] = @id

	@name = info['name'] || @name

	if info['lights'].is_a?(Array) && !info['lights'].empty?
		@lights.replace info['lights'].map(&:to_i)
	end

	@info = info
end

#light_idsObject

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



72
73
74
# File 'lib/nlhue/scene.rb', line 72

def light_ids
	@lights.to_a
end

#lightsObject

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



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

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

#recall(&block) ⇒ Object

Recalls this scene on the next RequestQueue tick using group 0 (all lights). The block, if given, will be called after the scene is recalled.



79
80
81
# File 'lib/nlhue/scene.rb', line 79

def recall(&block)
	@bridge.add_target self, &block
end

#send_changes(&block) ⇒ Object

Recalls this scene immediately using NLHue::Bridge#put_api.



84
85
86
# File 'lib/nlhue/scene.rb', line 84

def send_changes(&block)
	@bridge.put_api '/groups/0/action', {scene: @id}.to_json, :groups, &block
end

#stateObject

Returns a Hash containing basic information about the scene.



50
51
52
53
54
55
56
# File 'lib/nlhue/scene.rb', line 50

def state
	{
		id: @id,
		name: @name,
		lights: light_ids,
	}
end

#to_hObject

Returns a copy of the last received info from the bridge, plus the scene’s ID.



40
41
42
# File 'lib/nlhue/scene.rb', line 40

def to_h
	@info.clone
end

#to_json(*args) ⇒ Object

Converts the Hash returned by #state to JSON.



59
60
61
# File 'lib/nlhue/scene.rb', line 59

def to_json(*args)
	state.to_json(*args)
end

#to_sObject

Returns a description of this scene.



45
46
47
# File 'lib/nlhue/scene.rb', line 45

def to_s
	"Scene #{@id}: #{@name} (#{@lights.count} lights)"
end