Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Xbox friends binary sensors."""
2 
3 from __future__ import annotations
4 
5 from functools import partial
6 
7 from homeassistant.components.binary_sensor import BinarySensorEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers import entity_registry as er
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .const import DOMAIN
14 from .coordinator import XboxUpdateCoordinator
15 from .entity import XboxBaseEntity
16 
17 PRESENCE_ATTRIBUTES = ["online", "in_party", "in_game", "in_multiplayer"]
18 
19 
21  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
22 ) -> None:
23  """Set up Xbox Live friends."""
24  coordinator: XboxUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
25  "coordinator"
26  ]
27 
28  update_friends = partial(async_update_friends, coordinator, {}, async_add_entities)
29 
30  unsub = coordinator.async_add_listener(update_friends)
31  hass.data[DOMAIN][entry.entry_id]["binary_sensor_unsub"] = unsub
32  update_friends()
33 
34 
36  """Representation of a Xbox presence state."""
37 
38  @property
39  def is_on(self) -> bool:
40  """Return the status of the requested attribute."""
41  if not self.coordinator.last_update_success:
42  return False
43 
44  return getattr(self.datadatadatadata, self.attributeattribute, False)
45 
46 
47 @callback
49  coordinator: XboxUpdateCoordinator,
50  current: dict[str, list[XboxBinarySensorEntity]],
51  async_add_entities,
52 ) -> None:
53  """Update friends."""
54  new_ids = set(coordinator.data.presence)
55  current_ids = set(current)
56 
57  # Process new favorites, add them to Home Assistant
58  new_entities: list[XboxBinarySensorEntity] = []
59  for xuid in new_ids - current_ids:
60  current[xuid] = [
61  XboxBinarySensorEntity(coordinator, xuid, attribute)
62  for attribute in PRESENCE_ATTRIBUTES
63  ]
64  new_entities = new_entities + current[xuid]
65 
66  async_add_entities(new_entities)
67 
68  # Process deleted favorites, remove them from Home Assistant
69  for xuid in current_ids - new_ids:
70  coordinator.hass.async_create_task(
71  async_remove_entities(xuid, coordinator, current)
72  )
73 
74 
76  xuid: str,
77  coordinator: XboxUpdateCoordinator,
78  current: dict[str, list[XboxBinarySensorEntity]],
79 ) -> None:
80  """Remove friend sensors from Home Assistant."""
81  registry = er.async_get(coordinator.hass)
82  entities = current[xuid]
83  for entity in entities:
84  if entity.entity_id in registry.entities:
85  registry.async_remove(entity.entity_id)
86  del current[xuid]
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
None async_update_friends(XboxUpdateCoordinator coordinator, dict[str, list[XboxBinarySensorEntity]] current, async_add_entities)
None async_remove_entities(str xuid, XboxUpdateCoordinator coordinator, dict[str, list[XboxBinarySensorEntity]] current)