1 """Coordinator for the xbox integration."""
3 from __future__
import annotations
5 from contextlib
import suppress
6 from dataclasses
import dataclass
7 from datetime
import timedelta
10 from xbox.webapi.api.client
import XboxLiveClient
11 from xbox.webapi.api.provider.catalog.const
import SYSTEM_PFN_ID_MAP
12 from xbox.webapi.api.provider.catalog.models
import AlternateIdType, Product
13 from xbox.webapi.api.provider.people.models
import (
18 from xbox.webapi.api.provider.smartglass.models
import (
19 SmartglassConsoleList,
20 SmartglassConsoleStatus,
26 from .const
import DOMAIN
28 _LOGGER = logging.getLogger(__name__)
33 """Xbox console status data."""
35 status: SmartglassConsoleStatus
36 app_details: Product |
None
41 """Xbox user presence data."""
52 gold_tenure: str |
None
58 """Xbox dataclass for update coordinator."""
60 consoles: dict[str, ConsoleData]
61 presence: dict[str, PresenceData]
65 """Store Xbox Console Status."""
70 client: XboxLiveClient,
71 consoles: SmartglassConsoleList,
81 self.client: XboxLiveClient = client
82 self.consoles: SmartglassConsoleList = consoles
85 """Fetch the latest console status."""
87 new_console_data: dict[str, ConsoleData] = {}
88 for console
in self.consoles.result:
89 current_state: ConsoleData |
None = self.
datadatadata.consoles.get(console.id)
90 status: SmartglassConsoleStatus = (
91 await self.client.smartglass.get_console_status(console.id)
101 app_details: Product |
None =
None
102 if current_state
is not None:
103 app_details = current_state.app_details
105 if status.focus_app_aumid:
108 or status.focus_app_aumid != current_state.status.focus_app_aumid
110 app_id = status.focus_app_aumid.split(
"!")[0]
111 id_type = AlternateIdType.PACKAGE_FAMILY_NAME
112 if app_id
in SYSTEM_PFN_ID_MAP:
113 id_type = AlternateIdType.LEGACY_XBOX_PRODUCT_ID
114 app_id = SYSTEM_PFN_ID_MAP[app_id][id_type]
116 await self.client.catalog.get_product_from_alternate_id(
120 if catalog_result
and catalog_result.products:
121 app_details = catalog_result.products[0]
126 status=status, app_details=app_details
130 presence_data: dict[str, PresenceData] = {}
131 batch: PeopleResponse = await self.client.people.get_friends_own_batch(
134 own_presence: Person = batch.people[0]
137 friends: PeopleResponse = await self.client.people.get_friends_own()
138 for friend
in friends.people:
139 if not friend.is_favorite:
144 return XboxData(new_console_data, presence_data)
148 """Build presence data from a person."""
149 active_app: PresenceDetail |
None =
None
150 with suppress(StopIteration):
152 presence
for presence
in person.presence_details
if presence.is_primary
157 gamertag=person.gamertag,
158 display_pic=person.display_pic_raw,
159 online=person.presence_state ==
"Online",
160 status=person.presence_text,
161 in_party=person.multiplayer_summary.in_party > 0,
162 in_game=active_app
is not None and active_app.is_game,
163 in_multiplayer=person.multiplayer_summary.in_multiplayer_session,
164 gamer_score=person.gamer_score,
165 gold_tenure=person.detail.tenure,
166 account_tier=person.detail.account_tier,
None __init__(self, HomeAssistant hass, XboxLiveClient client, SmartglassConsoleList consoles)
XboxData _async_update_data(self)
PresenceData _build_presence_data(Person person)