Skip to content

Commit

Permalink
Add profile information
Browse files Browse the repository at this point in the history
  • Loading branch information
JackJPowell committed Nov 15, 2024
1 parent 18f9c15 commit f516159
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
10 changes: 7 additions & 3 deletions custom_components/playstation_network/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, hass: HomeAssistant, api, user, client) -> None:
"presence": {},
"available": False,
"online_status": False,
"profile": {},
"platform": {},
"title_metadata": {},
"friends": [],
Expand All @@ -56,8 +57,11 @@ async def _async_update_data(self) -> dict[str, Any]:
"""Get the latest data from the PSN."""
try:
self.data["username"] = self.user.online_id
self.data["profile"] = await self.hass.async_add_executor_job(
self.user.profile
)
self.data["presence"] = await self.hass.async_add_executor_job(
lambda: self.user.get_presence()
self.user.get_presence
)
self.data["available"] = (
self.data["presence"].get("basicPresence").get("availability")
Expand All @@ -77,7 +81,7 @@ async def _async_update_data(self) -> dict[str, Any]:

# self.data["friends"] = await self.client.available_to_play()
self.data["trophy_summary"] = await self.hass.async_add_executor_job(
lambda: self.client.trophy_summary()
self.client.trophy_summary
)

self.data["country"] = self.hass.config.country
Expand All @@ -100,7 +104,7 @@ async def _async_update_data(self) -> dict[str, Any]:
## If we receive an error, fall back to english
if self.data["title_details"][0].get("errorCode") is not None:
self.data["title_details"] = await self.hass.async_add_executor_job(
lambda: title.get_details()
title.get_details
)

trophy_titles = await self.hass.async_add_executor_job(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/playstation_network/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"integration_type": "device",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/JackJPowell/hass-psn/issues",
"requirements": ["PSNAWP-HA==2.2.0"],
"requirements": ["PSNAWP-HA==2.2.1"],
"ssdp": [],
"version": "0.7.0"
}
45 changes: 45 additions & 0 deletions custom_components/playstation_network/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def get_status(coordinator_data: any) -> str:
return "Offline"


def get_avatar(data: any) -> str:
"""Return Avatar URL"""
for avatar in data.get("profile").get("avatars"):
if avatar.get("size") == "l":
return avatar.get("url")


def get_status_attr(coordinator_data: any) -> dict[str, str]:
"""Parses status attributes"""
attrs: dict[str, str] = {
Expand All @@ -56,6 +63,9 @@ def get_status_attr(coordinator_data: any) -> dict[str, str]:
"play_count": None,
"play_duration": None,
"star_rating": None,
"about_me": None,
"avatar": None,
"playstation_plus": None,
"trophies": {
"platinum": None,
"gold": None,
Expand Down Expand Up @@ -93,6 +103,11 @@ def get_status_attr(coordinator_data: any) -> dict[str, str]:
attrs["earned_trophies"] = title_trophies.earned_trophies
attrs["trophy_progress"] = title_trophies.progress

attrs["about_me"] = coordinator_data.get("profile").get("aboutMe")
attrs["playstation_plus"] = coordinator_data.get("profile").get("isPlus")

attrs["avatar"] = get_avatar(coordinator_data)

for t in coordinator_data["recent_titles"]:
if t.title_id == coordinator_data.get("title_metadata").get("npTitleId"):
title_stats = t
Expand Down Expand Up @@ -172,6 +187,36 @@ def get_trophy_attr(coordinator_data: any) -> dict[str, str]:
unique_id="psn_title_name_attr",
value_fn=lambda data: data.get("name"),
),
PsnSensorEntityDescription(
key="about_me",
native_unit_of_measurement=None,
name="About Me",
icon="mdi:information-outline",
entity_registry_enabled_default=True,
has_entity_name=True,
unique_id="psn_about_me_attr",
value_fn=lambda data: data.get("profile").get("aboutMe"),
),
PsnSensorEntityDescription(
key="has_playstation_plus",
native_unit_of_measurement=None,
name="Playstation Plus",
icon="mdi:gamepad-outline",
entity_registry_enabled_default=True,
has_entity_name=True,
unique_id="psn_playstation_plus_attr",
value_fn=lambda data: data.get("profile").get("isPlus"),
),
PsnSensorEntityDescription(
key="avatar",
native_unit_of_measurement=None,
name="Avatar",
icon="mdi:face-man-profile",
entity_registry_enabled_default=True,
has_entity_name=True,
unique_id="psn_avatar_attr",
value_fn=get_avatar,
),
PsnSensorEntityDescription(
key="platform",
native_unit_of_measurement=None,
Expand Down

0 comments on commit f516159

Please sign in to comment.