hue 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32b4e4137e61f92f9e4a2033f4b01f5abf57a197
4
- data.tar.gz: fcd37ed79cb42411e10ac9bc84b4802390b54d5e
3
+ metadata.gz: 3c06483132e27c548f6636b9717d0fb1d7d3fb52
4
+ data.tar.gz: 4fe57bd0ecb43a9a208adcb8cdef6b490b0f813e
5
5
  SHA512:
6
- metadata.gz: 7c72e22fdd17c1065ae118474c03a6aaf9efe2d1cca17b7f27620e2727949b74808795cb729fb2851a1c21ea6e4117f3bb0641940c6a78f954970866cae3b3b2
7
- data.tar.gz: 6b5ab557a8a00e8ea23fa1f485134ad963052e8b517ed2b969c4895f8adab9ab8b621332997362835f64466064fa88b35acd4204aa5e03849245af3a3510f240
6
+ metadata.gz: 79d882703ab4d35a2792e8b62711941cb8ff88b33a5a444c31f11c450fe1d5ef0e9f713d55dadbf36666155f63f312351f8220570e375c70c2399c5a4d0b2b13
7
+ data.tar.gz: 2fa2d565f33b0a7464a906da5eebbae52f220baca044436f7c0f2e55009a461227cbe4d133bbf7226f0264fea1e3c428d9bbd10a23240115a1121415eea3ff0b
data/Readme.markdown CHANGED
@@ -4,19 +4,46 @@ Work with Philips Hue light bulbs from Ruby.
4
4
 
5
5
  ## Installation
6
6
 
7
- This gem is currently unreleased. For now, simply clone the repository.
7
+ Add this line to your application's Gemfile:
8
+
9
+ ``` ruby
10
+ gem 'hue'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ``` shell
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ``` shell
22
+ $ gem install hue
23
+ ```
8
24
 
9
25
  ## Usage
10
26
 
27
+ The first time you use it, it will automatically create a user for you. Doing this requires you to have pushed the button on your bridge in the last 30 seconds. If you haven't it will throw an exception and let you know you need to push the button. Simply press the button and run the command again.
28
+
29
+ From CLI:
30
+
11
31
  ``` shell
12
- $ git clone https://github.com/soffes/hue.git
13
- $ irb -Ihue/lib -rhue
32
+ $ hue all on
33
+ $ hue all off
34
+ $ hue light 2 on
35
+ $ hue light 2 --brightness 20
14
36
  ```
15
37
 
38
+ From Ruby:
39
+
16
40
  ``` ruby
17
41
  > client = Hue::Client.new
18
42
  > light = client.lights.first
19
43
  > light.on = true
44
+ > light.hue = 46920
45
+ > light.color_temperature = 100
46
+ > light.set_state({:color_temperature => 400, :transition => 100})
20
47
  ```
21
48
 
22
49
  ## Contributing
data/Todo.markdown ADDED
@@ -0,0 +1,11 @@
1
+ # To Do
2
+
3
+ * RGB translation
4
+ * Hex translation
5
+ * Groups
6
+ * Scheduling
7
+ * Effects
8
+ * User management
9
+ * Configuration
10
+ * More efficient calls
11
+ * Local UPNP
data/lib/hue/cli.rb CHANGED
@@ -17,6 +17,19 @@ module Hue
17
17
  end
18
18
  end
19
19
 
20
+ desc 'light ID STATE', 'Access a light'
21
+ option :hue, :type => :numeric
22
+ option :saturation, :type => :numeric
23
+ option :brightness, :type => :numeric
24
+ def light(id, state = nil)
25
+ light = client.light(id)
26
+ puts light.name
27
+
28
+ body = options.dup
29
+ body[:on] = state unless state.nil?
30
+ light.set_state(body) if body.length > 0
31
+ end
32
+
20
33
  private
21
34
 
22
35
  def client
data/lib/hue/client.rb CHANGED
@@ -36,6 +36,10 @@ module Hue
36
36
  end
37
37
  end
38
38
 
39
+ def light(id)
40
+ self.lights.select { |l| l.id == id }.first
41
+ end
42
+
39
43
  private
40
44
 
41
45
  def validate_user
data/lib/hue/light.rb CHANGED
@@ -1,11 +1,81 @@
1
1
  module Hue
2
2
  class Light
3
+ HUE_RANGE = 0..65535
4
+ SATURATION_RANGE = 0..255
5
+ BRIGHTNESS_RANGE = 0..255
6
+ COLOR_TEMPERATURE_RANGE = 153..500
7
+
8
+ # Unique identification number.
3
9
  attr_reader :id
4
- attr_reader :name
5
- attr_reader :state
10
+
11
+ # A unique, editable name given to the light.
12
+ attr_accessor :name
13
+
14
+ # Hue of the light. This is a wrapping value between 0 and 65535.
15
+ # Both 0 and 65535 are red, 25500 is green and 46920 is blue.
16
+ attr_accessor :hue
17
+
18
+ # Saturation of the light. 255 is the most saturated (colored)
19
+ # and 0 is the least saturated (white).
20
+ attr_accessor :saturation
21
+
22
+ # Brightness of the light. This is a scale from the minimum
23
+ # brightness the light is capable of, 0, to the maximum capable
24
+ # brightness, 255. Note a brightness of 0 is not off.
25
+ attr_accessor :brightness
26
+
27
+ # The x coordinate of a color in CIE color space. Between 0 and 1.
28
+ #
29
+ # @see http://developers.meethue.com/coreconcepts.html#color_gets_more_complicated
30
+ attr_reader :x
31
+
32
+ # The y coordinate of a color in CIE color space. Between 0 and 1.
33
+ #
34
+ # @see http://developers.meethue.com/coreconcepts.html#color_gets_more_complicated
35
+ attr_reader :y
36
+
37
+ # The Mired Color temperature of the light. 2012 connected lights
38
+ # are capable of 153 (6500K) to 500 (2000K).
39
+ #
40
+ # @see http://en.wikipedia.org/wiki/Mired
41
+ attr_accessor :color_temperature
42
+
43
+ # The alert effect, which is a temporary change to the bulb’s state.
44
+ # This can take one of the following values:
45
+ # * `none` – The light is not performing an alert effect.
46
+ # * `select` – The light is performing one breathe cycle.
47
+ # * `lselect` – The light is performing breathe cycles for 30 seconds
48
+ # or until an "alert": "none" command is received.
49
+ #
50
+ # Note that in version 1.0 this contains the last alert sent to the
51
+ # light and not its current state. This will be changed to contain the
52
+ # current state in an upcoming patch.
53
+ #
54
+ # @see http://developers.meethue.com/coreconcepts.html#some_extra_fun_stuff
55
+ attr_accessor :alert
56
+
57
+ # The dynamic effect of the light, can either be `none` or
58
+ # `colorloop`. If set to colorloop, the light will cycle through
59
+ # all hues using the current brightness and saturation settings.
60
+ attr_accessor :effect
61
+
62
+ # Indicates the color mode in which the light is working, this is
63
+ # the last command type it received. Values are `hs` for Hue and
64
+ # Saturation, `xy` for XY and `ct` for Color Temperature. This
65
+ # parameter is only present when the light supports at least one
66
+ # of the values.
67
+ attr_reader :color_mode
68
+
69
+ # A fixed name describing the type of light.
6
70
  attr_reader :type
71
+
72
+ # The hardware model of the light.
7
73
  attr_reader :model
74
+
75
+ # An identifier for the software version running on the light.
8
76
  attr_reader :software_version
77
+
78
+ # Reserved for future functionality.
9
79
  attr_reader :point_symbol
10
80
 
11
81
  def initialize(client, id, name)
@@ -15,52 +85,105 @@ module Hue
15
85
  refresh
16
86
  end
17
87
 
88
+ def [](index)
89
+ lights[index]
90
+ end
91
+
92
+ def name=(new_name)
93
+ unless (1..32).include?(new_name.length)
94
+ raise InvalidValueForParameter, 'name must be between 1 and 32 characters.'
95
+ end
96
+
97
+ body = {
98
+ :name => new_name
99
+ }
100
+
101
+ uri = URI.parse(base_url)
102
+ http = Net::HTTP.new(uri.hostname)
103
+ response = http.request_put(uri.path, MultiJson.dump(body))
104
+ response = MultiJson.load(response.body).first
105
+ if response['success']
106
+ @name = new_name
107
+ # else
108
+ # TODO: Error
109
+ end
110
+ end
111
+
18
112
  def on?
19
113
  @state['on']
20
114
  end
21
115
 
22
- def on=(new_state)
23
- self.set_state(new_state)
116
+ %w{on hue saturation brightness color_temperature}.each do |key|
117
+ define_method "#{key}=".to_sym do |value|
118
+ set_state({key.to_sym => value})
119
+ instance_variable_set("@#{key}".to_sym, value)
120
+ end
24
121
  end
25
122
 
26
- def hsb
27
- @state.select { |k, v| %w{sat bri hue}.include?(k) }
123
+ def set_xy(x, y)
124
+ set_state({:xy => [x, y]})
125
+ @x, @y = x, y
28
126
  end
29
127
 
30
- def set_state(on, hue = nil, saturation = nil, brightness = nil)
31
- body = {
32
- on: on
128
+ # Indicates if a light can be reached by the bridge. Currently
129
+ # always returns true, functionality will be added in a future
130
+ # patch.
131
+ def reachable?
132
+ @state['reachable']
133
+ end
134
+
135
+ # @param transition The duration of the transition from the light’s current
136
+ # state to the new state. This is given as a multiple of 100ms and defaults
137
+ # to 4 (400ms). For example, setting transistiontime:10 will make the
138
+ # transition last 1 second.
139
+ def set_state(attributes, transition = nil)
140
+ map = {
141
+ :brightness => :bri,
142
+ :saturation => :sat,
143
+ :color_temperature => :ct,
33
144
  }
34
145
 
35
- if on
36
- body.merge!({
37
- hue: hue,
38
- sat: saturation,
39
- bri: brightness
40
- })
146
+ body = {}
147
+ attributes.each do |key, value|
148
+ new_key = map[key.to_sym]
149
+ key = new_key if new_key
150
+ body[key] = value
41
151
  end
42
152
 
43
- bridge_ip = @client.base_station['internalipaddress']
44
- uri = URI.parse("http://#{bridge_ip}/api/#{@client.username}/lights/#{self.id}/state")
153
+ # Add transition
154
+ body.merge!({:transitiontime => transition}) if transition
45
155
 
156
+ uri = URI.parse("#{base_url}/state")
46
157
  http = Net::HTTP.new(uri.hostname)
47
158
  response = http.request_put(uri.path, MultiJson.dump(body))
48
159
  MultiJson.load(response.body)
49
160
  end
50
161
 
51
- private
52
-
162
+ # Refresh the state of the lamp
53
163
  def refresh
54
- bridge_ip = @client.base_station['internalipaddress']
55
- uri = URI.parse("http://#{bridge_ip}/api/#{@client.username}/lights/#{self.id}")
56
- json = MultiJson.load(Net::HTTP.get(uri))
164
+ json = MultiJson.load(Net::HTTP.get(URI.parse(base_url)))
57
165
 
58
166
  @state = json['state']
167
+ @brightness = @state['bri']
168
+ @hue = @state['hue']
169
+ @saturation = @state['sat']
170
+ @x, @y = @state['xy']
171
+ @color_temperature = @state['ct']
172
+ @alert = @state['alert'].to_sym
173
+ @effect = @state['effect'].to_sym
174
+ @color_mode = @state['colormode']
59
175
  @type = json['type']
60
176
  @name = json['name']
61
177
  @model = json['modelid']
62
178
  @software_version = json['swversion']
63
179
  @point_symbol = json['pointsymbol']
64
180
  end
181
+
182
+ private
183
+
184
+ def base_url
185
+ bridge_ip = @client.base_station['internalipaddress']
186
+ "http://#{bridge_ip}/api/#{@client.username}/lights/#{id}"
187
+ end
65
188
  end
66
189
  end
data/lib/hue/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hue
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Soffes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-19 00:00:00.000000000 Z
11
+ date: 2013-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -52,6 +52,7 @@ files:
52
52
  - LICENSE
53
53
  - Rakefile
54
54
  - Readme.markdown
55
+ - Todo.markdown
55
56
  - bin/hue
56
57
  - hue.gemspec
57
58
  - lib/hue.rb
@@ -80,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
81
  version: '0'
81
82
  requirements: []
82
83
  rubyforge_project:
83
- rubygems_version: 2.0.0
84
+ rubygems_version: 2.0.3
84
85
  signing_key:
85
86
  specification_version: 4
86
87
  summary: Work with Philips Hue light bulbs from Ruby.