JackJPowell / hass-psn

Home Assistant integration for the Playstation Network
https://www.home-assistant.io
MIT License
35 stars 4 forks source link

Improving PS5 Integration: Attribute List or Auto-Entity Creation #13

Open Levtos opened 2 months ago

Levtos commented 2 months ago

Is your feature request related to a problem? Please describe.

As a Home Assistant beginner, I’m always frustrated when I can’t fully utilize all the attributes of the PS5 integration. I can see the game covers and trophy information, but it’s tough for me to access and integrate all the extra details through the helper.

Describe the solution you'd like

I’d love to have a comprehensive list of all available attributes, maybe even grouped by which entity provides which info. Alternatively, it would be awesome if the integration could automatically create all entities in Home Assistant, so I don’t have to hunt for them.

Describe alternatives you've considered

I’ve considered looking through documentation and existing issues, but it’s quite overwhelming, especially as I’m just getting started. Manual integration of each attribute also seems tedious and time-consuming.

Additional context

I’m really excited about using this integration, as it’s something I’ve been looking for for years! If you need help translating the integration into German, feel free to reach out!

xXREDXIIIIXx commented 2 months ago

Below you can find the current Entity IDs and the attributes associated with them, I sympathise with you about being a new user and everything can be overwhelming, you should find the HA community to be very welcoming with answering questions or pointing you to relevant documentation

Manual integration of each attribute also seems tedious and time-consuming

Jack has done a lot of work to bring us this, you should be prepared to learn how to use it. there is a issue post from another user asking how to get the attributes #9, I have a post in there detailing how to do this.

media_player.ps5_console - Shows the title of the game you are playing or other information such as "Browsing the menu"

sensor.psn_status - Shows your online state - Online, Offline, Playing The below information is only available when a game is open on the console. left is the attribute name and right is the example output

sensor.playstation_trophy_level Shows Current trophy Level - 651

I hope this helps you out, but feel free to ask, I don't normally check here often but I should get notified of any replies.

Levtos commented 2 months ago

Hey xXREDXIIIIXx,

Thanks for your reply and the helpful information! I tried to implement what you suggested this morning because I saw the code in issue #9, but I'm running into some errors. I added the following code to my configuration.yaml:

# PSN
template:
  - sensor:
      - name: "PSN Trophäen Level"
        unique_id: "psn_trophy_level"
        icon: "mdi:trophy"
        state: "{{ state_attr('sensor.psn_data', 'trophy_summary').trophy_level }}"
        attributes:
          platinum: "{{ state_attr('sensor.psn_data', 'trophy_summary').earned_trophies['platinum'] }}"
          gold: "{{ state_attr('sensor.psn_data', 'trophy_summary').earned_trophies['gold'] }}"
          silver: "{{ state_attr('sensor.psn_data', 'trophy_summary').earned_trophies['silver'] }}"
          bronze: "{{ state_attr('sensor.psn_data', 'trophy_summary').earned_trophies['bronze'] }}"
          next_level_progress: "{{ state_attr('sensor.psn_data', 'trophy_summary').progress }}"

      - name: "PSN Status"
        unique_id: "psn_status"
        icon: "mdi:account-circle-outline"
        state: "{{ state_attr('sensor.psn_data', 'status') }}"
        attributes:
          name: "{{ state_attr('sensor.psn_data', 'status')['name'] }}"
          description: "{{ state_attr('sensor.psn_data', 'status')['description'] }}"
          platform: "{{ state_attr('sensor.psn_data', 'status')['platform'] }}"
          content_rating: "{{ state_attr('sensor.psn_data', 'status')['content_rating'] }}"
          play_count: "{{ state_attr('sensor.psn_data', 'status')['play_count'] }}"
          play_duration: "{{ state_attr('sensor.psn_data', 'status')['play_duration'] }}"
          platinum_trophies: "{{ state_attr('sensor.psn_data', 'status')['trophies']['platinum'] }}"
          gold_trophies: "{{ state_attr('sensor.psn_data', 'status')['trophies']['gold'] }}"
          silver_trophies: "{{ state_attr('sensor.psn_data', 'status')['trophies']['silver'] }}"
          bronze_trophies: "{{ state_attr('sensor.psn_data', 'status')['trophies']['bronze'] }}"
          earned_platinum_trophies: "{{ state_attr('sensor.psn_data', 'status')['earned_trophies']['platinum'] }}"
          earned_gold_trophies: "{{ state_attr('sensor.psn_data', 'status')['earned_trophies']['gold'] }}"
          earned_silver_trophies: "{{ state_attr('sensor.psn_data', 'status')['earned_trophies']['silver'] }}"
          earned_bronze_trophies: "{{ state_attr('sensor.psn_data', 'status')['earned_trophies']['bronze'] }}"
          trophy_progress: "{{ state_attr('sensor.psn_data', 'status')['trophy_progress'] }}"

Unfortunately, I'm encountering some errors. If you could provide any insights or suggestions, that would be awesome! Example Error:

`Logger: homeassistant.helpers.event
Quelle: helpers/template.py:621
Erstmals aufgetreten: 21:21:21 (12 Vorkommnisse)
Zuletzt protokolliert: 21:21:21

Error while processing template: Template<template=({{ state_attr('sensor.psn_data', 'status')['trophies']['bronze'] }}) renders=2>
Error while processing template: Template<template=({{ state_attr('sensor.psn_data', 'status')['earned_trophies']['platinum'] }}) renders=2>
Error while processing template: Template<template=({{ state_attr('sensor.psn_data', 'status')['earned_trophies']['gold'] }}) renders=2>
Error while processing template: Template<template=({{ state_attr('sensor.psn_data', 'status')['earned_trophies']['silver'] }}) renders=2>
Error while processing template: Template<template=({{ state_attr('sensor.psn_data', 'status')['earned_trophies']['bronze'] }}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 619, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2654, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.12/site-packages/jinja2/sandbox.py", line 304, in getitem
    return obj[argument]
           ~~~^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'None' has no attribute 'earned_trophies'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 731, in async_render_to_info
    render_info._result = self.async_render(  # noqa: SLF001
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 621, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'earned_trophies'`

Even when I manually add the helper under Settings -> Devices & Integrations -> Helpers, I still get the similiar error messages. Could it be possible that there's an issue with the translations since I'm logged in via de-DE?

Thanks again for your help!

Best, Levtos

xXREDXIIIIXx commented 2 months ago

Hey xXREDXIIIIXx,

Thanks for your reply and the helpful information! I tried to implement what you suggested this morning because I saw the code in issue #9, but I'm running into some errors. I added the following code to my configuration.yaml:

# PSN
template:
  - sensor:
      - name: "PSN Trophäen Level"
        unique_id: "psn_trophy_level"
        icon: "mdi:trophy"
        state: "{{ state_attr('sensor.psn_data', 'trophy_summary').trophy_level }}"
        attributes:
          platinum: "{{ state_attr('sensor.psn_data', 'trophy_summary').earned_trophies['platinum'] }}"
          gold: "{{ state_attr('sensor.psn_data', 'trophy_summary').earned_trophies['gold'] }}"
          silver: "{{ state_attr('sensor.psn_data', 'trophy_summary').earned_trophies['silver'] }}"
          bronze: "{{ state_attr('sensor.psn_data', 'trophy_summary').earned_trophies['bronze'] }}"
          next_level_progress: "{{ state_attr('sensor.psn_data', 'trophy_summary').progress }}"

      - name: "PSN Status"
        unique_id: "psn_status"
        icon: "mdi:account-circle-outline"
        state: "{{ state_attr('sensor.psn_data', 'status') }}"
        attributes:
          name: "{{ state_attr('sensor.psn_data', 'status')['name'] }}"
          description: "{{ state_attr('sensor.psn_data', 'status')['description'] }}"
          platform: "{{ state_attr('sensor.psn_data', 'status')['platform'] }}"
          content_rating: "{{ state_attr('sensor.psn_data', 'status')['content_rating'] }}"
          play_count: "{{ state_attr('sensor.psn_data', 'status')['play_count'] }}"
          play_duration: "{{ state_attr('sensor.psn_data', 'status')['play_duration'] }}"
          platinum_trophies: "{{ state_attr('sensor.psn_data', 'status')['trophies']['platinum'] }}"
          gold_trophies: "{{ state_attr('sensor.psn_data', 'status')['trophies']['gold'] }}"
          silver_trophies: "{{ state_attr('sensor.psn_data', 'status')['trophies']['silver'] }}"
          bronze_trophies: "{{ state_attr('sensor.psn_data', 'status')['trophies']['bronze'] }}"
          earned_platinum_trophies: "{{ state_attr('sensor.psn_data', 'status')['earned_trophies']['platinum'] }}"
          earned_gold_trophies: "{{ state_attr('sensor.psn_data', 'status')['earned_trophies']['gold'] }}"
          earned_silver_trophies: "{{ state_attr('sensor.psn_data', 'status')['earned_trophies']['silver'] }}"
          earned_bronze_trophies: "{{ state_attr('sensor.psn_data', 'status')['earned_trophies']['bronze'] }}"
          trophy_progress: "{{ state_attr('sensor.psn_data', 'status')['trophy_progress'] }}"

Unfortunately, I'm encountering some errors. If you could provide any insights or suggestions, that would be awesome! Example Error:

`Logger: homeassistant.helpers.event
Quelle: helpers/template.py:621
Erstmals aufgetreten: 21:21:21 (12 Vorkommnisse)
Zuletzt protokolliert: 21:21:21

Error while processing template: Template<template=({{ state_attr('sensor.psn_data', 'status')['trophies']['bronze'] }}) renders=2>
Error while processing template: Template<template=({{ state_attr('sensor.psn_data', 'status')['earned_trophies']['platinum'] }}) renders=2>
Error while processing template: Template<template=({{ state_attr('sensor.psn_data', 'status')['earned_trophies']['gold'] }}) renders=2>
Error while processing template: Template<template=({{ state_attr('sensor.psn_data', 'status')['earned_trophies']['silver'] }}) renders=2>
Error while processing template: Template<template=({{ state_attr('sensor.psn_data', 'status')['earned_trophies']['bronze'] }}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 619, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2654, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.12/site-packages/jinja2/sandbox.py", line 304, in getitem
    return obj[argument]
           ~~~^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'None' has no attribute 'earned_trophies'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 731, in async_render_to_info
    render_info._result = self.async_render(  # noqa: SLF001
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 621, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'earned_trophies'`

Even when I manually add the helper under Settings -> Devices & Integrations -> Helpers, I still get the similiar error messages. Could it be possible that there's an issue with the translations since I'm logged in via de-DE?

Thanks again for your help!

Best, Levtos

Not 100% Sure but I think the state you are trying to get is invalid:

state: "{{ state_attr('sensor.psn_data', 'trophy_summary').trophy_level }}"

Try the below as a test, these should just bring back the trophy level and your platinum count {{ states('sensor.playstation_trophy_level') }} {{ state_attr('sensor.playstation_trophy_level', 'platinum') }}

JackJPowell commented 3 weeks ago

@Levtos, I just pushed out a beta release of v0.6.0 that includes the option to expose all attributes as sensor entities. You can go into the HACS 2.0 integration to enable beta updates for the integration. Give it a shot and let me know how it goes.

Levtos commented 3 weeks ago

Hi @JackJPowell ,

Thank you for your kind words and for the beta. I’m still sorry about what happened and can promise you that yesterday's issue won’t happen again.

I've already started exploring the integration a bit and noticed a few minor things.

On my first attempt, I added the new entities to the existing setup via "Configure." I noticed that the old sensors follow the naming logic sensor.psn_entity, whereas the new ones are named sensor.psnname_entity. It’s not a major issue, just a small cosmetic detail ;)

For the second attempt, I removed my existing console from the integration and reconnected via NPSSO. Here, the same naming inconsistency appears. As I said, it’s not a big deal since entities can be renamed as desired, but personally, I prefer a naming convention like sensor_integration_entity or sensor.device_entity. Again, this is just a cosmetic detail, and I’m not sure if it’s intended.

On my third test, I reviewed the individual entities. To illustrate better, I created a custom dashboard and added all entities. The entity "earned Trophy" doesn’t seem to work properly. All four fields (Bronze, Silver, Gold, and Platinum) display the number 13. I’m not sure what this number means or where it comes from. In my first test, I selected Diablo IV, where the number 13 appeared, and I observed the same in Overwatch 2 and Sid Meier’s Civ VI. The number 13 keeps appearing. I’ll send you two screenshots to demonstrate this, one of the dashboard and one of the trophy overview from my account (apologies in advance that both images are in German).

Lastly, I noticed a fourth issue, though it’s not directly related to this one. I'll report a new issue for it within the next few hours.

I hope this information is helpful to you, and if there’s any way I can further assist, I’m happy to help.

Many greetings from Germany

Benjamin

levtos87_trophy_overview Screenshot 2024-11-09 094509

JackJPowell commented 3 weeks ago

On my first attempt, I added the new entities to the existing setup via "Configure." I noticed that the old sensors follow the naming logic sensor.psn_entity, whereas the new ones are named sensor.psnname_entity. It’s not a major issue, just a small cosmetic detail ;)

This is intentional and was done to support multiple psn accounts. Without including the psn name, people with multiple PSN accounts would generate the same entities for each causing a conflict and resulting in entities that end with _2.

For the second attempt, I removed my existing console from the integration and reconnected via NPSSO. Here, the same naming inconsistency appears. As I said, it’s not a big deal since entities can be renamed as desired, but personally, I prefer a naming convention like sensor_integration_entity or sensor.device_entity. Again, this is just a cosmetic detail, and I’m not sure if it’s intended.

Same as above. You held on to the old entity ID until you deleted and re-added everything. It's easy to set the entity ID as the developer, but the correct development method is to let HA derive the entity ID from the data provided. You can read about it and the has_entity_name attribute here.

On my third test, I reviewed the individual entities. To illustrate better, I created a custom dashboard and added all entities. The entity "earned Trophy" doesn’t seem to work properly. All four fields (Bronze, Silver, Gold, and Platinum) display the number 13. I’m not sure what this number means or where it comes from. In my first test, I selected Diablo IV, where the number 13 appeared, and I observed the same in Overwatch 2 and Sid Meier’s Civ VI. The number 13 keeps appearing. I’ll send you two screenshots to demonstrate this, one of the dashboard and one of the trophy overview from my account (apologies in advance that both images are in German).

Good find! I can't believe I missed this. Next update will fix this.

Lastly, I noticed a fourth issue, though it’s not directly related to this one. I'll report a new issue for it within the next few hours.

I think I know what's going on here. I'll check and respond to the issue

Levtos commented 3 weeks ago

Hellooo @JackJPowell

ah, got it! I hadn’t considered the possibility of having multiple PlayStation systems, maybe my mistake. -.-"

Regarding the entity names, I’ve already renamed them to fit my naming convention, so no worries. I just thought it might have been unintentional, but it totally makes sense when thinking about multiple systems. All good!

I did notice one other small "design" quirk: When the PlayStation is offline, multiple fields (the new ones) show the status as "Unbekannt" (Unknown in English). It might actually be smarter to leave these fields blank (“ ”), as displaying “Unknown” could be confusing.

As for troubleshooting: I’ve been working in eCommerce for a German DIY chain for over 10 years, and I handle QA testing, so I have a pretty good knack for spotting issues and thinking outside the box to recreate them. (Happy to help as a beta tester if you ever need one!) Screenshot 2024-11-09 160733

Levtos commented 3 weeks ago

Hello @JackJPowell

Maybe I closed the issue too quickly. :( Today, I received 546 warnings from the sensor sensor_username_description. It seems to have issues with game descriptions longer than 255 characters.

Logger: homeassistant.helpers.entity
Source: helpers/entity.py:1211
First occurred: November 11, 2024, at 23:17:40 (546 occurrences)
Last logged: 21:32:29

Failed to set state for sensor.ps5_description, falling back to unknown
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1211, in _async_write_ha_state
hass.states.async_set_internal(
File "/usr/src/homeassistant/homeassistant/core.py", line 2373, in async_set_internal
state = State(
^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 1817, in init
validate_state(state)
File "/usr/src/homeassistant/homeassistant/core.py", line 246, in validate_state
raise InvalidStateError(
homeassistant.exceptions.InvalidStateError: Invalid state with length 2553. State max length is 255 characters.
JackJPowell commented 2 weeks ago

Thanks! I've got it fixed with some bonus stuff too. I'll have a new beta up shortly.

JackJPowell commented 2 weeks ago

Hey @Levtos, just pushed out beta 4. I hope this is the one to release

Levtos commented 2 weeks ago

Hey @JackJPowell , sorry for the late reply! Had to check what’s been changed first. The new beta looks pretty cool overall, but I noticed a few things:

Text Fields The "Description" and "About Me" fields are now empty. In yesterdays Beta (I think it was Beta 3), at least the "Description" still had some content. Looks like something went missing in this version.

Avatar The avatar shows up for me, but it’s kind of oversized. Maybe this is a Mushroom issue, or I messed up something in the settings ^^".

Unknown Status When the PlayStation is turned off, the new sensors still show "unknown." I fixed it with an is_state workaround in Mushroom, but it might still be worth a look to avoid needing such custom solutions.

JackJPowell commented 2 weeks ago

Thanks @Levtos. I'm a bit surprised about the lack of "About me" text. It was showing up for me. The image I'm pulling is large so you may have to explicitly scale it to make it fit in the card. When I asked on the home assistant discord, the reply I got did say to set the value to None. I'm still not sure it's 100% right but I don't think I should set them to an empty string. I'm curious what value they would need to be for you to not need to handle it manually.

Beta 5 is out and should at least address the text field issues. Looks good on my end.

Screenshot 2024-11-18 at 7 58 54 PM

Levtos commented 1 week ago

Hi @JackJPowell,

sorry again for my delayed response. I haven’t had any time to play on my PS5 lately. Somehow, I just end up spending my free time at the PC, tweaking my Home Assistant setup, or playing a quick match of Overwatch. Haha, but anyway, I just turned on my PlayStation, and things are looking good. Upon revisiting the "About Me" section, I noticed I hadn’t filled it out at all. shame on me

I’ve reviewed all the entities, and everything seems fine. The system log isn’t throwing any errors either, which is already a good sign. So, from my side, I just want to give you a huge thank you and hope you’ll release the version soon. Thanks again!

Greetings from Germany, Benjamin