Class: Hue::Group

Inherits:
Object
  • Object
show all
Includes:
Enumerable, EditableState, TranslateKeys
Defined in:
lib/hue/group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EditableState

#off!, #on!, #on?, #set_xy

Methods included from TranslateKeys

#translate_keys, #unpack_hash

Constructor Details

#initialize(client, bridge, id = nil, data = {}) ⇒ Group


48
49
50
51
52
53
54
# File 'lib/hue/group.rb', line 48

def initialize(client, bridge, id = nil, data = {})
  @client = client
  @bridge = bridge
  @id = id

  unpack(data)
end

Instance Attribute Details

#bridgeObject (readonly)

Bridge the group is associated with


11
12
13
# File 'lib/hue/group.rb', line 11

def bridge
  @bridge
end

#brightnessObject

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.


27
28
29
# File 'lib/hue/group.rb', line 27

def brightness
  @brightness
end

#color_temperatureObject

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


43
44
45
# File 'lib/hue/group.rb', line 43

def color_temperature
  @color_temperature
end

#hueObject

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.


18
19
20
# File 'lib/hue/group.rb', line 18

def hue
  @hue
end

#idObject (readonly)

Unique identification number.


8
9
10
# File 'lib/hue/group.rb', line 8

def id
  @id
end

#nameObject

A unique, editable name given to the group.


14
15
16
# File 'lib/hue/group.rb', line 14

def name
  @name
end

#saturationObject

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


22
23
24
# File 'lib/hue/group.rb', line 22

def saturation
  @saturation
end

#typeObject (readonly)

A fixed name describing the type of group.


46
47
48
# File 'lib/hue/group.rb', line 46

def type
  @type
end

#xObject (readonly)

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


32
33
34
# File 'lib/hue/group.rb', line 32

def x
  @x
end

#yObject (readonly)

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


37
38
39
# File 'lib/hue/group.rb', line 37

def y
  @y
end

Instance Method Details

#<<(light_id) ⇒ Object Also known as: add_light


89
90
91
92
# File 'lib/hue/group.rb', line 89

def <<(light_id)
  @light_ids << light_id
  set_group_state({:lights => @light_ids})
end

#create!Object


121
122
123
124
125
126
127
128
129
130
131
132
133
# 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!Object


135
136
137
138
139
140
141
# 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) ⇒ Object


56
57
58
# File 'lib/hue/group.rb', line 56

def each(&block)
  lights.each(&block)
end

#lightsObject


60
61
62
63
64
65
66
# 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) ⇒ Object


73
74
75
76
77
78
79
80
81
82
# 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

#new?Boolean


143
144
145
# File 'lib/hue/group.rb', line 143

def new?
  @id.nil?
end

#refreshObject


115
116
117
118
119
# 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) ⇒ Object


84
85
86
87
# 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) ⇒ Object


95
96
97
98
99
100
101
102
103
# 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) ⇒ Object


105
106
107
108
109
110
111
112
113
# 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