From 06eed998b9edbec24f1265c5c99b09eff49a8ab7 Mon Sep 17 00:00:00 2001 From: dckiller51 <53062806+dckiller51@users.noreply.github.com> Date: Tue, 17 Mar 2026 13:40:42 +0100 Subject: [PATCH] Add platform attribute to Xbox sensors (#161661) --- homeassistant/components/xbox/sensor.py | 41 ++++++++++++++++++- homeassistant/components/xbox/strings.json | 1 + .../xbox/snapshots/test_sensor.ambr | 3 ++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/xbox/sensor.py b/homeassistant/components/xbox/sensor.py index 72028bbab216..e192f11c3bdb 100644 --- a/homeassistant/components/xbox/sensor.py +++ b/homeassistant/components/xbox/sensor.py @@ -44,6 +44,16 @@ MAP_JOIN_RESTRICTIONS = { "followed": "joinable", } +MAP_PLATFORM_NAME = { + "Android": "Android", + "iOS": "iOS", + "Nintendo": "Nintendo Switch", + "Scarlett": "Xbox Series X|S", + "WindowsOneCore": "Windows", + "Xbox360": "Xbox 360", + "XboxOne": "Xbox One", +} + class XboxSensor(StrEnum): """Xbox sensor.""" @@ -63,6 +73,9 @@ class XboxSensor(StrEnum): FREE_STORAGE = "free_storage" +PRESENCE_ACTIVE = "Active" + + @dataclass(kw_only=True, frozen=True) class XboxSensorEntityDescription(XboxBaseEntityDescription, SensorEntityDescription): """Xbox sensor description.""" @@ -79,7 +92,7 @@ class XboxStorageDeviceSensorEntityDescription( value_fn: Callable[[StorageDevice], StateType] -def now_playing_attributes(_: Person, title: Title | None) -> dict[str, Any]: +def now_playing_attributes(person: Person, title: Title | None) -> dict[str, Any]: """Attributes of the currently played title.""" attributes: dict[str, Any] = { "short_description": None, @@ -91,9 +104,35 @@ def now_playing_attributes(_: Person, title: Title | None) -> dict[str, Any]: "achievements": None, "gamerscore": None, "progress": None, + "platform": None, } + + if person.presence_details: + active_entry = next( + ( + d + for d in person.presence_details + if d.state == PRESENCE_ACTIVE and d.is_game + ), + None, + ) or next( + (d for d in person.presence_details if d.state == PRESENCE_ACTIVE), + None, + ) + + if active_entry: + platform = active_entry.device + if platform == "Scarlett" and title and title.devices: + if "Xbox360" in title.devices: + platform = "Xbox360" + elif "XboxOne" in title.devices: + platform = "XboxOne" + + attributes["platform"] = MAP_PLATFORM_NAME.get(platform, platform) + if not title: return attributes + if title.detail is not None: attributes.update( { diff --git a/homeassistant/components/xbox/strings.json b/homeassistant/components/xbox/strings.json index db783ed43986..c7a651353b38 100644 --- a/homeassistant/components/xbox/strings.json +++ b/homeassistant/components/xbox/strings.json @@ -141,6 +141,7 @@ }, "genres": { "name": "Genres" }, "min_age": { "name": "Minimum age" }, + "platform": { "name": "Platform" }, "progress": { "name": "Progress" }, "publisher": { "name": "Publisher" }, "release_date": { "name": "Release date" }, diff --git a/tests/components/xbox/snapshots/test_sensor.ambr b/tests/components/xbox/snapshots/test_sensor.ambr index 3f7d84238423..277250bd42e2 100644 --- a/tests/components/xbox/snapshots/test_sensor.ambr +++ b/tests/components/xbox/snapshots/test_sensor.ambr @@ -363,6 +363,7 @@ 'gamerscore': None, 'genres': None, 'min_age': None, + 'platform': None, 'progress': None, 'publisher': None, 'release_date': None, @@ -851,6 +852,7 @@ 'gamerscore': '10 / 1000', 'genres': 'Role Playing', 'min_age': 13, + 'platform': 'Xbox 360', 'progress': '1 %', 'publisher': 'Microsoft', 'release_date': datetime.date(2016, 12, 14), @@ -1338,6 +1340,7 @@ 'gamerscore': None, 'genres': None, 'min_age': None, + 'platform': None, 'progress': None, 'publisher': None, 'release_date': None,