diff --git a/homeassistant/components/steam_online/config_flow.py b/homeassistant/components/steam_online/config_flow.py index 91965685969..338b0a80fb6 100644 --- a/homeassistant/components/steam_online/config_flow.py +++ b/homeassistant/components/steam_online/config_flow.py @@ -174,11 +174,14 @@ class SteamOptionsFlowHandler(config_entries.OptionsFlow): } await self.hass.config_entries.async_reload(self.entry.entry_id) return self.async_create_entry(title="", data=channel_data) + error = None try: users = { name["steamid"]: name["personaname"] for name in await self.hass.async_add_executor_job(self.get_accounts) } + if not users: + error = {"base": "unauthorized"} except steam.api.HTTPTimeoutError: users = self.options[CONF_ACCOUNTS] @@ -191,12 +194,17 @@ class SteamOptionsFlowHandler(config_entries.OptionsFlow): } self.options[CONF_ACCOUNTS] = users | self.options[CONF_ACCOUNTS] - return self.async_show_form(step_id="init", data_schema=vol.Schema(options)) + return self.async_show_form( + step_id="init", data_schema=vol.Schema(options), errors=error + ) def get_accounts(self) -> list[dict[str, str | int]]: """Get accounts.""" interface = steam.api.interface("ISteamUser") - friends = interface.GetFriendList(steamid=self.entry.data[CONF_ACCOUNT]) - _users_str = [user["steamid"] for user in friends["friendslist"]["friends"]] + try: + friends = interface.GetFriendList(steamid=self.entry.data[CONF_ACCOUNT]) + _users_str = [user["steamid"] for user in friends["friendslist"]["friends"]] + except steam.api.HTTPError: + return [] names = interface.GetPlayerSummaries(steamids=_users_str) return names["response"]["players"]["player"] diff --git a/homeassistant/components/steam_online/strings.json b/homeassistant/components/steam_online/strings.json index 6d80bb77f1b..1b431795ea4 100644 --- a/homeassistant/components/steam_online/strings.json +++ b/homeassistant/components/steam_online/strings.json @@ -31,6 +31,9 @@ "accounts": "Names of accounts to be monitored" } } + }, + "error": { + "unauthorized": "Friends list restricted: Please refer to the documentation on how to see all other friends" } } } diff --git a/homeassistant/components/steam_online/translations/en.json b/homeassistant/components/steam_online/translations/en.json index 990a33dbeff..f93a517b241 100644 --- a/homeassistant/components/steam_online/translations/en.json +++ b/homeassistant/components/steam_online/translations/en.json @@ -31,6 +31,9 @@ "accounts": "Names of accounts to be monitored" } } + }, + "error": { + "unauthorized": "Friends list restricted: Please refer to the documentation on how to see all other friends" } } } \ No newline at end of file diff --git a/tests/components/steam_online/__init__.py b/tests/components/steam_online/__init__.py index 27958b76576..4c8c398502f 100644 --- a/tests/components/steam_online/__init__.py +++ b/tests/components/steam_online/__init__.py @@ -1,6 +1,8 @@ """Tests for Steam integration.""" from unittest.mock import patch +import steam + from homeassistant.components.steam_online import DOMAIN from homeassistant.components.steam_online.const import CONF_ACCOUNT, CONF_ACCOUNTS from homeassistant.const import CONF_API_KEY @@ -96,11 +98,24 @@ class MockedInterface(dict): return {"response": {"player_level": 10}} +class MockedInterfacePrivate(MockedInterface): + """Mocked interface for private friends list.""" + + def GetFriendList(self, steamid: str) -> None: + """Get friend list.""" + raise steam.api.HTTPError + + def patch_interface() -> MockedInterface: """Patch interface.""" return patch("steam.api.interface", return_value=MockedInterface()) +def patch_interface_private() -> MockedInterfacePrivate: + """Patch interface for private friends list.""" + return patch("steam.api.interface", return_value=MockedInterfacePrivate()) + + def patch_user_interface_null() -> MockedUserInterfaceNull: """Patch player interface with no players.""" return patch("steam.api.interface", return_value=MockedUserInterfaceNull()) diff --git a/tests/components/steam_online/test_config_flow.py b/tests/components/steam_online/test_config_flow.py index 6d8a16f35f7..c4504bf1641 100644 --- a/tests/components/steam_online/test_config_flow.py +++ b/tests/components/steam_online/test_config_flow.py @@ -21,6 +21,7 @@ from . import ( CONF_OPTIONS_2, create_entry, patch_interface, + patch_interface_private, patch_user_interface_null, ) @@ -225,3 +226,22 @@ async def test_options_flow_timeout(hass: HomeAssistant) -> None: assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["data"] == CONF_OPTIONS + + +async def test_options_flow_unauthorized(hass: HomeAssistant) -> None: + """Test updating options when user's friends list is not public.""" + entry = create_entry(hass) + with patch_interface_private(): + result = await hass.config_entries.options.async_init(entry.entry_id) + + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["step_id"] == "init" + + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={CONF_ACCOUNTS: [ACCOUNT_1]}, + ) + await hass.async_block_till_done() + + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["data"] == CONF_OPTIONS