diff --git a/homeassistant/components/playstation_network/helpers.py b/homeassistant/components/playstation_network/helpers.py index 267dc77ff06..9c7dac29a81 100644 --- a/homeassistant/components/playstation_network/helpers.py +++ b/homeassistant/components/playstation_network/helpers.py @@ -38,7 +38,7 @@ class PlaystationNetworkData: presence: dict[str, Any] = field(default_factory=dict) username: str = "" account_id: str = "" - available: bool = False + availability: str = "unavailable" active_sessions: dict[PlatformType, SessionData] = field(default_factory=dict) registered_platforms: set[PlatformType] = field(default_factory=set) trophy_summary: TrophySummary | None = None @@ -92,10 +92,7 @@ class PlaystationNetwork: data.username = self.user.online_id data.account_id = self.user.account_id - data.available = ( - data.presence.get("basicPresence", {}).get("availability") - == "availableToPlay" - ) + data.availability = data.presence["basicPresence"]["availability"] session = SessionData() session.platform = PlatformType( @@ -127,8 +124,6 @@ class PlaystationNetwork: if (game_title_info := presence[0] if presence else {}) and game_title_info[ "onlineStatus" ] == "online": - data.available = True - platform = PlatformType(game_title_info["platform"]) if platform is PlatformType.PS4: diff --git a/homeassistant/components/playstation_network/icons.json b/homeassistant/components/playstation_network/icons.json index 7817a4c8b07..612427c9a1d 100644 --- a/homeassistant/components/playstation_network/icons.json +++ b/homeassistant/components/playstation_network/icons.json @@ -29,6 +29,14 @@ }, "last_online": { "default": "mdi:account-clock" + }, + "online_status": { + "default": "mdi:account-badge", + "state": { + "busy": "mdi:account-cancel", + "availabletocommunicate": "mdi:cellphone", + "offline": "mdi:account-off-outline" + } } } } diff --git a/homeassistant/components/playstation_network/media_player.py b/homeassistant/components/playstation_network/media_player.py index c1320e9b280..3e55e565460 100644 --- a/homeassistant/components/playstation_network/media_player.py +++ b/homeassistant/components/playstation_network/media_player.py @@ -107,7 +107,7 @@ class PsnMediaPlayerEntity( """Media Player state getter.""" session = self.coordinator.data.active_sessions.get(self.key) if session and session.status == "online": - if self.coordinator.data.available and session.title_id is not None: + if session.title_id is not None: return MediaPlayerState.PLAYING return MediaPlayerState.ON return MediaPlayerState.OFF diff --git a/homeassistant/components/playstation_network/sensor.py b/homeassistant/components/playstation_network/sensor.py index ece2952c0f0..305f252f31d 100644 --- a/homeassistant/components/playstation_network/sensor.py +++ b/homeassistant/components/playstation_network/sensor.py @@ -51,6 +51,7 @@ class PlaystationNetworkSensor(StrEnum): EARNED_TROPHIES_BRONZE = "earned_trophies_bronze" ONLINE_ID = "online_id" LAST_ONLINE = "last_online" + ONLINE_STATUS = "online_status" SENSOR_DESCRIPTIONS: tuple[PlaystationNetworkSensorEntityDescription, ...] = ( @@ -121,6 +122,13 @@ SENSOR_DESCRIPTIONS: tuple[PlaystationNetworkSensorEntityDescription, ...] = ( available_fn=lambda psn: "lastAvailableDate" in psn.presence["basicPresence"], device_class=SensorDeviceClass.TIMESTAMP, ), + PlaystationNetworkSensorEntityDescription( + key=PlaystationNetworkSensor.ONLINE_STATUS, + translation_key=PlaystationNetworkSensor.ONLINE_STATUS, + value_fn=lambda psn: psn.availability.lower().replace("unavailable", "offline"), + device_class=SensorDeviceClass.ENUM, + options=["offline", "availabletoplay", "availabletocommunicate", "busy"], + ), ) diff --git a/homeassistant/components/playstation_network/strings.json b/homeassistant/components/playstation_network/strings.json index d3a9c986e88..f68d69417fb 100644 --- a/homeassistant/components/playstation_network/strings.json +++ b/homeassistant/components/playstation_network/strings.json @@ -81,6 +81,15 @@ }, "last_online": { "name": "Last online" + }, + "online_status": { + "name": "Online status", + "state": { + "offline": "Offline", + "availabletoplay": "Online", + "availabletocommunicate": "Online on PS App", + "busy": "Away" + } } } } diff --git a/tests/components/playstation_network/snapshots/test_diagnostics.ambr b/tests/components/playstation_network/snapshots/test_diagnostics.ambr index 6073b37863e..f320eea4b7c 100644 --- a/tests/components/playstation_network/snapshots/test_diagnostics.ambr +++ b/tests/components/playstation_network/snapshots/test_diagnostics.ambr @@ -13,7 +13,7 @@ 'title_name': 'STAR WARS Jedi: Survivorâ„¢', }), }), - 'available': True, + 'availability': 'availableToPlay', 'presence': dict({ 'basicPresence': dict({ 'availability': 'availableToPlay', diff --git a/tests/components/playstation_network/snapshots/test_sensor.ambr b/tests/components/playstation_network/snapshots/test_sensor.ambr index 59cd979ed76..a00e3c4ff0a 100644 --- a/tests/components/playstation_network/snapshots/test_sensor.ambr +++ b/tests/components/playstation_network/snapshots/test_sensor.ambr @@ -244,6 +244,68 @@ 'state': 'testuser', }) # --- +# name: test_sensors[sensor.testuser_online_status-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'offline', + 'availabletoplay', + 'availabletocommunicate', + 'busy', + ]), + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.testuser_online_status', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Online status', + 'platform': 'playstation_network', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': , + 'unique_id': 'my-psn-id_online_status', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[sensor.testuser_online_status-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'enum', + 'friendly_name': 'testuser Online status', + 'options': list([ + 'offline', + 'availabletoplay', + 'availabletocommunicate', + 'busy', + ]), + }), + 'context': , + 'entity_id': 'sensor.testuser_online_status', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'availabletoplay', + }) +# --- # name: test_sensors[sensor.testuser_platinum_trophies-entry] EntityRegistryEntrySnapshot({ 'aliases': set({