lib/hue/light.rb in hue-0.1.3 vs lib/hue/light.rb in hue-0.1.4
- old
+ new
@@ -1,7 +1,10 @@
module Hue
class Light
+ include TranslateKeys
+ include EditableState
+
HUE_RANGE = 0..65535
SATURATION_RANGE = 0..255
BRIGHTNESS_RANGE = 0..255
COLOR_TEMPERATURE_RANGE = 153..500
@@ -106,26 +109,10 @@
# else
# TODO: Error
end
end
- def on?
- @state['on']
- end
-
- %w{on hue saturation brightness color_temperature alert}.each do |key|
- define_method "#{key}=".to_sym do |value|
- set_state({key.to_sym => value})
- instance_variable_set("@#{key}".to_sym, value)
- end
- end
-
- def set_xy(x, y)
- set_state({:xy => [x, y]})
- @x, @y = x, y
- end
-
# Indicates if a light can be reached by the bridge. Currently
# always returns true, functionality will be added in a future
# patch.
def reachable?
@state['reachable']
@@ -134,11 +121,11 @@
# @param transition The duration of the transition from the light’s current
# state to the new state. This is given as a multiple of 100ms and
# defaults to 4 (400ms). For example, setting transistiontime:10 will
# make the transition last 1 second.
def set_state(attributes, transition = nil)
- body = translate_keys(attributes)
+ body = translate_keys(attributes, STATE_KEYS_MAP)
# Add transition
body.merge!({:transitiontime => transition}) if transition
uri = URI.parse("#{base_url}/state")
@@ -175,31 +162,13 @@
:effect => :effect,
:color_mode => :colormode,
:reachable => :reachable,
}
- def translate_keys(hash)
- new_hash = {}
- hash.each do |key, value|
- new_key = STATE_KEYS_MAP[key.to_sym]
- key = new_key if new_key
- new_hash[key] = value
- end
- new_hash
- end
-
def unpack(hash)
unpack_hash(hash, KEYS_MAP)
unpack_hash(@state, STATE_KEYS_MAP)
@x, @y = @state['xy']
- end
-
- def unpack_hash(hash, map)
- map.each do |local_key, remote_key|
- value = hash[remote_key.to_s]
- next unless value
- instance_variable_set("@#{local_key}", value)
- end
end
def base_url
"http://#{@bridge.ip}/api/#{@client.username}/lights/#{id}"
end