Files
homeassistant-core/homeassistant/components/xbox/binary_sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.7 KiB
Python
Raw Normal View History

2020-10-15 19:11:05 -04:00
"""Xbox friends binary sensors."""
2021-03-18 15:08:35 +01:00
from __future__ import annotations
2020-10-15 19:11:05 -04:00
from functools import partial
from homeassistant.components.binary_sensor import BinarySensorEntity
2021-05-27 09:56:20 -04:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
2022-01-07 16:30:01 +01:00
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2020-10-15 19:11:05 -04:00
from .base_sensor import XboxBaseSensorEntity
from .const import DOMAIN
from .coordinator import XboxUpdateCoordinator
2020-10-15 19:11:05 -04:00
PRESENCE_ATTRIBUTES = ["online", "in_party", "in_game", "in_multiplayer"]
2021-05-27 09:56:20 -04:00
async def async_setup_entry(
2022-01-07 16:30:01 +01:00
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
2020-10-15 19:11:05 -04:00
"""Set up Xbox Live friends."""
2021-05-27 09:56:20 -04:00
coordinator: XboxUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
2020-10-15 19:11:05 -04:00
"coordinator"
]
update_friends = partial(async_update_friends, coordinator, {}, async_add_entities)
unsub = coordinator.async_add_listener(update_friends)
2021-05-27 09:56:20 -04:00
hass.data[DOMAIN][entry.entry_id]["binary_sensor_unsub"] = unsub
2020-10-15 19:11:05 -04:00
update_friends()
class XboxBinarySensorEntity(XboxBaseSensorEntity, BinarySensorEntity):
"""Representation of a Xbox presence state."""
@property
def is_on(self) -> bool:
"""Return the status of the requested attribute."""
if not self.coordinator.last_update_success:
return False
return getattr(self.data, self.attribute, False)
@callback
def async_update_friends(
coordinator: XboxUpdateCoordinator,
2021-03-18 15:08:35 +01:00
current: dict[str, list[XboxBinarySensorEntity]],
2020-10-15 19:11:05 -04:00
async_add_entities,
) -> None:
"""Update friends."""
new_ids = set(coordinator.data.presence)
current_ids = set(current)
# Process new favorites, add them to Home Assistant
2022-07-09 23:09:15 +02:00
new_entities: list[XboxBinarySensorEntity] = []
2020-10-15 19:11:05 -04:00
for xuid in new_ids - current_ids:
current[xuid] = [
XboxBinarySensorEntity(coordinator, xuid, attribute)
for attribute in PRESENCE_ATTRIBUTES
]
new_entities = new_entities + current[xuid]
async_add_entities(new_entities)
2020-10-15 19:11:05 -04:00
# Process deleted favorites, remove them from Home Assistant
for xuid in current_ids - new_ids:
coordinator.hass.async_create_task(
async_remove_entities(xuid, coordinator, current)
)
async def async_remove_entities(
xuid: str,
coordinator: XboxUpdateCoordinator,
2022-07-09 23:09:15 +02:00
current: dict[str, list[XboxBinarySensorEntity]],
2020-10-15 19:11:05 -04:00
) -> None:
"""Remove friend sensors from Home Assistant."""
registry = er.async_get(coordinator.hass)
2020-10-15 19:11:05 -04:00
entities = current[xuid]
for entity in entities:
if entity.entity_id in registry.entities:
registry.async_remove(entity.entity_id)
del current[xuid]