Class: Hue::Client

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/alexa_hue/hue/client.rb

Direct Known Subclasses

JsClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#fix_schedule_syntax, #numbers_to_times, #parse_time, #set_time

Constructor Details

#initialize(options = {}, &block) ⇒ Client

Returns a new instance of Client.



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
38
39
40
41
42
43
44
45
46
47
# File 'lib/alexa_hue/hue/client.rb', line 12

def initialize(options={}, &block)
  # JUST UPNP SUPPORT FOR NOW
  @bridge_ip = HTTParty.get("https://www.meethue.com/api/nupnp").first["internalipaddress"] rescue nil
  @user = "1234567890"
  @groups, @lights, @scenes = {}, {}, []
  prefix = "/api/#{@user}"
  schemas = {
              get: {
                all_lights: "#{prefix}/groups/0",
                group: "#{prefix}/groups/{{group}}",
                root: "#{prefix}/"
              },
              put: {
                scene: "#{prefix}/scenes/{{scene}}",
                light: "#{prefix}/lights/{{lights}}/state",
                group: "#{prefix}/groups/{{group}}/action",
                all_lights: "#{prefix}/groups/0"
              },
              delete: {
                schedule: "#{prefix}/schedules/{{schedule}}"
              },
              post: {
                root: "#{prefix}/"
              }
            }
  
  @client = Takeout::Client.new(uri: @bridge_ip, endpoint_prefix: prefix, schemas: schemas, debug: true, headers: { "Expect" => "100-continue" })
  
  authorize_user
  populate_client
  
  @lights_array, @schedule_ids, @schedule_params, @command, @_group, @body = [], [], "", "0", Hue::RequestBody.new
  
  # TODO: Do blocks right
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#_groupObject

Returns the value of attribute _group.



10
11
12
# File 'lib/alexa_hue/hue/client.rb', line 10

def _group
  @_group
end

#bridge_ipObject

Returns the value of attribute bridge_ip.



10
11
12
# File 'lib/alexa_hue/hue/client.rb', line 10

def bridge_ip
  @bridge_ip
end

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/alexa_hue/hue/client.rb', line 10

def client
  @client
end

#commandObject

Returns the value of attribute command.



10
11
12
# File 'lib/alexa_hue/hue/client.rb', line 10

def command
  @command
end

#groupsObject

Returns the value of attribute groups.



10
11
12
# File 'lib/alexa_hue/hue/client.rb', line 10

def groups
  @groups
end

#lights(group_name) ⇒ Object

Returns the value of attribute lights.



10
11
12
# File 'lib/alexa_hue/hue/client.rb', line 10

def lights
  @lights
end

#scenesObject

Returns the value of attribute scenes.



10
11
12
# File 'lib/alexa_hue/hue/client.rb', line 10

def scenes
  @scenes
end

#schedule_idsObject

Returns the value of attribute schedule_ids.



10
11
12
# File 'lib/alexa_hue/hue/client.rb', line 10

def schedule_ids
  @schedule_ids
end

#schedule_paramsObject

Returns the value of attribute schedule_params.



10
11
12
# File 'lib/alexa_hue/hue/client.rb', line 10

def schedule_params
  @schedule_params
end

#userObject

Returns the value of attribute user.



10
11
12
# File 'lib/alexa_hue/hue/client.rb', line 10

def user
  @user
end

Instance Method Details

#alert(value) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/alexa_hue/hue/client.rb', line 179

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

#brightness(depth) ⇒ Object



75
76
77
78
# File 'lib/alexa_hue/hue/client.rb', line 75

def brightness(depth)
  @body.clear_scene
  @body.bri = depth
end

#color(color_name) ⇒ Object



63
64
65
66
67
68
# File 'lib/alexa_hue/hue/client.rb', line 63

def color(color_name)
  @body.reset
  @body.hue = @colors.keys.include?(color_name.to_sym) ? 
                    @colors[color_name.to_sym] :
                    @mired_colors[color_name.to_sym]
end

#colorloop(start_or_stop) ⇒ Object



175
176
177
# File 'lib/alexa_hue/hue/client.rb', line 175

def colorloop(start_or_stop)
  @body.effect = (start_or_stop == :start) ? "colorloop" : "none"
end

#confirmObject



49
50
51
# File 'lib/alexa_hue/hue/client.rb', line 49

def confirm
  @client.put_all_lights(alert: 'select')
end

#delete_schedules!Object



169
170
171
172
173
# File 'lib/alexa_hue/hue/client.rb', line 169

def delete_schedules!
  @schedule_ids.flatten!
  @schedule_ids.each { |k| @client.delete_schedule(schedule: k.dig("success","id")) }
  @schedule_ids = []
end

#fade(in_seconds) ⇒ Object



80
81
82
# File 'lib/alexa_hue/hue/client.rb', line 80

def fade(in_seconds)
  @body.transitiontime = in_seconds * 10
end

#hue(numeric_value) ⇒ Object



53
54
55
56
# File 'lib/alexa_hue/hue/client.rb', line 53

def hue(numeric_value)
  @body.reset
  @body.hue = numeric_value
end

#light(*args) ⇒ Object



84
85
86
87
88
89
# File 'lib/alexa_hue/hue/client.rb', line 84

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

#mired(numeric_value) ⇒ Object



58
59
60
61
# File 'lib/alexa_hue/hue/client.rb', line 58

def mired(numeric_value)
  @body.reset
  @body.ct = numeric_value
end

#offObject



143
144
145
146
# File 'lib/alexa_hue/hue/client.rb', line 143

def off
  @body.on = false
  toggle_system
end

#onObject



138
139
140
141
# File 'lib/alexa_hue/hue/client.rb', line 138

def on
  @body.on = true
  toggle_system
end

#resetObject



189
190
191
# File 'lib/alexa_hue/hue/client.rb', line 189

def reset
  @command, @_group, @body, @schedule_params = "", "0", Hue::RequestBody.new, nil
end

#saturation(depth) ⇒ Object



70
71
72
73
# File 'lib/alexa_hue/hue/client.rb', line 70

def saturation(depth)
  @body.clear_scene
  @body.sat = depth
end

#save_scene(scene_name) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/alexa_hue/hue/client.rb', line 106

def save_scene(scene_name)
  fade(2) if @body.transitiontime == nil
  light_group = @_group.empty? ? @client.get_all_lights.body["lights"] : @client.get_group(group: @_group).body["lights"]
  params = {name: scene_name.gsub!(' ','-'), lights: light_group, transitiontime: @body.transitiontime}
  response = @client.put_scene(scene: scene_name, options: params).body
  confirm if response.first.keys[0] == "success"
end

#scene(scene_name) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/alexa_hue/hue/client.rb', line 98

def scene(scene_name)
  @body.reset
  scene_details = @scenes[scene_name]
  @lights_array = scene_details["lights"]
  @_group = "0"
  @body.scene = scene_details["id"]
end

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/alexa_hue/hue/client.rb', line 148

def schedule(string, on_or_off = :default)
  @body.on = (on_or_off == :on)
  set_time = set_time(string)
  unless set_time < Time.now
    set_time = set_time.to_s.split(' ')[0..1].join(' ').sub(' ',"T")
    @schedule_params = {:name=>"Hue_Switch Alarm",
           :description=>"",
           :localtime=>"#{set_time}",
           :status=>"enabled",
           :autodelete=>true
          }
    if @lights_array.any?
      lights_array.each {|l| @schedule_params[:command] = {:address=>"/api/#{@user}/lights/#{l}/state", :method=>"PUT", :body=>@body} }
    else
      @schedule_params[:command] = {:address=>"/api/#{@user}/groups/#{@_group}/action", :method=>"PUT", :body=>@body}
    end
    @schedule_ids.push(@client.post_schedules(@schedule_params).body)
    confirm if @schedule_ids.flatten.last.include?("success")
  end
end

#toggle_groupObject



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

def toggle_group
  @client.put_group({group: @_group}.merge(@body.to_hash(without_scene: true)))
end

#toggle_lightsObject



114
115
116
# File 'lib/alexa_hue/hue/client.rb', line 114

def toggle_lights
  @lights_array.each { |l| @client.put_lights({lights: l}.merge(@body.to_hash)) }
end

#toggle_sceneObject



122
123
124
125
126
127
128
129
130
# File 'lib/alexa_hue/hue/client.rb', line 122

def toggle_scene
  if @body.on
    @client.put_group({group: @_group}.merge(@body.to_hash(without_scene: true)))
  else
    @client.get_scenes[@body[:scene]]["lights"].each do |l|
      @client.put_lights({lights: l}.merge(@body.to_hash))
    end
  end
end

#toggle_systemObject



132
133
134
135
136
# File 'lib/alexa_hue/hue/client.rb', line 132

def toggle_system
  toggle_lights if @lights_array.any? && @body.scene.nil?
  toggle_group if (!@_group.empty? && @body.scene.nil?)
  toggle_scene if @body.scene
end