Class: Lightscape::Light

Inherits:
Object
  • Object
show all
Defined in:
lib/lightscape/light.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ Light

Returns a new instance of Light.



17
18
19
20
# File 'lib/lightscape/light.rb', line 17

def initialize number
  @client = Lightscape::Client.base["lights/#{number}"]
  @pending_changes = {}
end

Instance Attribute Details

#clientObject (readonly)

The API client interface to the light



11
12
13
# File 'lib/lightscape/light.rb', line 11

def client
  @client
end

#pending_changesObject

A hash of pending changes to the light E.g. { on: true }



15
16
17
# File 'lib/lightscape/light.rb', line 15

def pending_changes
  @pending_changes
end

Class Method Details

.find(number) ⇒ Object



6
7
8
# File 'lib/lightscape/light.rb', line 6

def self.find number
  new(number)
end

Instance Method Details

#brightness=(number) ⇒ Object Also known as: bri=



45
46
47
48
# File 'lib/lightscape/light.rb', line 45

def brightness= number
  self.on = true
  pending_changes[:bri] = [0, [number, 255].min].max
end

#color=(symbol) ⇒ Object



36
37
38
39
# File 'lib/lightscape/light.rb', line 36

def color= symbol
  self.on = true
  pending_changes[:xy] = COLORS[symbol]
end

#off!Object



31
32
33
34
# File 'lib/lightscape/light.rb', line 31

def off!
  self.on = false
  update!
end

#on!Object



26
27
28
29
# File 'lib/lightscape/light.rb', line 26

def on!
  self.on = true
  update!
end

#on=(boolean) ⇒ Object



22
23
24
# File 'lib/lightscape/light.rb', line 22

def on= boolean
  pending_changes[:on] = boolean
end

#transition_time=(duration) ⇒ Object



41
42
43
# File 'lib/lightscape/light.rb', line 41

def transition_time= duration
  pending_changes[:transitiontime] = duration
end

#update!Object



53
54
55
56
57
58
# File 'lib/lightscape/light.rb', line 53

def update!
  response = client["/state"].put pending_changes.to_json
  pending_changes = {}
  raise response.to_str unless response =~ /success/
  true
end