From 5c1d16d582911f3780b1147fd27852cfb9af819b Mon Sep 17 00:00:00 2001 From: Manu <4445816+tr4nt0r@users.noreply.github.com> Date: Sat, 9 Aug 2025 07:44:35 +0200 Subject: [PATCH] Abort config flow if user has no friends in PlayStation Network (#150301) --- .../playstation_network/config_flow.py | 4 ++++ .../playstation_network/strings.json | 3 ++- .../playstation_network/test_config_flow.py | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/playstation_network/config_flow.py b/homeassistant/components/playstation_network/config_flow.py index d4822225c61..d7d82292378 100644 --- a/homeassistant/components/playstation_network/config_flow.py +++ b/homeassistant/components/playstation_network/config_flow.py @@ -202,6 +202,9 @@ class FriendSubentryFlowHandler(ConfigSubentryFlow): } ) + if not self.friends_list: + return self.async_abort(reason="no_friends") + options = [ SelectOptionDict( value=friend.account_id, @@ -209,6 +212,7 @@ class FriendSubentryFlowHandler(ConfigSubentryFlow): ) for friend in self.friends_list.values() ] + return self.async_show_form( step_id="user", data_schema=self.add_suggested_values_to_schema( diff --git a/homeassistant/components/playstation_network/strings.json b/homeassistant/components/playstation_network/strings.json index 26a1b336e2d..15b83b7cd0d 100644 --- a/homeassistant/components/playstation_network/strings.json +++ b/homeassistant/components/playstation_network/strings.json @@ -69,7 +69,8 @@ }, "abort": { "already_configured_as_entry": "Already configured as a service. This account cannot be added as a friend.", - "already_configured": "Already configured as a friend in this or another account." + "already_configured": "Already configured as a friend in this or another account.", + "no_friends": "Looks like your friend list is empty right now. Add friends on PlayStation Network first." } } }, diff --git a/tests/components/playstation_network/test_config_flow.py b/tests/components/playstation_network/test_config_flow.py index 4194f1fb258..0cd94fe153a 100644 --- a/tests/components/playstation_network/test_config_flow.py +++ b/tests/components/playstation_network/test_config_flow.py @@ -493,3 +493,27 @@ async def test_add_friend_flow_already_configured_as_entry( assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured_as_entry" + + +async def test_add_friend_flow_no_friends( + hass: HomeAssistant, + config_entry: MockConfigEntry, + mock_psnawpapi: MagicMock, +) -> None: + """Test we abort add friend subentry flow when the user has no friends.""" + + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.LOADED + + mock_psnawpapi.user.return_value.friends_list.return_value = [] + + result = await hass.config_entries.subentries.async_init( + (config_entry.entry_id, "friend"), + context={"source": SOURCE_USER}, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "no_friends"