Skip to content

Add discovery_topic option, descriptive error when stdout empty #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 27, 2024

Conversation

RobertD502
Copy link
Contributor

@RobertD502 RobertD502 commented Jun 13, 2024

Add Discovery Topic

With the discovery topic hardcoded to homeassistant, those that don't use the default homeassistant prefix for the discovery topic are currently unable to utilize MQTT auto-discovery. This PR introduces a new discovery_topic key in the mqtt config (and DISCOVERY_TOPIC env variable for regular container users) which can be used to define the custom discovery topic that a user's HA is set to use.

Note: If a user doesn't define a discovery_topic, ps5-mqtt will by default use homeassistant. So, users updating will not have to worry about this being a breaking change and needing to define the discovery_topic - that will remain reserved for those with a custom discovery topic.

Examples

The examples below were carried out using a build containing the code changes seen in this PR

1) If the discovery_topic is not set, ps5-mqtt will publish to the default homeassistant discovery topic

ps5_default_discovery_topic

2) If the discovery_topic is set by the user (it was set to RANDOM_DISCOVERY_TOPIC in this example), ps5-mqtt will publish to this discovery topic instead of homeassistant

ps5_custom_discovery_topic

Throw descriptive error if device state stdout is empty

Based on my testing, the JSON parsing error occurs when the Playstation is unreachable (resulting in stdout being empty) -
this will happen once when sending the Playstation into rest mode and continue to happen if the user completely turned off their Playstation / it is no longer reachable on their network. In order to provide some insight, a descriptive error message has been added to let users know what is going on.

1) If the user sends their Playstation into rest mode, the system will fail to get any data from the Playstation once. This results in the error getting logged, but the message reassures them that it only matters if the error continues to flood the log. See below of an example where the PS5 was sent into rest mode, the error was logged (due to stdout being temporarily empty), and then info started flowing again.

2024-06-14T01:02:51.311Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED/set/power
2024-06-14T01:02:56.320Z @ha:ps5:turnOffDevice 
2024-06-14T01:02:56.322Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED
2024-06-14T01:03:06.314Z @ha:ps5:checkDevicesState Resume polling
2024-06-14T01:03:06.848Z @ha:ps5:checkDevicesState {"address":{"address":"REDACTED","family":"IPv4","port":9302,"size":170},"hostRequestPort":997,"extras":{"statusLine":"620 Server Standby","statusCode":"620","statusMessage":"Server","status":"STANDBY"},"discoveryVersion":"00030010","systemVersion":"09400008","id":"REDACTED","name":"PS5-REDACTED","status":"STANDBY","type":"PS5"}

2024-06-14T01:03:12.424Z @ha:ps5:checkDevicesState {"address":{"address":"REDACTED","family":"IPv4","port":9302,"size":170},"hostRequestPort":997,"extras":{"statusLine":"620 Server Standby","statusCode":"620","statusMessage":"Server","status":"STANDBY"},"discoveryVersion":"00030010","systemVersion":"09400008","id":"REDACTED","name":"PS5-REDACTED","status":"STANDBY","type":"PS5"}

2024-06-14T01:03:32.450Z @ha:ps5:error No data received from Playstation. If this error continues, your Playstation is likely powered off or unreachable - it will not be available until it is in either rest mode/powered on and reachable.
2024-06-14T01:03:32.451Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED
2024-06-14T01:03:37.977Z @ha:ps5:checkDevicesState {"address":{"address":"REDACTED","family":"IPv4","port":9302,"size":170},"hostRequestPort":997,"extras":{"statusLine":"620 Server Standby","statusCode":"620","statusMessage":"Server","status":"STANDBY"},"discoveryVersion":"00030010","systemVersion":"09400008","id":"REDACTED","name":"PS5-REDACTED","status":"STANDBY","type":"PS5"}

2024-06-14T01:03:37.977Z @ha:ps5:checkDevicesState Update HA
2024-06-14T01:03:37.982Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED
2024-06-14T01:03:43.527Z @ha:ps5:checkDevicesState {"address":{"address":"REDACTED","family":"IPv4","port":9302,"size":170},"hostRequestPort":997,"extras":{"statusLine":"620 Server Standby","statusCode":"620","statusMessage":"Server","status":"STANDBY"},"discoveryVersion":"00030010","systemVersion":"09400008","id":"REDACTED","name":"PS5-REDACTED","status":"STANDBY","type":"PS5"}

2024-06-14T01:03:49.057Z @ha:ps5:checkDevicesState {"address":{"address":"REDACTED","family":"IPv4","port":9302,"size":170},"hostRequestPort":997,"extras":{"statusLine":"620 Server Standby","statusCode":"620","statusMessage":"Server","status":"STANDBY"},"discoveryVersion":"00030010","systemVersion":"09400008","id":"REDACTED","name":"PS5-REDACTED","status":"STANDBY","type":"PS5"}

2 If the Playstation is completely powered off or unreachable, stdout will always be empty, so, the descriptive error message will continue to get logged. See below:

2024-06-14T01:09:12.554Z @ha:ps5:error No data received from Playstation. If this error continues, your Playstation is likely powered off or unreachable - it will not be available until it is in either rest mode/powered on and reachable.
2024-06-14T01:09:12.558Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED
2024-06-14T01:09:32.573Z @ha:ps5:error No data received from Playstation. If this error continues, your Playstation is likely powered off or unreachable - it will not be available until it is in either rest mode/powered on and reachable.
2024-06-14T01:09:32.574Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED
2024-06-14T01:09:52.597Z @ha:ps5:error No data received from Playstation. If this error continues, your Playstation is likely powered off or unreachable - it will not be available until it is in either rest mode/powered on and reachable.
2024-06-14T01:09:52.598Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED
2024-06-14T01:10:12.618Z @ha:ps5:error No data received from Playstation. If this error continues, your Playstation is likely powered off or unreachable - it will not be available until it is in either rest mode/powered on and reachable.
2024-06-14T01:10:12.619Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED
2024-06-14T01:10:32.634Z @ha:ps5:error No data received from Playstation. If this error continues, your Playstation is likely powered off or unreachable - it will not be available until it is in either rest mode/powered on and reachable.
2024-06-14T01:10:32.635Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED
2024-06-14T01:10:52.659Z @ha:ps5:error No data received from Playstation. If this error continues, your Playstation is likely powered off or unreachable - it will not be available until it is in either rest mode/powered on and reachable.
2024-06-14T01:10:52.661Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED
2024-06-14T01:11:12.670Z @ha:ps5:error No data received from Playstation. If this error continues, your Playstation is likely powered off or unreachable - it will not be available until it is in either rest mode/powered on and reachable.
2024-06-14T01:11:12.671Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED
2024-06-14T01:11:32.685Z @ha:ps5:error No data received from Playstation. If this error continues, your Playstation is likely powered off or unreachable - it will not be available until it is in either rest mode/powered on and reachable.
2024-06-14T01:11:32.686Z @ha:ps5:mqtt MQTT Message received ps5-mqtt/REDACTED

Related Issues

@RobertD502 RobertD502 changed the title Add discovery_topic option Add discovery_topic option, descriptive error when stdout empty Jun 14, 2024
@RobertD502 RobertD502 marked this pull request as draft June 16, 2024 16:19
…eachable devices

* Throw string instead of error to prevent including stack trace in debugger (since we already know where the message is originating from).
* Improve the message to include the scenario where are user's Playstation may be powered on, but it is unreachable on their network, which will also lead to empty `stdout`
@RobertD502 RobertD502 marked this pull request as ready for review June 16, 2024 17:06
@FunkeyFlo FunkeyFlo merged commit 73770e8 into FunkeyFlo:main Jul 27, 2024
1 of 6 checks passed
@FunkeyFlo
Copy link
Owner

FunkeyFlo commented Jul 27, 2024

Hi @RobertD502,

Thanks for your contributions to the repository. They are much appreciated!

The edge container has just been deployed. Would you mind giving it a test-run to see if all the (subsequent) merges have been successful?

@RobertD502
Copy link
Contributor Author

Will attempt to test tomorrow as I am away from my setup at the moment!

@FunkeyFlo
Copy link
Owner

@RobertD502 were you able to test the new image? 😊

@RobertD502
Copy link
Contributor Author

@FunkeyFlo Sorry for getting back to you later than expected. Tested with the edge image and it works great! All functions work as expected (powering on, entering rest mode, current activity attributes, etc.). In addition, making the PS5 unavailable by fully shutting it down results in the new message being logged, as expected.

One thing that I forgot to mention/suggest (it doesn't pertain to this PR), what do you think about, in the future, adding a polling timeout of a few seconds whenever the switch entity is used to power the system on/off? Reason being, when sending a power on/off command, it results in the switch flipping back to its previous state within the HA UI before it finally reverts back to the state that the user set it to - this is likely occurring because the backend is immediately polling the PS5 status after a command is sent and it takes a few seconds until the system enters into/returns the new state. I get around this in the integrations I built by sleeping before requesting new data for APIs that tend to take a few seconds (after a command is sent) to reflect and, as a result, return the actual new state of a device.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PS5 suddenly become unavailable SyntaxError: Unexpected end of JSON input [Feature request]: Add Home Assistant discovery topic config option
2 participants