alexa_hue 1.0.2 → 1.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: 7fe4659ee0eb80c1e886f7f8e6e998ad1703f54f
4
- data.tar.gz: 00402f7a5131214ebd433e74a42d057eb68168ee
3
+ metadata.gz: 939ace937c9a84b49de17edb7195b74a5a6dc433
4
+ data.tar.gz: de31f09e481c0b904f704ec7b33a3349aa0f0827
5
5
  SHA512:
6
- metadata.gz: 1ed672e8af71018fe25726970758c737c831f71aae85cc593cfa977ce5259acaff1067cfbb0c47b776fe18dd07a9d726fe1f8e47c6065d4e2f18d097454cbe0c
7
- data.tar.gz: d6a8eb44881f710495c9ebb1ef4dfddfc58008bb73953615a472c96474ce7e12ebb88a275af3d93cac9fe0cd3d5fe15a8d24e8aab9983cd1415dba07d49538bc
6
+ metadata.gz: c375f8c8205baf13a4d7b7b055bc8778bb96a2826b18a0ba6f00d30603a9f0bb839f8078e36006ac4de429b40a78d7454bf3b1897c5e818f8dbc4ed014e5fd98
7
+ data.tar.gz: 8033ac7150f3fb23608e593d3c26e9d79862fedb7b86e4aa0d059027c19910e38d78ac89bdf86ba61e59b99769da5540e9daacc0dfc9413a79c31d0dcba91a9f
data/alexa_hue.gemspec CHANGED
@@ -5,7 +5,7 @@ require 'alexa_hue/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "alexa_hue"
8
- spec.version = Sinatra::Hue::VERSION
8
+ spec.version = Hue::VERSION
9
9
  spec.authors = ["Kyle Lucas"]
10
10
  spec.email = ["kglucas93@gmail.com"]
11
11
  spec.summary = %q{A sinatra middleware for alexa hue actions.}
@@ -1,7 +1,5 @@
1
- module Sinatra
2
- module Hue
3
- def self.custom_slots
4
- File.read(File.expand_path('../../../skills_config/custom_slots.txt', __FILE__))
5
- end
1
+ module Hue
2
+ def self.custom_slots
3
+ File.read(File.expand_path('../../../skills_config/custom_slots.txt', __FILE__))
6
4
  end
7
- end
5
+ end
@@ -10,7 +10,6 @@ require 'numbers_in_words'
10
10
  require 'numbers_in_words/duck_punch'
11
11
  require 'timeout'
12
12
 
13
- module Sinatra
14
13
  module Hue
15
14
  module_function
16
15
 
@@ -26,24 +25,14 @@ module Hue
26
25
  class Switch
27
26
  attr_accessor :command, :lights_array, :_group, :body, :schedule_params, :schedule_ids
28
27
  def initialize(command = "", _group = 0, &block)
29
-
30
28
  @user = "1234567890"
31
- begin
32
- @ip = HTTParty.get("https://www.meethue.com/api/nupnp").first["internalipaddress"] rescue nil
33
- if @ip.nil?
34
- bridge = get_bridge_by_SSDP
35
- @ip = bridge.ip
36
- end
37
29
 
38
- rescue Timeout::Error
39
- puts "Time Out"
40
- rescue NoMethodError
41
- puts "Cannot Find Bridge via Hue broker service, trying SSDP..."
42
- rescue Errno::ECONNREFUSED
43
- puts "Connection refused"
44
- rescue SocketError
45
- puts "Cannot connect to local network"
46
- end
30
+ begin
31
+ @ip = HTTParty.get("https://www.meethue.com/api/nupnp").first["internalipaddress"] rescue nil
32
+ @ip ||= get_bridge_by_SSDP.ip
33
+ rescue
34
+ puts "Cannot connect to bridge."
35
+ end
47
36
 
48
37
  authorize_user
49
38
  populate_switch
@@ -58,16 +47,12 @@ module Hue
58
47
  end
59
48
 
60
49
  def list_lights
61
- light_list = {}
62
- HTTParty.get("http://#{@ip}/api/#{@user}/lights").each { |k,v| light_list["#{v['name']}".downcase] = k }
63
- light_list
50
+ HTTParty.get("http://#{@ip}/api/#{@user}/lights").map{|k,v|[k,"#{v['name']}".downcase]}.to_h
64
51
  end
65
52
 
66
53
  def list_groups
67
- group_list = {}
68
- HTTParty.get("http://#{@ip}/api/#{@user}/groups").each { |k,v| group_list["#{v['name']}".downcase] = k }
69
- group_list["all"] = "0"
70
- group_list
54
+ HTTParty.get("http://#{@ip}/api/#{@user}/groups").map{|k,v|[k,"#{v['name']}".downcase]}.to_h.merge("all" =>
55
+ "0")
71
56
  end
72
57
 
73
58
  def list_scenes
@@ -111,9 +96,7 @@ module Hue
111
96
  end
112
97
 
113
98
  def clear_attributes
114
- self.body.delete(:scene)
115
- self.body.delete(:ct)
116
- self.body.delete(:hue)
99
+ self.body.delete_if{|k|[:scene,:ct,:hue].include?(k)}
117
100
  end
118
101
 
119
102
  def fade(in_seconds)
@@ -143,19 +126,17 @@ module Hue
143
126
  end
144
127
 
145
128
  def confirm
146
- params = {:alert => 'select'}
147
- HTTParty.put("http://#{@ip}/api/#{@user}/groups/0/action" , :body => params.to_json)
129
+ HTTParty.put("http://#{@ip}/api/#{@user}/groups/0/action" , :body => {:alert => 'select'}.to_json)
148
130
  end
149
131
 
150
132
  def save_scene(scene_name)
151
- scene_name.gsub!(' ','-')
152
133
  self.fade 2 if self.body[:transitiontime] == nil
153
134
  if self._group.empty?
154
135
  light_group = HTTParty.get("http://#{@ip}/api/#{@user}/groups/0")["lights"]
155
136
  else
156
137
  light_group = HTTParty.get("http://#{@ip}/api/#{@user}/groups/#{self._group}")["lights"]
157
138
  end
158
- params = {name: scene_name, lights: light_group, transitiontime: self.body[:transitiontime]}
139
+ params = {name: scene_name.gsub!(' ','-'), lights: light_group, transitiontime: self.body[:transitiontime]}
159
140
  response = HTTParty.put("http://#{@ip}/api/#{@user}/scenes/#{scene_name}", :body => params.to_json)
160
141
  confirm if response.first.keys[0] == "success"
161
142
  end
@@ -174,7 +155,6 @@ module Hue
174
155
  elsif self.body[:on] == false
175
156
  # turn off individual lights in the scene
176
157
  (HTTParty.get("http://#{@ip}/api/#{@user}/scenes"))[self.body[:scene]]["lights"].each do |l|
177
- puts self.body
178
158
  HTTParty.put("http://#{@ip}/api/#{@user}/lights/#{l}/state", :body => (self.body).to_json)
179
159
  end
180
160
  end
@@ -272,7 +252,6 @@ module Hue
272
252
  #The rest of the methods allow access to most of the Switch class functionality by supplying a single string
273
253
 
274
254
  def voice(string)
275
- puts string
276
255
  self.reset
277
256
  self.command << string
278
257
 
@@ -400,10 +379,6 @@ module Hue
400
379
  end
401
380
  end
402
381
 
403
-
404
- # The Device and SSDP classes are basically lifted from Sam Soffes' great GitHub repo:
405
- # https://github.com/soffes/discover.
406
-
407
382
  class Device
408
383
  attr_reader :ip
409
384
  attr_reader :port
@@ -521,4 +496,3 @@ module Hue
521
496
  end
522
497
  end
523
498
  end
524
- end
@@ -1,7 +1,5 @@
1
- module Sinatra
2
- module Hue
3
- def self.intent_schema
4
- File.read(File.expand_path('../../../skills_config/intent_schema.txt', __FILE__))
5
- end
1
+ module Hue
2
+ def self.intent_schema
3
+ File.read(File.expand_path('../../../skills_config/intent_schema.txt', __FILE__))
6
4
  end
7
- end
5
+ end
@@ -1,7 +1,5 @@
1
- module Sinatra
2
- module Hue
3
- def self.sample_utterances
4
- File.read(File.expand_path('../../../skills_config/sample_utterances.txt', __FILE__))
5
- end
1
+ module Hue
2
+ def self.sample_utterances
3
+ File.read(File.expand_path('../../../skills_config/sample_utterances.txt', __FILE__))
6
4
  end
7
- end
5
+ end
@@ -1,5 +1,4 @@
1
- module Sinatra
2
- module Hue
3
- VERSION = "1.0.2"
4
- end
1
+ module Hue
2
+ VERSION = "1.1.0"
5
3
  end
4
+
data/lib/alexa_hue.rb CHANGED
@@ -14,131 +14,80 @@ require 'alexa_hue/fix_schedule_syntax'
14
14
 
15
15
  LEVELS = {} ; [*1..10].each { |t| LEVELS[t.to_s ] = t.in_words }
16
16
 
17
- module Sinatra
18
- module Hue
19
- def self.registered(app)
20
- app.post '/alexa_hue' do
21
- content_type :json
22
17
 
23
- # halt 400, "Invalid Application ID" unless @application_id == "your application id here"
24
-
25
- # Behavior for Launch Request
26
- if @echo_request.launch_request?
27
- response = AlexaObjects::Response.new
28
- response.spoken_response = "I'm ready to control the lights"
29
- response.end_session = false
30
- response.without_card.to_json
31
-
32
- # Behavior for ControlLights intent. Keys and values need to be adjusted a bit to work with HueSwitch #voice syntax.
33
- elsif @echo_request.intent_name == "ControlLights"
34
- if @echo_request.slots.brightness
35
- LEVELS.keys.reverse_each { |level| @echo_request.slots.brightness.sub!(level, LEVELS[level]) } if @echo_request.slots.schedule.nil?
36
- end
18
+ module Hue
19
+ extend Sinatra::Extension
37
20
 
38
- if @echo_request.slots.saturation
39
- LEVELS.keys.reverse_each { |level| @echo_request.slots.saturation.sub!(level, LEVELS[level]) } if @echo_request.slots.schedule.nil?
40
- end
21
+ helpers do
22
+ def control_lights
23
+ if @echo_request.slots.brightness
24
+ LEVELS.keys.reverse_each { |level| @echo_request.slots.brightness.sub!(level, LEVELS[level]) } if @echo_request.slots.schedule.nil?
25
+ end
41
26
 
42
- puts @echo_request.slots
43
- @echo_request.slots.to_h.each do |k,v|
44
- @string ||= ""
45
- next unless v
46
- if k == :scene || k == :alert
47
- @string << "#{v.to_s} #{k.to_s} "
48
- elsif k == :lights || k == :modifier || k == :state
49
- @string << "#{v.to_s} "
50
- elsif k == :savescene
51
- @string << "save scene as #{v.to_s} "
52
- elsif k == :flash
53
- @string << "start long alert "
54
- else
55
- @string << "#{k.to_s} #{v.to_s} "
56
- end
57
- end
58
-
59
- fix_schedule_syntax(@string)
60
- @string.sub!("color loop", "colorloop")
61
- @string.strip!
62
-
63
- begin
64
- switch = Hue::Switch.new
65
- rescue RuntimeError
66
- response = AlexaObjects::Response.new
67
- response.end_session = true
68
- response.spoken_response = "Hello. Before using Hue lighting, you'll need to give me access to your Hue bridge." +
69
- " Please press the link button on your bridge and launch the skill again within ten seconds."
70
- halt response.without_card.to_json
27
+ if @echo_request.slots.saturation
28
+ LEVELS.keys.reverse_each { |level| @echo_request.slots.saturation.sub!(level, LEVELS[level]) } if @echo_request.slots.schedule.nil?
29
+ end
30
+
31
+ @echo_request.slots.to_h.each do |k,v|
32
+ @string ||= ""
33
+ next unless v
34
+ if k == :scene || k == :alert
35
+ @string << "#{v.to_s} #{k.to_s} "
36
+ elsif k == :lights || k == :modifier || k == :state
37
+ @string << "#{v.to_s} "
38
+ elsif k == :savescene
39
+ @string << "save scene as #{v.to_s} "
40
+ elsif k == :flash
41
+ @string << "start long alert "
42
+ else
43
+ @string << "#{k.to_s} #{v.to_s} "
71
44
  end
45
+ end
72
46
 
73
- if @echo_request.slots.lights.nil? && @echo_request.slots.scene.nil? && @echo_request.slots.savescene.nil?
74
- r = AlexaObjects::Response.new
75
- r.end_session = false
76
- r.spoken_response = "Please specify which light or lights you'd like to adjust. I'm ready to control the lights."
77
- halt r.without_card.to_json
78
- end
47
+ fix_schedule_syntax(@string)
48
+ @string.sub!("color loop", "colorloop")
49
+ @string.strip!
79
50
 
80
- if @echo_request.slots.lights
81
- if @echo_request.slots.lights.scan(/light|lights/).empty?
82
- r = AlexaObjects::Response.new
83
- r.end_session = false
84
- r.spoken_response = "Please specify which light or lights you'd like to adjust. I'm ready to control the lights."
85
- halt r.without_card.to_json
86
- end
51
+ begin
52
+ switch = Hue::Switch.new
53
+ rescue RuntimeError
54
+ halt AlexaObjects::Response.new(spoken_response: "Hello. Before using Hue lighting, you'll need to give me access to your Hue bridge. Please press the link button on your bridge and launch the skill again within ten seconds.").to_json
55
+ end
56
+
57
+ if @echo_request.slots.lights.nil? && @echo_request.slots.scene.nil? && @echo_request.slots.savescene.nil?
58
+ halt AlexaObjects::Response.new(end_session: false, spoken_response: "Please specify which light or lights you'd like to adjust. I'm ready to control the lights.").to_json
59
+ end
60
+
61
+ if @echo_request.slots.lights
62
+ if @echo_request.slots.lights.scan(/light|lights/).empty?
63
+ halt AlexaObjects::Response.new(end_session: false, spoken_response: "Please specify which light or lights you'd like to adjust. I'm ready to control the lights.").to_json
87
64
  end
65
+ end
88
66
 
89
- puts @echo_request.slots.lights
90
- if @echo_request.slots.lights
67
+ if @echo_request.slots.lights
91
68
  if @echo_request.slots.lights.include?('lights')
92
- puts switch.list_groups.keys.join(', ').downcase
93
69
  if !(switch.list_groups.keys.join(', ').downcase.include?("#{@echo_request.slots.lights.sub(' lights','')}"))
94
- r = AlexaObjects::Response.new
95
- r.end_session = true
96
- r.spoken_response = "I couldn't find a group with the name #{@echo_request.slots.lights}"
97
- halt r.without_card.to_json
70
+ halt AlexaObjects::Response.new(spoken_response: "I couldn't find a group with the name #{@echo_request.slots.lights}").to_json
98
71
  end
99
-
100
72
  elsif @echo_request.slots.lights.include?('light')
101
73
  if !(switch.list_lights.keys.join(', ').downcase.include?("#{@echo_request.slots.lights.sub(' light','')}"))
102
- r = AlexaObjects::Response.new
103
- r.end_session = true
104
- r.spoken_response = "I couldn't find a light with the name #{@echo_request.slots.lights}"
105
- halt r.without_card.to_json
74
+ halt AlexaObjects::Response.new(spoken_response: "I couldn't find a light with the name #{@echo_request.slots.lights}").to_json
106
75
  end
107
76
  end
108
77
  end
109
78
 
110
79
 
111
- #if @string.include?('light ')
112
- # if (@string.split(' ') & switch.list_lights.keys.join(', ').downcase.split(', ')).empty?
113
- # r = AlexaObjects::Response.new
114
- # r.end_session = true
115
- # r.spoken_response = "I couldn't find a light with the name #{@echo_request.slots.lights}"
116
- # halt r.without_card.to_json
117
- # end
118
- #end
119
- switch.voice @string
80
+ #if @string.include?('light ')
81
+ # if (@string.split(' ') & switch.list_lights.keys.join(', ').downcase.split(', ')).empty?
82
+ # r = AlexaObjects::Response.new
83
+ # r.end_session = true
84
+ # r.spoken_response = "I couldn't find a light with the name #{@echo_request.slots.lights}"
85
+ # halt r.without_card.to_json
86
+ # end
87
+ #end
88
+ switch.voice @string
120
89
 
121
- response = AlexaObjects::Response.new
122
- response.end_session = true
123
- response.spoken_response = "okay"
124
- response.card_content = "#{@string}...#{@data}"
125
- response.without_card.to_json # change to .with_card.to_json for debugging info
126
-
127
- # Behavior for End Session requests.
128
- elsif @echo_request.intent_name == "EndSession"
129
- response = AlexaObjects::Response.new
130
- response.end_session = true
131
- response.spoken_response = "exiting lighting"
132
- response.without_card.to_json
133
-
134
- elsif @echo_request.session_ended_request?
135
- response = AlexaObjects::Response.new
136
- response.end_session = true
137
- response.without_card.to_json
138
- end
139
- end
90
+ return AlexaObjects::Response.new(spoken_response: "okay").to_json
140
91
  end
141
92
  end
142
-
143
- register Hue
144
93
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alexa_hue
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Lucas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-14 00:00:00.000000000 Z
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -207,7 +207,6 @@ files:
207
207
  - alexa_hue.gemspec
208
208
  - lib/alexa_hue.rb
209
209
  - lib/alexa_hue/custom_slots.rb
210
- - lib/alexa_hue/endpoint.rb
211
210
  - lib/alexa_hue/fix_schedule_syntax.rb
212
211
  - lib/alexa_hue/hue_switch.rb
213
212
  - lib/alexa_hue/intent_schema.rb
@@ -1,7 +0,0 @@
1
- module Sinatra
2
- module Hue
3
- def self.endpoint
4
- '/alexa_hue'
5
- end
6
- end
7
- end