Skip to content

Commit 6fa73f7

Browse files
authored
Deprecate entities in Xbox integration (home-assistant#154891)
1 parent 3c46b40 commit 6fa73f7

File tree

9 files changed

+140
-632
lines changed

9 files changed

+140
-632
lines changed

homeassistant/components/xbox/binary_sensor.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
from yarl import URL
1212

1313
from homeassistant.components.binary_sensor import (
14+
DOMAIN as BINARY_SENSOR_DOMAIN,
1415
BinarySensorEntity,
1516
BinarySensorEntityDescription,
1617
)
1718
from homeassistant.core import HomeAssistant, callback
1819
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
1920

2021
from .coordinator import XboxConfigEntry, XboxUpdateCoordinator
21-
from .entity import XboxBaseEntity
22+
from .entity import XboxBaseEntity, check_deprecated_entity
2223

2324

2425
class XboxBinarySensor(StrEnum):
@@ -37,6 +38,7 @@ class XboxBinarySensorEntityDescription(BinarySensorEntityDescription):
3738

3839
is_on_fn: Callable[[Person], bool | None]
3940
entity_picture_fn: Callable[[Person], str | None] | None = None
41+
deprecated: bool | None = None
4042

4143

4244
def profile_pic(person: Person) -> str | None:
@@ -82,13 +84,8 @@ def in_game(person: Person) -> bool:
8284
),
8385
XboxBinarySensorEntityDescription(
8486
key=XboxBinarySensor.IN_PARTY,
85-
translation_key=XboxBinarySensor.IN_PARTY,
86-
is_on_fn=(
87-
lambda x: bool(x.multiplayer_summary.in_party)
88-
if x.multiplayer_summary
89-
else None
90-
),
91-
entity_registry_enabled_default=False,
87+
is_on_fn=lambda _: None,
88+
deprecated=True,
9289
),
9390
XboxBinarySensorEntityDescription(
9491
key=XboxBinarySensor.IN_GAME,
@@ -97,13 +94,8 @@ def in_game(person: Person) -> bool:
9794
),
9895
XboxBinarySensorEntityDescription(
9996
key=XboxBinarySensor.IN_MULTIPLAYER,
100-
translation_key=XboxBinarySensor.IN_MULTIPLAYER,
101-
is_on_fn=(
102-
lambda x: bool(x.multiplayer_summary.in_multiplayer_session)
103-
if x.multiplayer_summary
104-
else None
105-
),
106-
entity_registry_enabled_default=False,
97+
is_on_fn=lambda _: None,
98+
deprecated=True,
10799
),
108100
XboxBinarySensorEntityDescription(
109101
key=XboxBinarySensor.HAS_GAME_PASS,
@@ -121,7 +113,9 @@ async def async_setup_entry(
121113
"""Set up Xbox Live friends."""
122114
coordinator = entry.runtime_data
123115

124-
update_friends = partial(async_update_friends, coordinator, {}, async_add_entities)
116+
update_friends = partial(
117+
async_update_friends, hass, coordinator, {}, async_add_entities
118+
)
125119

126120
entry.async_on_unload(coordinator.async_add_listener(update_friends))
127121

@@ -152,6 +146,7 @@ def entity_picture(self) -> str | None:
152146

153147
@callback
154148
def async_update_friends(
149+
hass: HomeAssistant,
155150
coordinator: XboxUpdateCoordinator,
156151
current: dict[str, list[XboxBinarySensorEntity]],
157152
async_add_entities,
@@ -163,10 +158,11 @@ def async_update_friends(
163158
# Process new favorites, add them to Home Assistant
164159
new_entities: list[XboxBinarySensorEntity] = []
165160
for xuid in new_ids - current_ids:
166-
current[xuid] = [
167-
XboxBinarySensorEntity(coordinator, xuid, description)
168-
for description in SENSOR_DESCRIPTIONS
169-
]
161+
current[xuid] = []
162+
for description in SENSOR_DESCRIPTIONS:
163+
entity = XboxBinarySensorEntity(coordinator, xuid, description)
164+
if check_deprecated_entity(hass, entity, BINARY_SENSOR_DOMAIN):
165+
current[xuid].append(entity)
170166
new_entities = new_entities + current[xuid]
171167
if new_entities:
172168
async_add_entities(new_entities)

homeassistant/components/xbox/entity.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
from xbox.webapi.api.provider.smartglass.models import ConsoleType, SmartglassConsole
66

7+
from homeassistant.components.automation import automations_with_entity
8+
from homeassistant.components.script import scripts_with_entity
9+
from homeassistant.core import HomeAssistant
10+
from homeassistant.helpers import entity_registry as er
711
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
812
from homeassistant.helpers.entity import EntityDescription
913
from homeassistant.helpers.update_coordinator import CoordinatorEntity
@@ -83,3 +87,29 @@ def __init__(
8387
def data(self) -> ConsoleData:
8488
"""Return coordinator data for this console."""
8589
return self.coordinator.data.consoles[self._console.id]
90+
91+
92+
def entity_used_in(hass: HomeAssistant, entity_id: str) -> list[str]:
93+
"""Get list of related automations and scripts."""
94+
used_in = automations_with_entity(hass, entity_id)
95+
used_in += scripts_with_entity(hass, entity_id)
96+
return used_in
97+
98+
99+
def check_deprecated_entity(
100+
hass: HomeAssistant,
101+
entity: XboxBaseEntity,
102+
entity_domain: str,
103+
) -> bool:
104+
"""Check for deprecated entity and remove it."""
105+
if not getattr(entity.entity_description, "deprecated", False):
106+
return True
107+
ent_reg = er.async_get(hass)
108+
if entity_id := ent_reg.async_get_entity_id(
109+
entity_domain,
110+
DOMAIN,
111+
f"{entity.xuid}_{entity.entity_description.key}",
112+
):
113+
ent_reg.async_remove(entity_id)
114+
115+
return False

homeassistant/components/xbox/icons.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
"gamer_score": {
88
"default": "mdi:alpha-g-circle"
99
},
10-
"account_tier": {
11-
"default": "mdi:microsoft-xbox"
12-
},
13-
"gold_tenure": {
14-
"default": "mdi:microsoft-xbox"
15-
},
1610
"last_online": {
1711
"default": "mdi:account-clock"
1812
},
@@ -27,15 +21,9 @@
2721
"online": {
2822
"default": "mdi:account"
2923
},
30-
"in_party": {
31-
"default": "mdi:account-group"
32-
},
3324
"in_game": {
3425
"default": "mdi:microsoft-xbox-controller"
3526
},
36-
"in_multiplayer": {
37-
"default": "mdi:account-multiple"
38-
},
3927
"has_game_pass": {
4028
"default": "mdi:microsoft-xbox"
4129
}

homeassistant/components/xbox/sensor.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from xbox.webapi.api.provider.people.models import Person
1212

1313
from homeassistant.components.sensor import (
14+
DOMAIN as SENSOR_DOMAIN,
1415
SensorDeviceClass,
1516
SensorEntity,
1617
SensorEntityDescription,
@@ -20,7 +21,7 @@
2021
from homeassistant.helpers.typing import StateType
2122

2223
from .coordinator import XboxConfigEntry, XboxUpdateCoordinator
23-
from .entity import XboxBaseEntity
24+
from .entity import XboxBaseEntity, check_deprecated_entity
2425

2526

2627
class XboxSensor(StrEnum):
@@ -40,6 +41,7 @@ class XboxSensorEntityDescription(SensorEntityDescription):
4041
"""Xbox sensor description."""
4142

4243
value_fn: Callable[[Person], StateType | datetime]
44+
deprecated: bool | None = None
4345

4446

4547
SENSOR_DESCRIPTIONS: tuple[XboxSensorEntityDescription, ...] = (
@@ -55,15 +57,13 @@ class XboxSensorEntityDescription(SensorEntityDescription):
5557
),
5658
XboxSensorEntityDescription(
5759
key=XboxSensor.ACCOUNT_TIER,
58-
translation_key=XboxSensor.ACCOUNT_TIER,
59-
entity_registry_enabled_default=False,
60-
value_fn=lambda x: x.detail.account_tier if x.detail else None,
60+
value_fn=lambda _: None,
61+
deprecated=True,
6162
),
6263
XboxSensorEntityDescription(
6364
key=XboxSensor.GOLD_TENURE,
64-
translation_key=XboxSensor.GOLD_TENURE,
65-
entity_registry_enabled_default=False,
66-
value_fn=lambda x: x.detail.tenure if x.detail else None,
65+
value_fn=lambda _: None,
66+
deprecated=True,
6767
),
6868
XboxSensorEntityDescription(
6969
key=XboxSensor.LAST_ONLINE,
@@ -96,7 +96,9 @@ async def async_setup_entry(
9696
"""Set up Xbox Live friends."""
9797
coordinator = config_entry.runtime_data
9898

99-
update_friends = partial(async_update_friends, coordinator, {}, async_add_entities)
99+
update_friends = partial(
100+
async_update_friends, hass, coordinator, {}, async_add_entities
101+
)
100102

101103
config_entry.async_on_unload(coordinator.async_add_listener(update_friends))
102104
update_friends()
@@ -115,6 +117,7 @@ def native_value(self) -> StateType | datetime:
115117

116118
@callback
117119
def async_update_friends(
120+
hass: HomeAssistant,
118121
coordinator: XboxUpdateCoordinator,
119122
current: dict[str, list[XboxSensorEntity]],
120123
async_add_entities,
@@ -126,10 +129,11 @@ def async_update_friends(
126129
# Process new favorites, add them to Home Assistant
127130
new_entities: list[XboxSensorEntity] = []
128131
for xuid in new_ids - current_ids:
129-
current[xuid] = [
130-
XboxSensorEntity(coordinator, xuid, description)
131-
for description in SENSOR_DESCRIPTIONS
132-
]
132+
current[xuid] = []
133+
for description in SENSOR_DESCRIPTIONS:
134+
entity = XboxSensorEntity(coordinator, xuid, description)
135+
if check_deprecated_entity(hass, entity, SENSOR_DOMAIN):
136+
current[xuid].append(entity)
133137
new_entities = new_entities + current[xuid]
134138
if new_entities:
135139
async_add_entities(new_entities)

homeassistant/components/xbox/strings.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@
3737
"name": "Gamerscore",
3838
"unit_of_measurement": "points"
3939
},
40-
"account_tier": {
41-
"name": "Account tier"
42-
},
43-
"gold_tenure": {
44-
"name": "Gold tenure"
45-
},
4640
"last_online": {
4741
"name": "Last online"
4842
},
@@ -56,15 +50,9 @@
5650
}
5751
},
5852
"binary_sensor": {
59-
"in_party": {
60-
"name": "In party"
61-
},
6253
"in_game": {
6354
"name": "In game"
6455
},
65-
"in_multiplayer": {
66-
"name": "In multiplayer"
67-
},
6856
"has_game_pass": {
6957
"name": "Subscribed to Xbox Game Pass"
7058
}

0 commit comments

Comments
 (0)