class Hue::Group

Constants

GROUP_KEYS_MAP
STATE_KEYS_MAP

Attributes

bridge[R]

Bridge the group is associated with

brightness[RW]

Brightness of the group. This is a scale from the minimum brightness the group is capable of, 0, to the maximum capable brightness, 255. Note a brightness of 0 is not off.

color_temperature[RW]

The Mired Color temperature of the light. 2012 connected lights are capable of 153 (6500K) to 500 (2000K).

@see en.wikipedia.org/wiki/Mired

hue[RW]

Hue of the group. This is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue.

id[R]

Unique identification number.

name[RW]

A unique, editable name given to the group.

saturation[RW]

Saturation of the group. 255 is the most saturated (colored) and 0 is the least saturated (white).

type[R]

A fixed name describing the type of group.

x[R]

The x coordinate of a color in CIE color space. Between 0 and 1.

@see developers.meethue.com/coreconcepts.html#color_gets_more_complicated

y[R]

The y coordinate of a color in CIE color space. Between 0 and 1.

@see developers.meethue.com/coreconcepts.html#color_gets_more_complicated

Public Class Methods

new(client, bridge, id = nil, data = {}) click to toggle source
# File lib/hue/group.rb, line 48
def initialize(client, bridge, id = nil, data = {})
  @client = client
  @bridge = bridge
  @id = id

  unpack(data)
end

Public Instance Methods

<<(light_id) click to toggle source
# File lib/hue/group.rb, line 89
def <<(light_id)
  @light_ids << light_id
  set_group_state({:lights => @light_ids})
end
Also aliased as: add_light
add_light(light_id)
Alias for: <<
create!() click to toggle source
# File lib/hue/group.rb, line 121
def create!
  body = {
    :name => @name,
    :lights => @light_ids,
  }

  uri = URI.parse("http://#{@bridge.ip}/api/#{@client.username}/groups")
  http = Net::HTTP.new(uri.host)
  response = http.request_post(uri.path, JSON.dump(body))
  json = JSON(response.body)

  @id = json[0]['success']['id']
end
destroy!() click to toggle source
# File lib/hue/group.rb, line 135
def destroy!
  uri = URI.parse(base_url)
  http = Net::HTTP.new(uri.host)
  response = http.delete(uri.path)
  json = JSON(response.body)
  @id = nil if json[0]['success']
end
each(&block) click to toggle source
# File lib/hue/group.rb, line 56
def each(&block)
  lights.each(&block)
end
lights() click to toggle source
# File lib/hue/group.rb, line 60
def lights
  @lights ||= begin
    @light_ids.map do |light_id|
      @client.light(light_id)
    end
  end
end
lights=(light_ids) click to toggle source
# File lib/hue/group.rb, line 73
def lights=(light_ids)
  light_ids.map! do |light_id|
    light_id.is_a?(Light) ? light_id.id : light_id.to_s
  end

  @light_ids = light_ids.uniq
  @lights = nil # resets the memoization

  set_group_state({:lights => @light_ids})
end
name=(name) click to toggle source
# File lib/hue/group.rb, line 68
def name=(name)
  resp = set_group_state({:name => name})
  @name = new? ? name : resp[0]['success']["/groups/#{id}/name"]
end
new?() click to toggle source
# File lib/hue/group.rb, line 143
def new?
  @id.nil?
end
refresh() click to toggle source
# File lib/hue/group.rb, line 115
def refresh
  json = JSON(Net::HTTP.get(URI.parse(base_url)))
  unpack(json)
  @lights = nil
end
scene=(scene) click to toggle source
# File lib/hue/group.rb, line 84
def scene=(scene)
  scene_id = scene.is_a?(Scene) ? scene.id : scene
  set_group_state({:scene => scene_id})
end
set_group_state(attributes) click to toggle source
# File lib/hue/group.rb, line 95
def set_group_state(attributes)
  return if new?
  body = translate_keys(attributes, GROUP_KEYS_MAP)

  uri = URI.parse(base_url)
  http = Net::HTTP.new(uri.host)
  response = http.request_put(uri.path, JSON.dump(body))
  JSON(response.body)
end
set_state(attributes) click to toggle source
# File lib/hue/group.rb, line 105
def set_state(attributes)
  return if new?
  body = translate_keys(attributes, STATE_KEYS_MAP)

  uri = URI.parse("#{base_url}/action")
  http = Net::HTTP.new(uri.host)
  response = http.request_put(uri.path, JSON.dump(body))
  JSON(response.body)
end

Private Instance Methods

base_url() click to toggle source
# File lib/hue/group.rb, line 177
def base_url
  "http://#{@bridge.ip}/api/#{@client.username}/groups/#{id}"
end
unpack(data) click to toggle source
# File lib/hue/group.rb, line 168
def unpack(data)
  unpack_hash(data, GROUP_KEYS_MAP)

  unless new?
    unpack_hash(@state, STATE_KEYS_MAP)
    @x, @y = @state['xy']
  end
end