hue_switch 0.9.2 → 1.0.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: ead8fa737e264f8dfacab9c256337c2f84a091a4
4
- data.tar.gz: 12d051202e0b31677fb804cf90d36fa47800c84e
3
+ metadata.gz: 71118f28167bd6ca48882186c602645ec1135e03
4
+ data.tar.gz: b8f1ce3f43b8a2f77563fd5859c03973fe4435fd
5
5
  SHA512:
6
- metadata.gz: c54ee80a93137306c7317da6027bec59d005603a8096cfed73b5e262a51cea1efba21a4eee17eae4d4e86efb8250b8e7efd37ec1c8777a099ae83180112b4076
7
- data.tar.gz: 5cbae30d9e871376749985051c6c0a8d8791bdc98ed8529b68b4dce8ff6c1d1309f648df49c6d2d9f3a4c1f36a1e4d57f038012989801187d1ac6f8cdce4cc78
6
+ metadata.gz: 3fd0ff508760d89656f0c24b95df11b0cb08101754fb647a9b8038d31dbac2ae017a027d23b8608275ace5309b0fa3b98674e8cec8c3a69f0a4c90476e331bd4
7
+ data.tar.gz: 74de57fffc30b3f6384961c0d85bc48d2126a6fbe14bb60bb5b1233875e5dedb984450a5e86d5cb2b75a62b8b66d05385e3a0f7e08c594851b3538eabc50a13d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/README.md CHANGED
@@ -1,11 +1,9 @@
1
- ## Installation
2
1
 
2
+ ## Installation
3
3
  Add this line to your application's Gemfile:
4
-
5
4
  ```ruby
6
5
  gem 'hue_switch'
7
6
  ```
8
-
9
7
  And then execute:
10
8
 
11
9
  $ bundle
@@ -17,22 +15,19 @@ Or install it yourself as:
17
15
  ## Usage
18
16
  A Hue Switch is designed control Hue lights, groups, and scenes. You can schedule a Switch. You can save a Switch as a Hue Scene on the bridge.
19
17
 
20
- NOTE: The first time you run this gem, you'll need to press the link button on the bridge immediately beforehand. If you don't, you'll get an error telling you to do so and try again.
21
-
22
- Why is this different from other wrappers? Hue-Switch is not designed to give full access to Hue API, but to make access to basic operations super easy. A Hue Switch can be assigned an existing group, scene, or one or more individual lights. Switches can be scheduled easily. Color effects can be added (color loop, random colors, etc.) For example, a switch could be three random lights, rotating through random colors (or colors in a range.) If you turn the switch off, the lights go off. Turn it on, and they continue to cycle through colors.
23
-
24
- Colors can be specified by
18
+ NOTE: The first time you run this gem, you'll need to press the link button on the bridge immediately beforehand. If you don't, you'll get an error telling you to do so and try again. You need to be logged into
25
19
 
26
- 1. Color name (pink, red, blue, etc) including Hue color names (reading, energize, etc.)
20
+ Why is this different from other wrappers?
27
21
 
28
- 2. Numeric value of hue [0-65535], or mired color value [0-500]
22
+ 1. Hue-Switch is not designed to give full access to Hue API, but to make access to basic operations super easy. A Hue Switch can be assigned an existing group, scene, or one or more individual lights. Switches can be scheduled easily. Color effects can be added (color loop, long and short alerts etc.) For example, a switch could be three random lights on a colorloop. If you turn the switch off, the lights go off. Turn it on, and they continue to cycle through colors.
29
23
 
24
+ 2. Hue-Switch comes with a utility method (dare I call it a DSL?), #voice. The voice method allows access to (almost) all of the Switch class funtionality with a single string. See below for important differences when using the #voice method
30
25
 
31
26
  ###Create a Switch
32
27
 
33
28
  Basic case, an on-off switch for all lights
34
29
  ```ruby
35
- switch = Switch.new {}
30
+ switch = Switch.new
36
31
  ```
37
32
 
38
33
  Then
@@ -40,37 +35,50 @@ Then
40
35
  switch.on
41
36
  switch.off
42
37
  ```
43
-
44
- ###Switch to control a group.
38
+ You can fully specify a switch at creation
45
39
 
46
40
  ```ruby
47
41
  switch = Switch.new do
48
- group :kitchen
42
+ light :kitchen
49
43
  color :blue
50
44
  saturation 255
51
45
  brightness 120
52
46
  fade 10 #specified in seconds
53
47
  end
54
48
  ```
55
- Use `group :all` to specify all lights.
49
+ Then switch.on
50
+
51
+ Colors can be specified by
52
+
53
+ 1. Color name (red, pink, purple, violet, blue, orange, turquoise, yellow, green, orange), including Hue mired color names (candle, relax, reading, concentrate, energize.)
54
+
55
+ 2. Numeric value of hue [0-65535], or mired color value [0-500]
56
+
57
+ Groups are indicated by **:lights**, single lights by **:light**.
56
58
 
57
- ###Switch to control a scene
59
+ A switch can control multiple individual lights, even if they are not in a group.
58
60
 
61
+ ```ruby
62
+ switch.lights :'living room' #controls a group
63
+ switch.light :bedside #controls a single light
64
+ switch.light :table, :"front door" #controls two lights not in a group together.
65
+ ```
66
+ Note: multi-word group/light/scene names must be enclosed in quotation marks.
67
+ ###Scenes
68
+ #####Recall
59
69
  ```ruby
60
70
  switch = Switch.new do
61
71
  scene :dinner
62
72
  end
63
- ```
64
-
65
- ###Switch to control one or more individually specified lights.
66
73
 
74
+ switch.scene :dinner
75
+ ```
76
+ #####Save
77
+ The current switch state can be saved as a scene on the bridge.
67
78
  ```ruby
68
- switch = Switch.new do
69
- lights :bedside, :floor, :table
70
- hue 27000
71
- brightness 120
72
- end
79
+ switch.save_scene "romantic dinner"
73
80
  ```
81
+ If a group is assigned to the switch, the scene will be assigned to that group. If no group is assigned, the scene will affect all lights.
74
82
  ###Modify Switch
75
83
 
76
84
  After the switch is created, you can modify it:
@@ -81,13 +89,10 @@ switch.color :red
81
89
  You can assign a new group, scene, or set of individual lights to a switch
82
90
 
83
91
  ####Or reset it
84
-
85
92
  ```ruby
86
93
  switch.reset
87
94
  ```
88
-
89
95
  ###Schedule the lights
90
-
91
96
  Switches can be scheduled:
92
97
 
93
98
  ```ruby
@@ -95,9 +100,11 @@ switch.schedule :on, "8:30 tonight"
95
100
  switch.schedule :off, "in three minutes" # "tomorrow", "next week"
96
101
  ```
97
102
 
98
- If you don't specify :on, :off, or a time, schedule will default to turning on the current switch state in 24 hours.
103
+ 1. If you don't specify :on, :off, or a time, schedule will default to turning on the current switch state in 24 hours.
104
+ 2. Lights will flash once to let you know the scene was successfully sceduled.
105
+ 3. Schedules can be set a week up to a week ahead of time. You can specify days of the week (e.g, monday, next tuesday) but you cannot specify a date.
99
106
 
100
- ####Delete the schedule[s] created by the switch:
107
+ ####Delete the schedule(s) created by the switch:
101
108
 
102
109
  ```ruby
103
110
  switch.delete_schedules!
@@ -105,53 +112,76 @@ switch.delete_schedules!
105
112
 
106
113
  ###Save Switch as a Hue Scene
107
114
 
108
- Just specify a name (words must be join with a hyphen.)
109
-
110
115
  The scene will be assigned to the group the switch is currently controlling (or all the lights if a group hasn't been assigned to the switch.)
111
116
 
112
117
  All features of the Switch (fade, group, color, etc) will be saved in the scene attributes.
113
-
114
118
  ```ruby
115
- switch.save_scene "my-switch"
119
+ switch.save_scene "my switch"
116
120
  ```
117
-
118
-
119
- ###Switches can be dynamic:
121
+ ###Dynamic Effects:
120
122
  ```ruby
121
123
  switch.colorloop # to cycle through colors
122
124
  switch.alert # to flash lights once
123
125
  switch.long_alert # to flash lights for 30 seconds
124
126
  ```
125
- then `switch.on`
126
-
127
- In addition
127
+ Then `switch.on`
128
128
 
129
+ Stop dynamic effects
129
130
  ```ruby
130
- switch.random_colors # to loop through random colors
131
+ switch.colorloop :stop
132
+ switch.alert :stop
131
133
  ```
132
- Hue, saturation, and brightness can be changed while the switch is cycling. If turned off, the switch will still be dynamic when turned back on.
134
+ Then `switch.on`
135
+ ##Voice
136
+ The voice method takes a single string and is meant to control the Switch class with voice commands. *This allows the Switch class to be used as a full-featured replacement for the Hue module on [Zach Feldman's](https://github.com/zachfeldman/alexa-home) Alexa-Home GitHub Repo.*
137
+
138
+ Because #voice takes a string meant to be spoken, there are important differences between using switch and switch.voice:
139
+
140
+ 1. switch.voice takes no numeric inputs. Brightness and Saturation are set on a scale of one to ten. Fade times for lights are similarly specified in words by seconds. See examples below.
141
+ 2. Colors can only be specified by name (i.e., 'hue 40000' is not available using #voice).
142
+ 3. Only a single light can be specified after a 'light' command.
143
+ 4. .reset and .delete_scenes! methods not available with #voice
144
+ 5. **Changes take effect immediately.** Using the #voice method, you don't need to send a separate "on" command for switch properties to be sent to the lights. So:
133
145
 
134
- Speed and color range can optionally be assigned
135
146
  ```ruby
136
- switch.random_colors range: 20000..28000 #cycles through greens, roughly
147
+ switch.color :blue : switch.on
148
+ switch.lights :kitchen ; switch.saturation 255 ; switch.brightness 127 ; switch.on
137
149
  ```
138
- Stops dynamic effects.
150
+ is now
139
151
  ```ruby
140
- switch.colorloop :stop
141
- switch.alert :stop
142
- switch.random_colors :stop
152
+ switch.voice "color blue"
153
+ swtich.voice "kitchen lights saturation ten brightness five"
154
+ ```
155
+ ###Examples
156
+ #####Recall a scene
157
+ ```ruby
158
+ switch.voice "romantic scene"
159
+ switch.voice "purple paradise scene"
160
+ ```
161
+ #####Save a scene
162
+ Will save the current light configuration to a scene on the bridge.
163
+ ```ruby
164
+ switch.voice "save scene as romantic dinner"
165
+ ```
166
+ ###Schedules
167
+ Scheduling syntax is extremely flexible. Any command can be scheduled. You can schedule up to a week ahead of time.
168
+ ```ruby
169
+ switch.voice "schedule kitchen lights color blue in ten minutes"
170
+ switch.voice "start colorloop bedside light schedule at eight fifty three next Friday"
171
+ switch.voice "romantic scene schedule tomorrow at eight in the evening"
172
+ switch.voice "schedule purple paradise scene thursday evening at eight forty five"
173
+ ```
174
+ ###Dynamic Effects:
175
+ ```ruby
176
+ "living room lights short alert" [flash lights in the group once]
177
+ "short alert table light" [flash light for 30 seconds]
178
+ "brightness five fireplace lights start colorloop"
179
+ "fireplace lights stop colorloop"
180
+ ```
181
+
143
182
 
144
183
 
145
- ## Development
146
184
 
147
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
148
185
 
149
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
150
186
 
151
- ## Contributing
152
187
 
153
- 1. Fork it ( https://github.com/[my-github-username]/hue_switch/fork )
154
- 2. Create your feature branch (`git checkout -b my-new-feature`)
155
- 3. Commit your changes (`git commit -am 'Add some feature'`)
156
- 4. Push to the branch (`git push origin my-new-feature`)
157
- 5. Create a new Pull Request
data/hue_switch.gemspec CHANGED
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_dependency 'httparty', '~> 0.13.0'
27
27
  spec.add_development_dependency "bundler", "~> 1.9"
28
28
  spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency 'rspec', '~> 0'
29
30
  end
@@ -1,3 +1,3 @@
1
1
  module HueSwitch
2
- VERSION = "0.9.2"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/hue_switch.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  require 'chronic'
2
2
  require 'chronic_duration'
3
3
  require 'httparty'
4
+ require 'numbers_in_words'
5
+ require 'numbers_in_words/duck_punch'
4
6
 
5
7
  class Switch
6
-
7
- attr_accessor :description, :lights_array, :_group, :body, :schedule_params, :schedule_ids, :effect, :effect_state, :effect_thread, :switch_state
8
-
9
- def initialize(description = "", _group = 0, &block)
8
+ attr_accessor :command, :lights_array, :_group, :body, :schedule_params, :schedule_ids
9
+ def initialize(command = "", _group = 0, &block)
10
10
 
11
11
  @user = "1234567890"
12
12
  @ip = HTTParty.get("https://www.meethue.com/api/nupnp").first["internalipaddress"]
@@ -19,17 +19,13 @@ class Switch
19
19
  @colors = {red: 65280, pink: 56100, purple: 52180, violet: 47188, blue: 46920, turquoise: 31146, green: 25500, yellow: 12750, orange: 8618}
20
20
  @mired_colors = {candle: 500, relax: 467, reading: 346, neutral: 300, concentrate: 231, energize: 136} ; puts @mired_colors
21
21
  @scenes = [] ; HTTParty.get("http://#{@ip}/api/#{@user}/scenes").keys.each { |k| @scenes.push(k) }
22
- @groups = {} ; HTTParty.get("http://#{@ip}/api/#{@user}/groups").each { |k,v| @groups["#{v['name']}".downcase.gsub(' ','_')] = k } ; @groups["all"] = "0"
22
+ @groups = {} ; HTTParty.get("http://#{@ip}/api/#{@user}/groups").each { |k,v| @groups["#{v['name']}".downcase] = k } ; @groups["all"] = "0"
23
23
  @lights = {} ; HTTParty.get("http://#{@ip}/api/#{@user}/lights").each { |k,v| @lights["#{v['name']}".downcase] = k }
24
24
 
25
- switch_state = switch_state
26
25
  self.lights_array = []
27
- self.effect_thread = []
28
- self.effect = ""
29
- self.effect_state = :off
30
26
  self.schedule_ids = []
31
27
  self.schedule_params = nil
32
- self.description = description
28
+ self.command = ""
33
29
  self._group = :all
34
30
  self.body = {}
35
31
  instance_eval(&block) if block_given?
@@ -69,35 +65,43 @@ class Switch
69
65
  self.body[:transitiontime] = in_seconds * 10
70
66
  end
71
67
 
72
- def lights(*args)
68
+ def light (*args)
73
69
  self.lights_array = []
74
70
  self._group = nil
75
71
  self.body.delete(:groups)
76
72
  self.body.delete(:scene)
77
73
  args.each { |l| self.lights_array.push @lights[l.to_s] if @lights.keys.include?(l.to_s) }
74
+ #self.send("on") if !self.command.include?("schedule")
78
75
  end
79
76
 
80
- def group(group_name)
77
+ def lights(group_name)
81
78
  self.lights_array = []
82
79
  self.body.delete(:scene)
83
80
  self._group = @groups[group_name.to_s]
81
+ #self.send("on") if !self.command.include?("schedule")
84
82
  end
85
83
 
86
84
  def scene(scene_name)
87
85
  self.lights_array = []
88
86
  self._group = 0
89
87
  self.body.delete(:groups)
88
+ self.body.delete(:hue)
89
+ self.body.delete(:ct)
90
90
  self.body[:scene] = scene_name.to_s
91
+ #self.send("on") if !self.command.include?("schedule")
91
92
  end
92
93
 
93
- def save_scene (scene_name)
94
+ def save_scene(name)
94
95
  self.fade 2 if self.body[:transitiontime] == nil
95
- if self._group.empty?
96
- light_group = HTTParty.get("http://10.100.100.3/api/#{@user}/groups/0")["lights"]
96
+ if self._group.nil?
97
+ light_group = HTTParty.get("http://#{@ip}/api/#{@user}/groups/0")["lights"]
97
98
  else
98
- light_group = HTTParty.get("http://10.100.100.3/api/#{@user}/groups/#{self._group}")["lights"]
99
+ light_group = HTTParty.get("http://#{@ip}/api/#{@user}/groups/#{self._group}")["lights"]
99
100
  end
101
+ scene_name = name
102
+ scene_name.gsub!(' ','-')
100
103
  params = {name: scene_name, lights: light_group, transitiontime: self.body[:transitiontime]}
104
+ p params
101
105
  response = HTTParty.put("http://#{@ip}/api/#{@user}/scenes/#{scene_name}", :body => params.to_json)
102
106
  if response.first.keys[0] == "success"
103
107
  puts "Scene created!"
@@ -107,11 +111,7 @@ class Switch
107
111
  end
108
112
 
109
113
  def on
110
- if self.switch_state != "on"
111
- self.switch_state = "on"
112
- self.body[:on] = true
113
- end
114
-
114
+ self.body[:on]=true
115
115
  if self.lights_array.any?
116
116
  self.lights_array.each { |l| HTTParty.put("http://#{@ip}/api/#{@user}/lights/#{l}/state", :body => (self.body).to_json) }
117
117
  elsif self.body.include?(:scene)
@@ -119,26 +119,10 @@ class Switch
119
119
  else
120
120
  HTTParty.put("http://#{@ip}/api/#{@user}/groups/#{self._group}/action", :body => (self.body).to_json)
121
121
  end
122
-
123
- #restart switch custom dynamic effects after switch is turned back on
124
- if !self.effect.empty? && !self.effect_thread.last.alive?
125
- self.effect_thread.each { |t| self.effect_thread.delete t if !t.alive?}
126
- self.send(self.effect, :start) if !self.effect.nil?
127
- end
128
- # Don't keep sending "on" signal to lights as suggested by Hue API
129
- self.body.delete(:on)
130
122
  end
131
123
 
132
124
  def off
133
- # Stop Switch custom dynamic effects
134
- if self.effect_state == :start
135
- self.effect_thread.last.kill
136
- self.effect_state = :stop
137
- end
138
- if self.switch_state != "off"
139
- self.switch_state = "off"
140
- self.body[:on] = false
141
- end
125
+ self.body[:on] = false
142
126
  if self.lights_array.any?
143
127
  self.lights_array.each { |l| HTTParty.put("http://#{@ip}/api/#{@user}/lights/#{l}/state", :body => (self.body).to_json) }
144
128
  elsif !self.body[:scene].nil?
@@ -150,22 +134,35 @@ class Switch
150
134
  end
151
135
  end
152
136
 
153
- def schedule(on_or_off = :default, set_time = "tomorrow")
154
- if set_time.scan(/ seconds?| minutes?| hours?| days?| weeks?/).any?
155
- set_time = Time.now + ChronicDuration.parse(set_time)
156
- set_time = set_time.to_s.split(' ')[0..1].join(' ').sub(' ',"T")
157
- else
158
- set_time = Chronic.parse(set_time).to_s.split(' ')[0..1].join(' ').sub(' ',"T")
159
- end
160
-
137
+ def schedule (on_or_off = :default, string)
161
138
  if on_or_off != :default
162
139
  on_or_off = on_or_off == :on ? true : false
163
140
  self.body[:on]=on_or_off
164
141
  end
165
142
 
166
- if Chronic.parse(set_time) < Time.now
143
+ if string.scan(/ seconds?| minutes?| hours?| days?| weeks?/).any?
144
+ set_time = Time.now + ChronicDuration.parse(string)
145
+ else
146
+ string.sub!(" noon", " twelve in the afternoon")
147
+ string.sub!("midnight", "twelve in the morning")
148
+ time_modifier = string.downcase.scan(/(evening)|(night|tonight)|(afternoon)|(pm)|(a.m.)|(morning)|(today)/).flatten.compact.first
149
+ guess = Time.now.strftime('%H').to_i >= 12 ? "p.m." : "a.m."
150
+ time_modifier = time_modifier.nil? ? guess : time_modifier
151
+ day_modifier = string.scan(/(tomorrow)|(next )?(monday|tuesday|wednesday|thursday|friday|saturday|sunday)/).flatten.compact.join(' ')
152
+ set_time = string.scan(Regexp.union((1..59).map(&:in_words)))
153
+ set_time.map!(&:in_numbers)
154
+ set_time.map!(&:to_s)
155
+ set_time.push("0") if set_time[1] == nil
156
+ set_time = set_time.shift + ':' + (set_time[0].to_i + set_time[1].to_i).to_s
157
+ set_time.gsub!(':', ':0') if set_time.split(":")[1].length < 2
158
+ set_time = Chronic.parse(day_modifier + ' ' + set_time + ' ' + time_modifier)
159
+ p set_time
160
+ end
161
+
162
+ if set_time < Time.now
167
163
  p "You've scheduled this in the past"
168
164
  else
165
+ set_time = set_time.to_s.split(' ')[0..1].join(' ').sub(' ',"T")
169
166
  self.schedule_params = {:name=>"Hue_Switch Alarm",
170
167
  :description=>"",
171
168
  :localtime=>"#{set_time}",
@@ -179,12 +176,17 @@ class Switch
179
176
  self.schedule_ids.push(HTTParty.post("http://#{@ip}/api/#{@user}/schedules", :body => (self.schedule_params).to_json))
180
177
  end
181
178
  elsif !self.body[:scene].nil?
182
- self.schedule_params[:command] = {:address=>"/api/#{@user}/groups/#{self._group}/action", :method=>"PUT", :body=>self.body.reject { |s| s == :scene }}
179
+ self.schedule_params[:command] = {:address=>"/api/#{@user}/groups/#{self._group}/action", :method=>"PUT", :body=>self.body }
183
180
  self.schedule_ids.push(HTTParty.post("http://#{@ip}/api/#{@user}/schedules", :body => (self.schedule_params).to_json))
184
181
  else
185
182
  self.schedule_params[:command] = {:address=>"/api/#{@user}/groups/#{self._group}/action", :method=>"PUT", :body=>self.body}
186
183
  self.schedule_ids.push(HTTParty.post("http://#{@ip}/api/#{@user}/schedules", :body => (self.schedule_params).to_json))
187
184
  end
185
+ result = self.schedule_ids.flatten.last
186
+ if result.keys.include?("success")
187
+ params = {:alert => 'select'}
188
+ HTTParty.put("http://#{@ip}/api/#{@user}/groups/0/action" , :body => params.to_json)
189
+ end
188
190
  end
189
191
  end
190
192
 
@@ -197,42 +199,114 @@ class Switch
197
199
  self.schedule_ids = []
198
200
  end
199
201
 
202
+ def colorloop(start_or_stop)
203
+ if start_or_stop == :start
204
+ self.body[:effect] = "colorloop"
205
+ elsif start_or_stop == :stop
206
+ self.body[:effect] = "none"
207
+ end
208
+ end
209
+
210
+ def alert(value)
211
+ if value == :short
212
+ self.body[:alert] = "select"
213
+ elsif value == :long
214
+ self.body[:alert] = "lselect"
215
+ elsif value == :stop
216
+ self.body[:alert] = "none"
217
+ end
218
+ end
219
+
200
220
  def reset
221
+ self.command = ""
201
222
  self._group = 0
202
223
  self.body = {}
203
224
  self.schedule_params = nil
204
225
  end
205
226
 
206
- # Switches can be dynamic
207
- def colorloop(state = :on)
208
- state == :on ? self.body[:effect] = "colorloop" : self.body[:effect] = "none"
227
+ #The following two methods are required to use Switch for Zach Feldman's Alexa-home*
228
+ def wake_words
229
+ ["light", "lights", "scene", "seen", "flash"]
209
230
  end
210
231
 
211
- def alert(state = :on)
212
- state == :on ? self.body[:alert] = "select" : self.body[:alert] = "none"
232
+ def process_command (command)
233
+ command.sub!("color loop", "colorloop")
234
+ command.sub!("too", "two")
235
+ command.sub!(/a half$/, 'thirty seconds')
236
+ self.voice command
213
237
  end
238
+
239
+ #This method allows access to most of the Switch class functionality by supplying a single string.
240
+ def voice (string)
241
+ trailing_methods = string.split(' ') & %w[lights light]
242
+ leading_methods = string.split(' ') & %w[hue brightness saturation fade color]
243
+ dynamic_methods = string.split(' ') & %w[colorloop alert]
244
+ scene_name = string.partition(" scene").first
245
+ self.command = ""
246
+ self.command << string
247
+ string.gsub!('schedule','')
214
248
 
215
- def long_alert(state = :on)
216
- state == :on ? self.body[:alert] = "lselect" : self.body[:alert] = "none"
217
- end
249
+ if string.include?('scene')
250
+ scene_name.gsub!(' ','-') if scene_name.size > 1
251
+ scene_name.gsub!(/schedule-?/,'') if scene_name.include?('schedule')
252
+ puts scene_name
253
+ self.send("scene", scene_name)
254
+ end
218
255
 
219
- def random_colors(state = :start, range: 0..65500)
220
- if state == :stop
221
- self.effect = ""
222
- self.effect_thread = []
223
- self.effect_state = :stop
224
- else
225
- self.effect_state = state
226
- self.effect = __method__.to_s
227
- self.fade 2
228
- Thread.new do
229
- self.effect_thread.push Thread.list.last
230
- while self.effect_state == :start do
231
- self.body[:hue] = rand(range)
232
- sleep (self.body[:transitiontime] / 10) + 0.5
233
- self.on
234
- end
256
+ if leading_methods.any?
257
+ leading_methods.each do |l|
258
+ capture = (string.match (/\b#{l}\s\w+/)).to_s.split(' ')
259
+ method = capture[0]
260
+ value = capture[1]
261
+ value = value.in_numbers if value.scan(Regexp.union( (1..10).map {|k| k.in_words} ) ).any?
262
+ value = ((value.to_f/10.to_f)*255).to_i if (value.class == Fixnum) && (l != "fade")
263
+ self.send( method, value )
264
+ end
265
+ end
266
+
267
+ if trailing_methods.any?
268
+ trailing_methods.each do |t|
269
+ all_keys = []
270
+ all_keys.push(@groups.keys)
271
+ all_keys.push(@lights.keys)
272
+ all_keys.flatten!
273
+ all_keys = Regexp.union(all_keys)
274
+ value = string.match (all_keys).to_s
275
+ self.send( "#{t}", value )
276
+ end
277
+ end
278
+
279
+ if dynamic_methods.any?
280
+ dynamic_methods.each do |d|
281
+ capture = (string.match (/\w+ #{d}\b/)).to_s.split(' ')
282
+ capture = capture.reverse
283
+ method = capture[0]
284
+ value = capture[1].to_sym
285
+ self.send( method, value )
286
+ end
287
+ end
288
+
289
+ if self.command.scan(/save (scene|seen) as/).length > 0
290
+ scene_name = self.command.partition(/save (scene|seen) as /).last
291
+ self.send( "save_scene", scene_name)
292
+ end
293
+
294
+ if !self.command.include?("schedule")
295
+ if string.include?(" on")
296
+ self.send("on")
297
+ elsif string.include?(" off")
298
+ self.send("off")
299
+ end
300
+ end
301
+
302
+ if self.command.include?("schedule")
303
+ if string.include?(" on")
304
+ self.body[:on] = true
305
+ elsif string.include?(" off")
306
+ self.body[:on] = false
235
307
  end
308
+ self.send("schedule", string)
236
309
  end
310
+ self.send("on") unless string.include?(" off") && !string.include?("schedule")
237
311
  end
238
312
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hue_switch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sarkonovich
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-27 00:00:00.000000000 Z
11
+ date: 2015-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chronic
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: A 'switch' controls Hue lights, groups, or scenes. Switches can be scheduled,
84
98
  and given dynamic effects.
85
99
  email:
@@ -88,13 +102,13 @@ executables: []
88
102
  extensions: []
89
103
  extra_rdoc_files: []
90
104
  files:
105
+ - ".gitignore"
106
+ - ".travis.yml"
91
107
  - Gemfile
92
- - Gemfile.lock
93
108
  - README.md
94
109
  - Rakefile
95
110
  - bin/console
96
111
  - bin/setup
97
- - hue_switch-0.1.0.gem
98
112
  - hue_switch.gemspec
99
113
  - lib/hue_switch.rb
100
114
  - lib/hue_switch/version.rb
data/Gemfile.lock DELETED
@@ -1,29 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- hue_switch (0.1.0)
5
- chronic
6
- chronic_duration
7
- httparty
8
-
9
- GEM
10
- remote: https://rubygems.org/
11
- specs:
12
- chronic (0.10.2)
13
- chronic_duration (0.10.6)
14
- numerizer (~> 0.1.1)
15
- httparty (0.13.3)
16
- json (~> 1.8)
17
- multi_xml (>= 0.5.2)
18
- json (1.8.2)
19
- multi_xml (0.5.5)
20
- numerizer (0.1.1)
21
- rake (10.4.2)
22
-
23
- PLATFORMS
24
- ruby
25
-
26
- DEPENDENCIES
27
- bundler (~> 1.9)
28
- hue_switch!
29
- rake (~> 10.0)
data/hue_switch-0.1.0.gem DELETED
Binary file