huey 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -38,7 +38,9 @@ The first time you issue any Huey command, you're likely to see something like t
38
38
  Huey::Errors::PressLinkButton: 'Press the link button and try your request again'
39
39
  ```
40
40
 
41
- Just like the message says, go press the link button on your Hue hub, and then reissue the request. It should work the second time. Then you can get to the exciting stuff:
41
+ Just like the message says, go press the link button on your Hue hub, and then reissue the request. It should work the second time. Then you can get to the exciting stuff.
42
+
43
+ ### Bulbs
42
44
 
43
45
  ```ruby
44
46
  Huey::Bulb.all # Returns an array of your bulbs
@@ -78,6 +80,32 @@ I've added in some convenience attributes as well:
78
80
 
79
81
  - **rgb**: An HTML hex value. Will automatically convert to hue/saturation.
80
82
 
83
+ ### Groups
84
+
85
+ You can also simply and sensibly create groups of bulbs.
86
+
87
+ ```ruby
88
+ Huey::Group.new('Living Room') # Contains all bulbs that have 'Living Room' in their name
89
+ Huey::Group.new('Living Room', 'Foyer') # All bulbs that have either 'Living Room' or 'Foyer' in their name
90
+ g = Huey::Group.new(Huey::Bulb.find(1), Huey::Bulb.find(3)) # A group specifically containing bulbs 1 and 3
91
+ g.name = 'My Bulbs' # Name your group to find it later
92
+
93
+ Huey::Group.import('groups.yml') # Import many groups at once from a YAML file.
94
+ # The file should look like this:
95
+ #
96
+ # living_room: [TV, Living Room - Fireplace]
97
+ # foyer: [Foyer]
98
+ # bedroom: [Bedroom]
99
+
100
+ group = Huey::Group.find('My Bulbs')
101
+
102
+ group.bri = 200
103
+ group.on = true
104
+ group.save # All changes you've made are committed to all the bulbs in a group
105
+
106
+ group.update(bri: 200, ct: 500) # Set and save in one step
107
+ ```
108
+
81
109
  ## Attribution
82
110
 
83
111
  The structure of the configuration files and the modules are taken from [Mongoid](https://github.com/mongoid/mongoid), which had some really great ideas.
data/lib/huey.rb CHANGED
@@ -4,6 +4,7 @@ require 'eventmachine'
4
4
  require 'logger'
5
5
  require 'httparty'
6
6
  require 'color'
7
+ require 'yaml'
7
8
 
8
9
  require 'huey/version'
9
10
 
@@ -12,6 +13,7 @@ require 'huey/errors'
12
13
  require 'huey/ssdp'
13
14
  require 'huey/request'
14
15
  require 'huey/bulb'
16
+ require 'huey/group'
15
17
 
16
18
  module Huey
17
19
  extend self
data/lib/huey/bulb.rb CHANGED
@@ -14,7 +14,11 @@ module Huey
14
14
  end
15
15
 
16
16
  def self.find(id)
17
- self.all.find {|b| b.id == id || b.name == id}
17
+ self.all.find {|b| b.id == id || b.name.include?(id)}
18
+ end
19
+
20
+ def self.find_all(id)
21
+ self.all.select {|b| b.name.include?(id)}
18
22
  end
19
23
 
20
24
  def initialize(id, hash)
data/lib/huey/group.rb ADDED
@@ -0,0 +1,70 @@
1
+ # encoding: utf-8
2
+
3
+ module Huey
4
+
5
+ # A group is a collection of bulbs.
6
+ class Group
7
+ Attributes = Huey::Bulb::Attributes - [:name] + [:rgb]
8
+ attr_accessor :bulbs, :name
9
+
10
+ def self.import(file)
11
+ hash = YAML.load_file(file)
12
+
13
+ hash.each do |key, value|
14
+ g = Huey::Group.new(value)
15
+ g.name = key
16
+ end
17
+ Huey::Group.all
18
+ end
19
+
20
+ def self.all
21
+ @all ||= []
22
+ end
23
+
24
+ def self.find(name)
25
+ Huey::Group.all.find {|g| g.name == name}
26
+ end
27
+
28
+ def initialize(*string_or_array)
29
+ string_or_array = string_or_array.first if string_or_array.first.is_a?(Array)
30
+
31
+ self.bulbs = if string_or_array.first.is_a?(Bulb)
32
+ string_or_array
33
+ else
34
+ string_or_array.collect {|s| Huey::Bulb.find_all(s)}.flatten.uniq
35
+ end
36
+
37
+ @attributes_to_write = {}
38
+ Huey::Group.all << self
39
+ self
40
+ end
41
+
42
+ Huey::Group::Attributes.each do |attribute|
43
+ define_method("#{attribute}=".to_sym) do |new_value|
44
+ @attributes_to_write[attribute] = new_value
45
+ end
46
+ end
47
+
48
+ def save
49
+ response = bulbs.collect {|b| b.update(@attributes_to_write)}
50
+ @attributes_to_write = {}
51
+ response
52
+ end
53
+ alias :commit :save
54
+
55
+ def update(attrs)
56
+ bulbs.collect {|b| b.update(attrs)}
57
+ end
58
+
59
+ def method_missing(meth, *args, &block)
60
+ if !self.bulbs.empty? && self.bulbs.first.respond_to?(meth)
61
+ h = {}
62
+ bulbs.each {|b| h[b.id] = b.send(meth, *args, &block)}
63
+ h
64
+ else
65
+ super
66
+ end
67
+ end
68
+ end
69
+
70
+ end
data/lib/huey/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Huey
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,3 @@
1
+ living_room: [TV, Living Room - Fireplace]
2
+ foyer: [Foyer]
3
+ bedroom: [Bedroom]
data/test/test_helper.rb CHANGED
@@ -8,7 +8,7 @@ class Test::Unit::TestCase
8
8
  def setup
9
9
  super
10
10
  # Prevent real connections to the hub
11
- # EM.stubs(:open_datagram_socket).returns(fake_searcher)
11
+ EM.stubs(:open_datagram_socket).returns(fake_searcher)
12
12
  end
13
13
 
14
14
  def set_hue_ip(ip)
@@ -22,4 +22,14 @@ class Test::Unit::TestCase
22
22
  searcher.stubs(:discovery_responses).returns(responses)
23
23
  searcher
24
24
  end
25
+
26
+ def light_response(id = "1", name = "Living Room")
27
+ {id => {"state"=>{"on"=>false, "bri"=>127, "hue"=>54418, "sat"=>158, "xy"=>[0.509, 0.4149], "ct"=>459, "alert"=>"none", "effect"=>"none", "colormode"=>"hue", "reachable"=>true}, "type"=>"Extended color light", "name"=>name, "modelid"=>"LCT001", "swversion"=>"65003148", "pointsymbol"=>{"1"=>"none", "2"=>"none", "3"=>"none", "4"=>"none", "5"=>"none", "6"=>"none", "7"=>"none", "8"=>"none"}}}
28
+ end
29
+
30
+ def init_bulb(id = "1", name = "Living Room")
31
+ light = light_response(id, name)
32
+ Huey::Bulb.new(light.keys.first, light.values.first)
33
+ end
34
+
25
35
  end
@@ -76,12 +76,4 @@ class BulbTest < Test::Unit::TestCase
76
76
  assert_equal 245, @bulb.bri
77
77
  end
78
78
 
79
- def light_response(id = "1", name = "Living Room")
80
- {id => {"state"=>{"on"=>false, "bri"=>127, "hue"=>54418, "sat"=>158, "xy"=>[0.509, 0.4149], "ct"=>459, "alert"=>"none", "effect"=>"none", "colormode"=>"hue", "reachable"=>true}, "type"=>"Extended color light", "name"=>name, "modelid"=>"LCT001", "swversion"=>"65003148", "pointsymbol"=>{"1"=>"none", "2"=>"none", "3"=>"none", "4"=>"none", "5"=>"none", "6"=>"none", "7"=>"none", "8"=>"none"}}}
81
- end
82
-
83
- def init_bulb
84
- Huey::Bulb.new(light_response.keys.first, light_response.values.last)
85
- end
86
-
87
79
  end
@@ -0,0 +1,88 @@
1
+ require 'test_helper'
2
+
3
+ class GroupTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ super
7
+ @bulb1 = init_bulb('1', 'Living Room - TV')
8
+ @bulb2 = init_bulb('2', 'Living Room - Fireplace')
9
+ @bulb3 = init_bulb('3', 'Foyer')
10
+ @bulb4 = init_bulb('4', 'Bedroom')
11
+
12
+ Huey::Bulb.stubs(:all).returns([@bulb1, @bulb2, @bulb3, @bulb4])
13
+ end
14
+
15
+ def test_initializes_group_from_bulbs
16
+ @group = Huey::Group.new(@bulb1, @bulb2, @bulb3)
17
+
18
+ assert_equal [@bulb1, @bulb2, @bulb3], @group.bulbs
19
+ end
20
+
21
+ def test_initializes_group_from_name
22
+ @group = Huey::Group.new('Living Room')
23
+
24
+ assert_equal [@bulb1, @bulb2], @group.bulbs
25
+ end
26
+
27
+ def test_initialzes_group_from_names
28
+ @group = Huey::Group.new('Living Room', 'Foyer')
29
+
30
+ assert_equal [@bulb1, @bulb2, @bulb3], @group.bulbs
31
+ end
32
+
33
+ def test_initializes_groups_from_yml
34
+ Huey::Group.instance_variable_set(:@all, [])
35
+
36
+ Huey::Group.import('test/fixtures/group.yml')
37
+
38
+ assert_equal 3, Huey::Group.all.count
39
+ assert_equal [@bulb1, @bulb2], Huey::Group.all[0].bulbs
40
+ assert_equal [@bulb3], Huey::Group.all[1].bulbs
41
+ assert_equal [@bulb4], Huey::Group.all[2].bulbs
42
+ end
43
+
44
+ def test_find_group_by_name
45
+ g = Huey::Group.new('Living Room')
46
+ g.name = 'Living Room'
47
+
48
+ assert_equal g, Huey::Group.find('Living Room')
49
+ end
50
+
51
+ def test_delegates_save_to_bulbs
52
+ @group = Huey::Group.new(@bulb1, @bulb2, @bulb3)
53
+
54
+ [@bulb1, @bulb2, @bulb3].each do |bulb|
55
+ Huey::Request.expects(:put).with("lights/#{bulb.id}/state", body: MultiJson.dump({on: true, bri: 100})).once.returns(true)
56
+ end
57
+
58
+ @group.on = true
59
+ @group.bri = 100
60
+ @group.save
61
+
62
+ [@bulb1, @bulb2, @bulb3].each do |bulb|
63
+ assert_equal true, bulb.on
64
+ assert_equal 100, bulb.bri
65
+ end
66
+ end
67
+
68
+ def test_delegates_update_to_bulbs
69
+ @group = Huey::Group.new(@bulb1, @bulb2, @bulb3)
70
+
71
+ [@bulb1, @bulb2, @bulb3].each do |bulb|
72
+ Huey::Request.expects(:put).with("lights/#{bulb.id}/state", body: MultiJson.dump({on: true, bri: 100})).once.returns(true)
73
+ end
74
+
75
+ @group.update(on: true, bri: 100)
76
+
77
+ [@bulb1, @bulb2, @bulb3].each do |bulb|
78
+ assert_equal true, bulb.on
79
+ assert_equal 100, bulb.bri
80
+ end
81
+ end
82
+
83
+ def test_returns_all_bulb_attrs_for_method_missing
84
+ @group = Huey::Group.new(@bulb1, @bulb2, @bulb3)
85
+
86
+ assert_equal @group.bri, {1=>127, 2=>127, 3=>127}
87
+ end
88
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-20 00:00:00.000000000 Z
12
+ date: 2013-02-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -128,12 +128,15 @@ files:
128
128
  - lib/huey/config.rb
129
129
  - lib/huey/config/options.rb
130
130
  - lib/huey/errors.rb
131
+ - lib/huey/group.rb
131
132
  - lib/huey/request.rb
132
133
  - lib/huey/ssdp.rb
133
134
  - lib/huey/ssdp/searcher.rb
134
135
  - lib/huey/version.rb
136
+ - test/fixtures/group.yml
135
137
  - test/test_helper.rb
136
138
  - test/unit/bulb_test.rb
139
+ - test/unit/group_test.rb
137
140
  - test/unit/request_test.rb
138
141
  - test/unit/ssdp_test.rb
139
142
  homepage: https://github.com/Veraticus/huey
@@ -154,12 +157,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
157
  - - ! '>='
155
158
  - !ruby/object:Gem::Version
156
159
  version: '0'
157
- segments:
158
- - 0
159
- hash: -1646308313904845457
160
160
  requirements: []
161
161
  rubyforge_project:
162
- rubygems_version: 1.8.24
162
+ rubygems_version: 1.8.23
163
163
  signing_key:
164
164
  specification_version: 3
165
165
  summary: Quick and simple discovery and control of Phillips Hue lightbulbs