lib/hue/light.rb in hue-0.3.0 vs lib/hue/light.rb in hue-0.3.1

- old
+ new

@@ -1,35 +1,32 @@ 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 # 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 + attr_reader :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. attr_reader :hue - # Saturation of the light. 255 is the most saturated (colored) + # Saturation of the light. 254 is the most saturated (colored) # and 0 is the least saturated (white). attr_reader :saturation # Brightness of the light. This is a scale from the minimum # brightness the light is capable of, 0, to the maximum capable - # brightness, 255. Note a brightness of 0 is not off. + # brightness, 254. Note a brightness of 0 is not off. attr_reader :brightness # The x coordinate of a color in CIE color space. Between 0 and 1. # # @see http://developers.meethue.com/coreconcepts.html#color_gets_more_complicated @@ -82,53 +79,62 @@ attr_reader :software_version # Reserved for future functionality. attr_reader :point_symbol + # The unique ID of the light. + attr_reader :uid + + # The hash of capabilities of the light + attr_reader :capabilities + + # The config hash + attr_reader :config + def initialize(client, bridge, id, hash) @client = client @bridge = bridge @id = id unpack(hash) end def name=(new_name) - unless (1..32).include?(new_name.length) - raise InvalidValueForParameter, 'name must be between 1 and 32 characters.' + unless (1..32).cover?(new_name.length) + raise InvalidValueForParameter, "name must be between 1 and 32 characters." end body = { - :name => new_name + name: new_name } uri = URI.parse(base_url) http = Net::HTTP.new(uri.host) response = http.request_put(uri.path, JSON.dump(body)) response = JSON(response.body).first - if response['success'] + if response["success"] @name = new_name - # else + # else # TODO: Error end 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'] + @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. def set_state(attributes, transition = nil) body = translate_keys(attributes, STATE_KEYS_MAP) # Add transition - body.merge!({:transitiontime => transition}) if transition + body[:transitiontime] = transition if transition uri = URI.parse("#{base_url}/state") http = Net::HTTP.new(uri.host) response = http.request_put(uri.path, JSON.dump(body)) JSON(response.body) @@ -138,37 +144,40 @@ def refresh json = JSON(Net::HTTP.get(URI.parse(base_url))) unpack(json) end - private + private KEYS_MAP = { - :state => :state, - :type => :type, - :name => :name, - :model => :modelid, - :software_version => :swversion, - :point_symbol => :pointsymbol + state: :state, + type: :type, + name: :name, + model: :modelid, + software_version: :swversion, + point_symbol: :pointsymbol, + uid: :uniqueid, + capabilities: :capabilities, + config: :config } 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, + on: :on, + brightness: :bri, + hue: :hue, + saturation: :sat, + xy: :xy, + color_temperature: :ct, + alert: :alert, + effect: :effect, + color_mode: :colormode, + reachable: :reachable } def unpack(hash) unpack_hash(hash, KEYS_MAP) unpack_hash(@state, STATE_KEYS_MAP) - @x, @y = @state['xy'] + @x, @y = @state["xy"] end def base_url "http://#{@bridge.ip}/api/#{@client.username}/lights/#{id}" end