lib/hue/light.rb in hue-0.1.0 vs lib/hue/light.rb in hue-0.1.1

- old
+ new

@@ -6,10 +6,13 @@ COLOR_TEMPERATURE_RANGE = 153..500 # Unique identification number. attr_reader :id + # Bridge the light is associated with + attr_reader :bridge + # A unique, editable name given to the light. attr_accessor :name # Hue of the light. This is a wrapping value between 0 and 65535. # Both 0 and 65535 are red, 25500 is green and 46920 is blue. @@ -76,32 +79,28 @@ attr_reader :software_version # Reserved for future functionality. attr_reader :point_symbol - def initialize(client, id, name) + def initialize(client, bridge, id, hash) @client = client + @bridge = bridge @id = id - @name = name - refresh + unpack(hash) end - def [](index) - lights[index] - end - def name=(new_name) unless (1..32).include?(new_name.length) raise InvalidValueForParameter, 'name must be between 1 and 32 characters.' end body = { :name => new_name } uri = URI.parse(base_url) - http = Net::HTTP.new(uri.hostname) + http = Net::HTTP.new(uri.host) response = http.request_put(uri.path, MultiJson.dump(body)) response = MultiJson.load(response.body).first if response['success'] @name = new_name # else @@ -131,59 +130,79 @@ def reachable? @state['reachable'] end # @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. + # 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) - map = { - :brightness => :bri, - :saturation => :sat, - :color_temperature => :ct, - } + body = translate_keys(attributes) - body = {} - attributes.each do |key, value| - new_key = map[key.to_sym] - key = new_key if new_key - body[key] = value - end - # Add transition body.merge!({:transitiontime => transition}) if transition uri = URI.parse("#{base_url}/state") - http = Net::HTTP.new(uri.hostname) + http = Net::HTTP.new(uri.host) response = http.request_put(uri.path, MultiJson.dump(body)) MultiJson.load(response.body) end # Refresh the state of the lamp def refresh json = MultiJson.load(Net::HTTP.get(URI.parse(base_url))) + unpack(json) + end - @state = json['state'] - @brightness = @state['bri'] - @hue = @state['hue'] - @saturation = @state['sat'] + private + + KEYS_MAP = { + :state => :state, + :type => :type, + :name => :name, + :model => :modelid, + :software_version => :swversion, + :point_symbol => :pointsymbol + } + + STATE_KEYS_MAP = { + :on => :on, + :brightness => :bri, + :hue => :hue, + :saturation => :sat, + :xy => :xy, + :color_temperature => :ct, + :alert => :alert, + :effect => :effect, + :color_mode => :colormode, + :reachable => :reachable, + } + + def translate_keys(hash) + new_hash = {} + hash.each do |key, value| + new_key = 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'] - @color_temperature = @state['ct'] - @alert = @state['alert'].to_sym - @effect = @state['effect'].to_sym - @color_mode = @state['colormode'] - @type = json['type'] - @name = json['name'] - @model = json['modelid'] - @software_version = json['swversion'] - @point_symbol = json['pointsymbol'] end - private + 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 - bridge_ip = @client.base_station['internalipaddress'] - "http://#{bridge_ip}/api/#{@client.username}/lights/#{id}" + "http://#{@bridge.ip}/api/#{@client.username}/lights/#{id}" end end end