Warn user if Steam friends list is restricted (#72285)

This commit is contained in:
Robert Hillis 2022-05-23 10:11:17 -04:00 committed by GitHub
parent 5cfb31d28a
commit 204e26a1b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 3 deletions

View file

@ -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"]

View file

@ -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"
}
}
}

View file

@ -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"
}
}
}

View file

@ -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())

View file

@ -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