diff --git a/homeassistant/components/xbox/coordinator.py b/homeassistant/components/xbox/coordinator.py index c10a07f76b44..3a013c15d78e 100644 --- a/homeassistant/components/xbox/coordinator.py +++ b/homeassistant/components/xbox/coordinator.py @@ -213,16 +213,17 @@ class XboxPresenceCoordinator(XboxBaseCoordinator[XboxData]): """Fetch presence data.""" me = await self.client.people.get_friend_by_xuid(self.client.xuid) - friends = await self.client.people.get_friends_own() presence_data = {self.client.xuid: me.people[0]} - presence_data.update( - { - friend.xuid: friend - for friend in friends.people - if friend.xuid in self.friend_subentries() - } - ) + if friend_xuids := self.friend_subentries(): + friends = await self.client.people.get_friends_own() + presence_data.update( + { + friend.xuid: friend + for friend in friends.people + if friend.xuid in friend_xuids + } + ) # retrieve title details for person in presence_data.values(): diff --git a/tests/components/xbox/test_coordinator.py b/tests/components/xbox/test_coordinator.py index 1f16163bb77b..af239f87ecf9 100644 --- a/tests/components/xbox/test_coordinator.py +++ b/tests/components/xbox/test_coordinator.py @@ -123,3 +123,30 @@ async def test_achievement_total_failure_keeps_entities_available( assert state assert state.state != "unavailable" assert state.attributes["achievements"] == "2 / 0" + + +async def test_no_friend_polling_without_friend_subentries( + hass: HomeAssistant, + config_entry: MockConfigEntry, + xbox_live_client: AsyncMock, + freezer: FrozenDateTimeFactory, +) -> None: + """Do not call the rate-limited friends endpoint without tracked friends.""" + entry = MockConfigEntry( + domain=DOMAIN, + title=config_entry.title, + data=config_entry.data, + unique_id=config_entry.unique_id, + minor_version=3, + ) + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + xbox_live_client.people.get_friends_own.assert_not_awaited() + xbox_live_client.people.get_friend_by_xuid.assert_awaited() + assert hass.states.get("sensor.gsr_ae_now_playing") is not None + + freezer.tick(PRESENCE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + xbox_live_client.people.get_friends_own.assert_not_awaited()