Class: 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.



27
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
# File 'lib/alexa_hue/hue_switch.rb', line 27

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.



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

def _group
  @_group
end

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#commandObject

Returns the value of attribute command.



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

def command
  @command
end

#lights_arrayObject

Returns the value of attribute lights_array.



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

def lights_array
  @lights_array
end

#schedule_idsObject

Returns the value of attribute schedule_ids.



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

def schedule_ids
  @schedule_ids
end

#schedule_paramsObject

Returns the value of attribute schedule_params.



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

def schedule_params
  @schedule_params
end

Instance Method Details

#alert(value) ⇒ Object



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

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



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

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

#clear_attributesObject



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

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

#color(color_name) ⇒ Object



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

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



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

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



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

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

#delete_schedules!Object



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

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



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

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

#group_on_offObject



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

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



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

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

#light(*args) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#offObject



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

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



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

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



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

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



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

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

#saturation(depth) ⇒ Object



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

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

#save_scene(scene_name) ⇒ Object



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

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



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

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



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

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



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

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



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

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*



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

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