Class: Sinatra::Hue::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/alexa_hue/hue_switch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command = "", _group = 0, &block) ⇒ Switch

Returns a new instance of Switch.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/alexa_hue/hue_switch.rb', line 28

def initialize(command = "", _group = 0, &block)

  @user = "1234567890"
    begin
      @ip = HTTParty.get("https://www.meethue.com/api/nupnp").first["internalipaddress"] rescue nil
      if @ip.nil?
        bridge = get_bridge_by_SSDP
        @ip = bridge.ip
      end

    rescue Timeout::Error
      puts "Time Out"
    rescue NoMethodError
      puts "Cannot Find Bridge via Hue broker service, trying SSDP..."
    rescue Errno::ECONNREFUSED
      puts "Connection refused"
    rescue SocketError
      puts "Cannot connect to local network"
    end

  authorize_user
  populate_switch

  self.lights_array = []
  self.schedule_ids = []
  self.schedule_params = nil
  self.command = ""
  self._group = "0"
  self.body = {}
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#_groupObject

Returns the value of attribute _group.



27
28
29
# File 'lib/alexa_hue/hue_switch.rb', line 27

def _group
  @_group
end

#bodyObject

Returns the value of attribute body.



27
28
29
# File 'lib/alexa_hue/hue_switch.rb', line 27

def body
  @body
end

#commandObject

Returns the value of attribute command.



27
28
29
# File 'lib/alexa_hue/hue_switch.rb', line 27

def command
  @command
end

#lights_arrayObject

Returns the value of attribute lights_array.



27
28
29
# File 'lib/alexa_hue/hue_switch.rb', line 27

def lights_array
  @lights_array
end

#schedule_idsObject

Returns the value of attribute schedule_ids.



27
28
29
# File 'lib/alexa_hue/hue_switch.rb', line 27

def schedule_ids
  @schedule_ids
end

#schedule_paramsObject

Returns the value of attribute schedule_params.



27
28
29
# File 'lib/alexa_hue/hue_switch.rb', line 27

def schedule_params
  @schedule_params
end

Instance Method Details

#alert(value) ⇒ Object



242
243
244
245
246
247
248
249
250
# File 'lib/alexa_hue/hue_switch.rb', line 242

def alert(value)
  if value == :short
    self.body[:alert] = "select"
  elsif value == :long
    self.body[:alert] = "lselect"
  elsif value == :stop
    self.body[:alert] = "none"
  end
end

#brightness(depth) ⇒ Object



108
109
110
111
# File 'lib/alexa_hue/hue_switch.rb', line 108

def brightness(depth)
  self.body.delete(:scene)
  self.body[:bri] = depth
end

#clear_attributesObject



113
114
115
116
117
# File 'lib/alexa_hue/hue_switch.rb', line 113

def clear_attributes
  self.body.delete(:scene)
  self.body.delete(:ct)
  self.body.delete(:hue)
end

#color(color_name) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/alexa_hue/hue_switch.rb', line 94

def color(color_name)
  clear_attributes
  if @colors.keys.include?(color_name.to_sym)
    self.body[:hue] = @colors[color_name.to_sym]
  else
    self.body[:ct] = @mired_colors[color_name.to_sym]
  end
end

#colorloop(start_or_stop) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/alexa_hue/hue_switch.rb', line 234

def colorloop(start_or_stop)
  if start_or_stop == :start
    self.body[:effect] = "colorloop"
  elsif start_or_stop == :stop
    self.body[:effect] = "none"
  end
end

#confirmObject



145
146
147
148
# File 'lib/alexa_hue/hue_switch.rb', line 145

def confirm
  params = {:alert => 'select'}
  HTTParty.put("http://#{@ip}/api/#{@user}/groups/0/action" , :body => params.to_json)
end

#delete_schedules!Object



225
226
227
228
229
230
231
232
# File 'lib/alexa_hue/hue_switch.rb', line 225

def delete_schedules!
  self.schedule_ids.flatten!
  self.schedule_ids.each { |k|
    id = k["success"]["id"] if k.include?("success")
    HTTParty.delete("http://#{@ip}/api/#{@user}/schedules/#{id}")
  }
  self.schedule_ids = []
end

#fade(in_seconds) ⇒ Object



119
120
121
# File 'lib/alexa_hue/hue_switch.rb', line 119

def fade(in_seconds)
  self.body[:transitiontime] = in_seconds * 10
end

#group_on_offObject



167
168
169
# File 'lib/alexa_hue/hue_switch.rb', line 167

def group_on_off
  HTTParty.put("http://#{@ip}/api/#{@user}/groups/#{self._group}/action", :body => (self.body.reject { |s| s == :scene }).to_json)
end

#hue(numeric_value) ⇒ Object



84
85
86
87
# File 'lib/alexa_hue/hue_switch.rb', line 84

def hue (numeric_value)
  clear_attributes
  self.body[:hue] = numeric_value
end

#light(*args) ⇒ Object



123
124
125
126
127
128
# File 'lib/alexa_hue/hue_switch.rb', line 123

def light (*args)
  self.lights_array = []
  self._group = ""
  self.body.delete(:scene)
  args.each { |l| self.lights_array.push @lights[l.to_s] if @lights.keys.include?(l.to_s) }
end

#lights(group_name) ⇒ Object



130
131
132
133
134
135
# File 'lib/alexa_hue/hue_switch.rb', line 130

def lights(group_name)
  self.lights_array = []
  self.body.delete(:scene)
  group = @groups[group_name.to_s]
  self._group = group if !group.nil?
end

#lights_on_offObject



163
164
165
# File 'lib/alexa_hue/hue_switch.rb', line 163

def lights_on_off
  self.lights_array.each { |l| HTTParty.put("http://#{@ip}/api/#{@user}/lights/#{l}/state", :body => (self.body).to_json) }
end

#list_groupsObject



66
67
68
69
70
71
# File 'lib/alexa_hue/hue_switch.rb', line 66

def list_groups
  group_list = {}
  HTTParty.get("http://#{@ip}/api/#{@user}/groups").each { |k,v| group_list["#{v['name']}".downcase] = k }
  group_list["all"] = "0"
  group_list
end

#list_lightsObject



60
61
62
63
64
# File 'lib/alexa_hue/hue_switch.rb', line 60

def list_lights
  light_list = {}
  HTTParty.get("http://#{@ip}/api/#{@user}/lights").each { |k,v| light_list["#{v['name']}".downcase] = k }
  light_list
end

#list_scenesObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/alexa_hue/hue_switch.rb', line 73

def list_scenes
  scene_list = {}
  HTTParty.get("http://#{@ip}/api/#{@user}/scenes").each do |scene|
    if scene[1]["owner"] != "none"
      inner = {"id" => scene[0]}.merge(scene[1])
      scene_list.merge!({"#{scene[1]["name"].split(' ').first.downcase}" => inner})
    end
  end
  scene_list
end

#mired(numeric_value) ⇒ Object



89
90
91
92
# File 'lib/alexa_hue/hue_switch.rb', line 89

def mired (numeric_value)
  clear_attributes
  self.body[:ct] = numeric_value
end

#offObject



190
191
192
193
194
195
# File 'lib/alexa_hue/hue_switch.rb', line 190

def off
  self.body[:on]=false
  lights_on_off if self.lights_array.any?
  group_on_off if (!self._group.empty? && self.body[:scene].nil?)
  scene_on_off if !self.body[:scene].nil?
end

#onObject



183
184
185
186
187
188
# File 'lib/alexa_hue/hue_switch.rb', line 183

def on
  self.body[:on]=true
  lights_on_off if self.lights_array.any?
  group_on_off if (!self._group.empty? && self.body[:scene].nil?)
  scene_on_off if !self.body[:scene].nil?
end

#process_command(command) ⇒ Object



264
265
266
267
268
269
270
# File 'lib/alexa_hue/hue_switch.rb', line 264

def process_command (command)
  command.sub!("color loop", "colorloop")
  command.sub!("too", "two")
  command.sub!("for", "four")
  command.sub!(/a half$/, 'thirty seconds')
  self.voice command
end

#resetObject



252
253
254
255
256
257
# File 'lib/alexa_hue/hue_switch.rb', line 252

def reset
  self.command = ""
  self._group = "0"
  self.body = {}
  self.schedule_params = nil
end

#saturation(depth) ⇒ Object



103
104
105
106
# File 'lib/alexa_hue/hue_switch.rb', line 103

def saturation(depth)
  self.body.delete(:scene)
  self.body[:sat] = depth
end

#save_scene(scene_name) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/alexa_hue/hue_switch.rb', line 150

def save_scene(scene_name)
  scene_name.gsub!(' ','-')
  self.fade 2 if self.body[:transitiontime] == nil
  if self._group.empty?
    light_group = HTTParty.get("http://#{@ip}/api/#{@user}/groups/0")["lights"]
  else
    light_group = HTTParty.get("http://#{@ip}/api/#{@user}/groups/#{self._group}")["lights"]
  end
  params = {name: scene_name, lights: light_group, transitiontime: self.body[:transitiontime]}
  response = HTTParty.put("http://#{@ip}/api/#{@user}/scenes/#{scene_name}", :body => params.to_json)
  confirm if response.first.keys[0] == "success"
end

#scene(scene_name) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/alexa_hue/hue_switch.rb', line 137

def scene(scene_name)
  clear_attributes
  scene_details = self.list_scenes[scene_name]
  self.lights_array = scene_details["lights"]
  self._group = "0"
  self.body[:scene] = scene_details["id"]
end

#scene_on_offObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/alexa_hue/hue_switch.rb', line 171

def scene_on_off
  if self.body[:on] == true
    HTTParty.put("http://#{@ip}/api/#{@user}/groups/#{self._group}/action", :body => (self.body.select { |s| s == :scene }).to_json)
  elsif self.body[:on] == false
    # turn off individual lights in the scene
    (HTTParty.get("http://#{@ip}/api/#{@user}/scenes"))[self.body[:scene]]["lights"].each do |l|
      puts self.body
      HTTParty.put("http://#{@ip}/api/#{@user}/lights/#{l}/state", :body => (self.body).to_json)
    end
  end
end

#schedule(string, on_or_off = :default) ⇒ Object

Parses times in words (e.g., “eight forty five”) to standard HH:MM format



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/alexa_hue/hue_switch.rb', line 199

def schedule (string, on_or_off = :default)
  self.body[:on] = true if on_or_off == :on
  self.body[:on] = false if on_or_off == :off
  set_time = set_time(string)
  if set_time < Time.now
    p "You've scheduled this in the past"
  else
    set_time = set_time.to_s.split(' ')[0..1].join(' ').sub(' ',"T")
    self.schedule_params = {:name=>"Hue_Switch Alarm",
           :description=>"",
           :localtime=>"#{set_time}",
           :status=>"enabled",
           :autodelete=>true
          }
    if self.lights_array.any?
      lights_array.each do |l|
        self.schedule_params[:command] = {:address=>"/api/#{@user}/lights/#{l}/state", :method=>"PUT", :body=>self.body}
      end
    else
      self.schedule_params[:command] = {:address=>"/api/#{@user}/groups/#{self._group}/action", :method=>"PUT", :body=>self.body}
    end
    self.schedule_ids.push(HTTParty.post("http://#{@ip}/api/#{@user}/schedules", :body => (self.schedule_params).to_json))
    confirm if self.schedule_ids.flatten.last.include?("success")
  end
end

#voice(string) ⇒ Object

The rest of the methods allow access to most of the Switch class functionality by supplying a single string



274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/alexa_hue/hue_switch.rb', line 274

def voice(string)
  puts string
  self.reset
  self.command << string

  parse_voice(string)

  if self.command.include?("schedule")
    state = string.match(/off|on/)[0].to_sym rescue nil
    self.send("schedule", *[string, state])
  else
    string.include?(' off') ? self.send("off") : self.send("on")
  end
end

#wake_wordsObject

The following two methods are required to use Switch with Zach Feldman’s Alexa-home*



260
261
262
# File 'lib/alexa_hue/hue_switch.rb', line 260

def wake_words
  ["light", "lights", "scene", "seen"]
end