Permalink
Browse files

Improve group detection

  • Loading branch information...
1 parent 4f7d8ea commit 30596d9d88a725d4619357c1bed9f357c3f3976c @soffes soffes committed Jan 3, 2013
Showing with 18 additions and 19 deletions.
  1. +17 −18 lib/sonos/system.rb
  2. +1 −1 test/units/system_test.rb
View
@@ -10,6 +10,9 @@ class System
# @param [Array] the system topology. If this is nil, it will autodiscover.
def initialize(topology = Sonos::Discovery.new.topology)
@topology = topology
+ @groups = []
+ @devices = @topology.collect(&:device)
+
construct_groups
end
@@ -28,29 +31,25 @@ def pause_all
private
def construct_groups
- # Reset
- @groups = []
- @devices = @topology.collect(&:device)
-
# Loop through all of the unique groups
@topology.collect(&:group).uniq.each do |group_uid|
- # Select all of the nodes with this group uid
- nodes = @topology.select do |node|
- node.group == group_uid
- end
-
- next if nodes.empty?
-
- # Find master node
master_uuid = group_uid.split(':').first
- master = nodes.select do |node|
- node.uuid == master_uuid
+ nodes = []
+ master = nil
+
+ @topology.each do |node|
+ # Select all of the nodes with this group uid
+ next unless node.group == group_uid
+
+ if node.uuid == master_uuid
+ master = node
+ else
+ nodes << node
+ end
end
- next unless master.count == 1
- master = master.first
-
- nodes.delete(master)
+ # Skip this group if there are no nodes or master
+ next if nodes.empty? or master.nil?
# Add the group
@groups << Group.new(master.device, nodes.collect(&:device))
@@ -2,7 +2,7 @@
class SystemTest < SonosTest
def test_group_detection
- VCR.use_cassette('topology') do
+ VCR.use_cassette('topology', match_requests_on: [:path]) do
system = Sonos.system
assert_equal 1, system.groups.length
end

0 comments on commit 30596d9

Please sign in to comment.