Class: Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hue_switch.rb', line 10

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

  @user = "1234567890"
  begin
    Timeout::timeout(5) {
      @ip = HTTParty.get("https://www.meethue.com/api/nupnp").first["internalipaddress"]
    }
  rescue Timeout::Error
      "Time Out"
  rescue NoMethodError
     "Cannot Find Bridge"
  rescue Errno::ECONNREFUSED
     "connection refused"
  rescue SocketError
      "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.



9
10
11
# File 'lib/hue_switch.rb', line 9

def _group
  @_group
end

#bodyObject

Returns the value of attribute body.



9
10
11
# File 'lib/hue_switch.rb', line 9

def body
  @body
end

#commandObject

Returns the value of attribute command.



9
10
11
# File 'lib/hue_switch.rb', line 9

def command
  @command
end

#lights_arrayObject

Returns the value of attribute lights_array.



9
10
11
# File 'lib/hue_switch.rb', line 9

def lights_array
  @lights_array
end

#schedule_idsObject

Returns the value of attribute schedule_ids.



9
10
11
# File 'lib/hue_switch.rb', line 9

def schedule_ids
  @schedule_ids
end

#schedule_paramsObject

Returns the value of attribute schedule_params.



9
10
11
# File 'lib/hue_switch.rb', line 9

def schedule_params
  @schedule_params
end

Instance Method Details

#alert(value) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/hue_switch.rb', line 196

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



63
64
65
66
# File 'lib/hue_switch.rb', line 63

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

#clear_attributesObject



68
69
70
71
72
# File 'lib/hue_switch.rb', line 68

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

#color(color_name) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/hue_switch.rb', line 49

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



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

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



99
100
101
102
# File 'lib/hue_switch.rb', line 99

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

#delete_schedules!Object



179
180
181
182
183
184
185
186
# File 'lib/hue_switch.rb', line 179

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



74
75
76
# File 'lib/hue_switch.rb', line 74

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

#group_on_offObject



121
122
123
# File 'lib/hue_switch.rb', line 121

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



39
40
41
42
# File 'lib/hue_switch.rb', line 39

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

#light(*args) ⇒ Object



78
79
80
81
82
83
# File 'lib/hue_switch.rb', line 78

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



85
86
87
88
89
90
# File 'lib/hue_switch.rb', line 85

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



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

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

#mired(numeric_value) ⇒ Object



44
45
46
47
# File 'lib/hue_switch.rb', line 44

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

#offObject



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

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



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

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



218
219
220
221
222
223
224
# File 'lib/hue_switch.rb', line 218

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



206
207
208
209
210
211
# File 'lib/hue_switch.rb', line 206

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

#saturation(depth) ⇒ Object



58
59
60
61
# File 'lib/hue_switch.rb', line 58

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

#save_scene(scene_name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hue_switch.rb', line 104

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



92
93
94
95
96
97
# File 'lib/hue_switch.rb', line 92

def scene(scene_name)
  clear_attributes
  self.lights_array = []
  self._group = "0"
  self.body[:scene] = scene_name.to_s
end

#scene_on_offObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/hue_switch.rb', line 125

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/hue_switch.rb', line 153

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



228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/hue_switch.rb', line 228

def voice(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*



214
215
216
# File 'lib/hue_switch.rb', line 214

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