Skip to content

Commit 36797da

Browse files
committed
Make new group with all players
1 parent c6efca8 commit 36797da

1 file changed

Lines changed: 36 additions & 24 deletions

File tree

lib/pr/sonos_households/group_manager.ex

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ defmodule PR.SonosHouseholds.GroupManager do
1717
end
1818

1919
def check_or_recrate_active_group(
20-
%Group{group_id: active_group_id, player_ids: player_ids},
20+
%Group{group_id: active_group_id} = group,
2121
retries
2222
) do
2323
with {:ok, groups} <- get_sonos_groups(),
2424
{:ok, active_group_id} <-
2525
group_id_present?(groups, active_group_id) do
2626
Logger.info("Group #{active_group_id} still exists on Sonos")
2727

28-
# Resubscribes after groupstatus gone, which i thin unsubscribes
28+
# Resubscribes after groupstatus gone, which i think unsubscribes
2929
# TODO it only matches on group name, so might need to update the group id
3030
# saved here, in case that has changed
3131
Logger.info("Resubscribing webhooks for group #{active_group_id}")
@@ -38,11 +38,10 @@ defmodule PR.SonosHouseholds.GroupManager do
3838

3939
{:error, :group_id_is_gone} ->
4040
Logger.warn(
41-
"Check groups: Group #{active_group_id} isn't present on Sonos. Recreating with player ids",
42-
player_ids: player_ids
41+
"Check groups: Group #{active_group_id} isn't present on Sonos. Recreating with all available players"
4342
)
4443

45-
recreate_group(player_ids)
44+
recreate_group()
4645
:ok
4746

4847
{:error, :cant_get_groups} ->
@@ -53,13 +52,12 @@ defmodule PR.SonosHouseholds.GroupManager do
5352
# So if the group can found after getting here and retrying, probs needs a resubscription (above)
5453
Logger.warn(
5554
"Couldn't retrieve Sonos groups when trying to check_groups. Scheduling retry",
56-
player_ids: player_ids,
5755
active_group_id: active_group_id,
5856
retries: retries
5957
)
6058

6159
# Flag to say its a retry from here, in which case the ok state needs to resubscribe?
62-
{:retry, %Group{id: active_group_id, player_ids: player_ids}, retries - 1}
60+
{:retry, group, retries - 1}
6361

6462
{:error, reason} ->
6563
Logger.error("Check groups: #{Atom.to_string(reason)}")
@@ -76,30 +74,44 @@ defmodule PR.SonosHouseholds.GroupManager do
7674
:ok
7775
end
7876

79-
@spec recreate_group(List.t()) :: {:ok, String.t()} | {:error, String.t()}
80-
defp recreate_group(player_ids) do
77+
@spec recreate_group() :: {:ok, String.t()} | {:error, String.t()}
78+
defp recreate_group do
8179
# Do this before making the new group
8280
Logger.info("Unsubscribing")
8381
SonosAPI.unsubscribe_webhooks()
84-
Logger.info("Trying to create group with player_ids", player_ids: player_ids)
8582

86-
# Make a new group with the player ids from the last saved group
87-
case SonosAPI.create_group(player_ids) do
88-
{:ok, %Group{group_id: id}} ->
89-
SonosAPI.subscribe_webhooks()
90-
Logger.info("New group created #{id}")
91-
{:ok, id}
92-
93-
{:error, :no_household_activated} ->
94-
Logger.error("Couldn't re-create group, no household activated")
95-
{:error, :no_household_activated}
96-
97-
_ ->
98-
Logger.error("Couldn't re-create group")
99-
{:error, :couldnt_recreate_group}
83+
# Get all available players from the household and group them all together
84+
with {:ok, groups} <- get_sonos_groups(),
85+
player_ids <- get_all_player_ids_from_groups(groups) do
86+
Logger.info("Trying to create group with all available player_ids", player_ids: player_ids)
87+
88+
case SonosAPI.create_group(player_ids) do
89+
{:ok, %Group{group_id: id}} ->
90+
SonosAPI.subscribe_webhooks()
91+
Logger.info("New group created #{id}")
92+
{:ok, id}
93+
94+
{:error, :no_household_activated} ->
95+
Logger.error("Couldn't re-create group, no household activated")
96+
{:error, :no_household_activated}
97+
98+
_ ->
99+
Logger.error("Couldn't re-create group")
100+
{:error, :couldnt_recreate_group}
101+
end
102+
else
103+
{:error, reason} ->
104+
Logger.error("Couldn't get groups to recreate group: #{inspect(reason)}")
105+
{:error, :cant_get_groups}
100106
end
101107
end
102108

109+
defp get_all_player_ids_from_groups(groups) do
110+
groups
111+
|> Enum.flat_map(fn group -> Map.get(group, :player_ids, []) end)
112+
|> Enum.uniq()
113+
end
114+
103115
defp group_id_present?(groups, active_group_id) do
104116
if Enum.any?(groups, &match?(%{id: ^active_group_id}, &1)) do
105117
{:ok, active_group_id}

0 commit comments

Comments
 (0)