Skip to content

Commit bab9ec9

Browse files
authored
Add sensor for online status to PlayStation Network (home-assistant#147842)
1 parent 1051f85 commit bab9ec9

File tree

7 files changed

+91
-9
lines changed

7 files changed

+91
-9
lines changed

homeassistant/components/playstation_network/helpers.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class PlaystationNetworkData:
3838
presence: dict[str, Any] = field(default_factory=dict)
3939
username: str = ""
4040
account_id: str = ""
41-
available: bool = False
41+
availability: str = "unavailable"
4242
active_sessions: dict[PlatformType, SessionData] = field(default_factory=dict)
4343
registered_platforms: set[PlatformType] = field(default_factory=set)
4444
trophy_summary: TrophySummary | None = None
@@ -92,10 +92,7 @@ async def get_data(self) -> PlaystationNetworkData:
9292
data.username = self.user.online_id
9393
data.account_id = self.user.account_id
9494

95-
data.available = (
96-
data.presence.get("basicPresence", {}).get("availability")
97-
== "availableToPlay"
98-
)
95+
data.availability = data.presence["basicPresence"]["availability"]
9996

10097
session = SessionData()
10198
session.platform = PlatformType(
@@ -127,8 +124,6 @@ async def get_data(self) -> PlaystationNetworkData:
127124
if (game_title_info := presence[0] if presence else {}) and game_title_info[
128125
"onlineStatus"
129126
] == "online":
130-
data.available = True
131-
132127
platform = PlatformType(game_title_info["platform"])
133128

134129
if platform is PlatformType.PS4:

homeassistant/components/playstation_network/icons.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
},
3030
"last_online": {
3131
"default": "mdi:account-clock"
32+
},
33+
"online_status": {
34+
"default": "mdi:account-badge",
35+
"state": {
36+
"busy": "mdi:account-cancel",
37+
"availabletocommunicate": "mdi:cellphone",
38+
"offline": "mdi:account-off-outline"
39+
}
3240
}
3341
}
3442
}

homeassistant/components/playstation_network/media_player.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def state(self) -> MediaPlayerState:
107107
"""Media Player state getter."""
108108
session = self.coordinator.data.active_sessions.get(self.key)
109109
if session and session.status == "online":
110-
if self.coordinator.data.available and session.title_id is not None:
110+
if session.title_id is not None:
111111
return MediaPlayerState.PLAYING
112112
return MediaPlayerState.ON
113113
return MediaPlayerState.OFF

homeassistant/components/playstation_network/sensor.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class PlaystationNetworkSensor(StrEnum):
5151
EARNED_TROPHIES_BRONZE = "earned_trophies_bronze"
5252
ONLINE_ID = "online_id"
5353
LAST_ONLINE = "last_online"
54+
ONLINE_STATUS = "online_status"
5455

5556

5657
SENSOR_DESCRIPTIONS: tuple[PlaystationNetworkSensorEntityDescription, ...] = (
@@ -121,6 +122,13 @@ class PlaystationNetworkSensor(StrEnum):
121122
available_fn=lambda psn: "lastAvailableDate" in psn.presence["basicPresence"],
122123
device_class=SensorDeviceClass.TIMESTAMP,
123124
),
125+
PlaystationNetworkSensorEntityDescription(
126+
key=PlaystationNetworkSensor.ONLINE_STATUS,
127+
translation_key=PlaystationNetworkSensor.ONLINE_STATUS,
128+
value_fn=lambda psn: psn.availability.lower().replace("unavailable", "offline"),
129+
device_class=SensorDeviceClass.ENUM,
130+
options=["offline", "availabletoplay", "availabletocommunicate", "busy"],
131+
),
124132
)
125133

126134

homeassistant/components/playstation_network/strings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@
8181
},
8282
"last_online": {
8383
"name": "Last online"
84+
},
85+
"online_status": {
86+
"name": "Online status",
87+
"state": {
88+
"offline": "Offline",
89+
"availabletoplay": "Online",
90+
"availabletocommunicate": "Online on PS App",
91+
"busy": "Away"
92+
}
8493
}
8594
}
8695
}

tests/components/playstation_network/snapshots/test_diagnostics.ambr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
'title_name': 'STAR WARS Jedi: Survivor™',
1414
}),
1515
}),
16-
'available': True,
16+
'availability': 'availableToPlay',
1717
'presence': dict({
1818
'basicPresence': dict({
1919
'availability': 'availableToPlay',

tests/components/playstation_network/snapshots/test_sensor.ambr

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,68 @@
244244
'state': 'testuser',
245245
})
246246
# ---
247+
# name: test_sensors[sensor.testuser_online_status-entry]
248+
EntityRegistryEntrySnapshot({
249+
'aliases': set({
250+
}),
251+
'area_id': None,
252+
'capabilities': dict({
253+
'options': list([
254+
'offline',
255+
'availabletoplay',
256+
'availabletocommunicate',
257+
'busy',
258+
]),
259+
}),
260+
'config_entry_id': <ANY>,
261+
'config_subentry_id': <ANY>,
262+
'device_class': None,
263+
'device_id': <ANY>,
264+
'disabled_by': None,
265+
'domain': 'sensor',
266+
'entity_category': None,
267+
'entity_id': 'sensor.testuser_online_status',
268+
'has_entity_name': True,
269+
'hidden_by': None,
270+
'icon': None,
271+
'id': <ANY>,
272+
'labels': set({
273+
}),
274+
'name': None,
275+
'options': dict({
276+
}),
277+
'original_device_class': <SensorDeviceClass.ENUM: 'enum'>,
278+
'original_icon': None,
279+
'original_name': 'Online status',
280+
'platform': 'playstation_network',
281+
'previous_unique_id': None,
282+
'suggested_object_id': None,
283+
'supported_features': 0,
284+
'translation_key': <PlaystationNetworkSensor.ONLINE_STATUS: 'online_status'>,
285+
'unique_id': 'my-psn-id_online_status',
286+
'unit_of_measurement': None,
287+
})
288+
# ---
289+
# name: test_sensors[sensor.testuser_online_status-state]
290+
StateSnapshot({
291+
'attributes': ReadOnlyDict({
292+
'device_class': 'enum',
293+
'friendly_name': 'testuser Online status',
294+
'options': list([
295+
'offline',
296+
'availabletoplay',
297+
'availabletocommunicate',
298+
'busy',
299+
]),
300+
}),
301+
'context': <ANY>,
302+
'entity_id': 'sensor.testuser_online_status',
303+
'last_changed': <ANY>,
304+
'last_reported': <ANY>,
305+
'last_updated': <ANY>,
306+
'state': 'availabletoplay',
307+
})
308+
# ---
247309
# name: test_sensors[sensor.testuser_platinum_trophies-entry]
248310
EntityRegistryEntrySnapshot({
249311
'aliases': set({

0 commit comments

Comments
 (0)