philips_hue 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,7 +4,7 @@ This is a library for accessing and controlling your [Philips Hue](http://www.me
4
4
 
5
5
  #### TL;DR:
6
6
 
7
- [Check out the bin/ directory for examples](https://github.com/dmerrick/lights_app/tree/master/bin) of how to use this project and what kind of things you can do.
7
+ Install with `gem install philips_hue`. [Check out the bin/ directory for examples](https://github.com/dmerrick/lights_app/tree/master/bin) of how to use this project and what kind of things you can do.
8
8
 
9
9
 
10
10
  ### Registering with the Bridge
data/bin/flash.rb CHANGED
@@ -34,10 +34,10 @@ OptionParser.new do |opts|
34
34
  opts.on("-n [number]", Integer, "Repeat [number] times") do |num|
35
35
  options.repeat = num
36
36
  end
37
- opts.on("--app [app_name]", "The name of the registered app") do |app|
37
+ opts.on("-a [app_name]","--app [app_name]", "The name of the registered app") do |app|
38
38
  options.app_name = app
39
39
  end
40
- opts.on("--api [url]", "The address of the Hue hub") do |api|
40
+ opts.on("-b [addr]","--bridge [addr]", "The address of the Hue bridge") do |api|
41
41
  options.api_url = api
42
42
  end
43
43
  end.parse!
data/bin/register.rb ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+ # example: register.rb --bridge 192.168.1.14 --app lightsapp
3
+
4
+ # this is a great place for a new user to start
5
+ # if you see "Success!", you're ready to start hacking
6
+
7
+ # any time you use a new app name, you will have to reregister
8
+
9
+ require 'rubygems'
10
+ require 'optparse'
11
+ require 'philips_hue'
12
+
13
+ # parse the command line options
14
+ options = {}
15
+ OptionParser.new do |opts|
16
+ opts.banner = "Usage: register.rb [options]"
17
+ opts.on("-a [app_name]","--app [app_name]", "The name of the app to register") do |app|
18
+ options[:app_name] = app
19
+ end
20
+ opts.on("-b [addr]","--bridge [addr]", "The address of the Hue bridge") do |api|
21
+ options[:api_url] = api
22
+ end
23
+ end.parse!
24
+
25
+ # register the app with the hue bridge
26
+ # get ready to press the link button!
27
+ hue = PhilipsHue::Bridge.register(options[:app_name], options[:api_url])
28
+
29
+ # it worked!
30
+ # print some friendly info
31
+ puts
32
+ puts "Success!"
33
+ puts hue
34
+ puts hue.lights.first
@@ -9,11 +9,10 @@ module PhilipsHue
9
9
  @app_name = app_name
10
10
  @key = Digest::MD5.hexdigest(@app_name)
11
11
  @api_endpoint = "http://#{api_url}/api"
12
- @lights = add_all_lights
13
12
  end
14
13
 
15
14
  # provide getter methods for these variables
16
- attr_reader :app_name, :key, :api_endpoint, :lights
15
+ attr_reader :app_name, :key, :api_endpoint
17
16
 
18
17
  # returns overall system status as JSON
19
18
  def overview
@@ -26,26 +25,43 @@ module PhilipsHue
26
25
  Light.new(light_name, light_id, @api_endpoint, @key)
27
26
  end
28
27
 
28
+ # return the lights array or add them it if needed
29
+ def lights
30
+ @lights ||= add_all_lights
31
+ end
32
+
29
33
  # helper method to get light by light_id
30
34
  def light(light_id)
31
35
  @lights[light_id.to_i-1]
32
36
  end
33
37
 
34
38
  # registers your app with the Hue
35
- # this must be run for every unique app name
36
- # FIXME: this needs to be more adequately tested
37
- def register!
39
+ # this must be run for every new app name
40
+ def self.register(app_name, api_url)
38
41
  puts "Press the link button on the Hue..."
39
- sleep 10
40
- json_body = {:username => @key, :devicetype => @app_name}.to_json
41
- response = HTTParty.post(@api_endpoint, :body => json_body)
42
-
43
- if response.first["error"] # should be response.code
44
- puts "Press link button and try again."
45
- exit
46
- else
47
- return response
42
+
43
+ # pretty countdown
44
+ 10.downto(1) do |n|
45
+ print n
46
+ sleep 1
47
+ print n == 1 ? "\r#{' '*30}\r" : ", "
48
48
  end
49
+
50
+ # create a new bridge and submit a special POST request
51
+ new_bridge = self.new(app_name, api_url)
52
+ json_body = { :username => new_bridge.key, :devicetype => new_bridge.app_name}.to_json
53
+ response = HTTParty.post(new_bridge.api_endpoint, :body => json_body)
54
+
55
+ # quit if it didn't work (link button wasn't pressed)
56
+ abort "Press link button and try again." if response.first["error"]
57
+
58
+ # return the now-registered bridge
59
+ new_bridge
60
+ end
61
+
62
+ # human-readable bridge summary
63
+ def to_s
64
+ "#{app_name}: #{api_endpoint}"
49
65
  end
50
66
 
51
67
  private
@@ -1,3 +1,3 @@
1
1
  module PhilipsHue
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/philips_hue.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'digest/md5'
2
2
  require 'httparty'
3
+ require 'json'
3
4
 
4
5
  require 'philips_hue/version'
5
6
  require 'philips_hue/bridge'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philips_hue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: 0.10.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '1.6'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '1.6'
30
46
  description: A library to control and query Philips Hue lights
31
47
  email: dana.merrick@gmail.com
32
48
  executables: []
@@ -42,6 +58,7 @@ files:
42
58
  - bin/all_on.rb
43
59
  - bin/flash.rb
44
60
  - bin/light_show.rb
61
+ - bin/register.rb
45
62
  - bin/sandbox.rb
46
63
  - bin/test.rb
47
64
  - README.md