Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base Sensor for the Xbox Integration."""
2 
3 from __future__ import annotations
4 
5 from yarl import URL
6 
7 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
8 from homeassistant.helpers.update_coordinator import CoordinatorEntity
9 
10 from .const import DOMAIN
11 from .coordinator import PresenceData, XboxUpdateCoordinator
12 
13 
14 class XboxBaseEntity(CoordinatorEntity[XboxUpdateCoordinator]):
15  """Base Sensor for the Xbox Integration."""
16 
17  def __init__(
18  self, coordinator: XboxUpdateCoordinator, xuid: str, attribute: str
19  ) -> None:
20  """Initialize Xbox binary sensor."""
21  super().__init__(coordinator)
22  self.xuidxuid = xuid
23  self.attributeattribute = attribute
24  self._attr_unique_id_attr_unique_id = f"{xuid}_{attribute}"
25  self._attr_entity_registry_enabled_default_attr_entity_registry_enabled_default = attribute == "online"
26  self._attr_device_info_attr_device_info = DeviceInfo(
27  entry_type=DeviceEntryType.SERVICE,
28  identifiers={(DOMAIN, "xbox_live")},
29  manufacturer="Microsoft",
30  model="Xbox Live",
31  name="Xbox Live",
32  )
33 
34  @property
35  def data(self) -> PresenceData | None:
36  """Return coordinator data for this console."""
37  return self.coordinator.data.presence.get(self.xuidxuid)
38 
39  @property
40  def name(self) -> str | None:
41  """Return the name of the sensor."""
42  if not self.datadatadatadata:
43  return None
44 
45  if self.attributeattribute == "online":
46  return self.datadatadatadata.gamertag
47 
48  attr_name = " ".join([part.title() for part in self.attributeattribute.split("_")])
49  return f"{self.data.gamertag} {attr_name}"
50 
51  @property
52  def entity_picture(self) -> str | None:
53  """Return the gamer pic."""
54  if not self.datadatadatadata:
55  return None
56 
57  # Xbox sometimes returns a domain that uses a wrong certificate which
58  # creates issues with loading the image.
59  # The correct domain is images-eds-ssl which can just be replaced
60  # to point to the correct image, with the correct domain and certificate.
61  # We need to also remove the 'mode=Padding' query because with it,
62  # it results in an error 400.
63  url = URL(self.datadatadatadata.display_pic)
64  if url.host == "images-eds.xboxlive.com":
65  url = url.with_host("images-eds-ssl.xboxlive.com").with_scheme("https")
66  query = dict(url.query)
67  query.pop("mode", None)
68  return str(url.with_query(query))
None __init__(self, XboxUpdateCoordinator coordinator, str xuid, str attribute)
Definition: entity.py:19