Switch for Playstation 4

Okay, getting failure and timeout… any thoughts?

e[31m17-04-26 16:07:31 ERROR (Thread-10) [custom_components.media_player.ps4] Command failed: ps4-waker -c /config/ps4-waker/.ps4-wake.credentials.json -d 192.168.1.184 -t 5000 --bind-port 997 searche[0m
e[31m17-04-26 16:07:42 ERROR (Thread-5) [custom_components.media_player.ps4] Timeout for command: ps4-waker -c /config/ps4-waker/.ps4-wake.credentials.json -d 192.168.1.184 -t 5000 --bind-port 997 e[0m

I really like the effort you made :thumbsup: . I did have a look at ps4-waker if i could convert it to python. But it’s all quite new to me. So i don’t see this happen very soon. So having it as a custom component i’m quite happy about. It’s the best thing we can get at the moment i think :slight_smile:.

As you maybe saw a few posts back, i miss used the camera component to show an image of the current game. As of now with your component it is a media_player so we could show images in it. When i used the camera component users had to download the pictures themselves. So that’s not very userfriendly. I did some searching to see if it possible to find some nice pictures based on the titleid but it doesn’t seem so.

So i don’t know if you also want to try to implement this? I could host pictures on my website. So that we can use it to download pictures as soon as ps4-games.json is updated. Or maybe somebody does have a better idea?

Does the credentials file contain information that would allow non LAN devices to communicate to the PS4? (Is it worth hiding?)

To be honest i am not 100% sure. But the credentials are used for LAN devices which are trying to connect. If it’s not on the same LAN it wouldn’t be allowed.
But I think it’s always better to be safe than sorry. So i wouldn’t show it to the world :slight_smile:

(I suppose if somebody got access to your network, your playstation could be turned on/off etc… not that i think anybody is interested in doing that :slight_smile:)

I think on “backing up” my configuration on Github. Currently it is a private repository, but maybe I make it public one day.

Thanks for your reply.

As far as I have tested it only works if you run with the --host option in docker because. Probably because of the way the ps4-waker tool works, but I have not used a lot of time on testing this.

I looked into adding images, but as you never found a way to lookup the titleid’s so having pictures would require some service where that option could be looked up

I tought about using something like giantbomb and then just lookup the game name, but that did not seem right either.

So if pictures should be added I guess someone needs to create a service where titleid can be mapped to a game name and image url

Oke. I tried linking it to my website where the images have their own path with {titleid}.jpg as name and i got it working.
I also tried to make it so it downloads the image to a folder and shows the local image, but i think it has to be an online link, a local path to a file is not working. So from an online source (for now my site) it is working. If you like it i could shoot in a pull request to review it?

I also encountered an error when the game name is: Tom Clancy’s Ghost Recon Wildlands or Tom Clancy’s The Division
So im pretty sure its because of the ? When this happens it doesn’t say the game name but says “idle”.
This is the error:

17-04-28 23:15:53 ERROR (MainThread) [homeassistant.helpers.entity] Update for media_player.playstation_4 fails
Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/custom_components/media_player/ps4.py", line 281, in search
    data = json.loads(value)
  File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.4/json/decoder.py", line 359, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Invalid \escape: line 11 column 32 (char 295)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/entity.py", line 225, in async_update_ha_state
    None, self.update)
  File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
    value = future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/util/__init__.py", line 303, in wrapper
    result = method(*args, **kwargs)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/util/__init__.py", line 303, in wrapper
    result = method(*args, **kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/media_player/ps4.py", line 94, in update
    data = self.ps4.search()
  File "/home/homeassistant/.homeassistant/custom_components/media_player/ps4.py", line 282, in search
    except json.decoder.JSONDecodeError:
AttributeError: 'module' object has no attribute 'JSONDecodeError'

I fixed the json parsing, and just send a pull request for the image handling.

I guess there is no good solution to it if we can’t find a public database with the titleid’s other than creating some site where people can add a mapping from a titleid to an image url

Thank you for fixing it so quickly. I was not thinking clearly and was already uploading images to the site, but that’s a bad idea ofcourse (copyright etc…) So we need indeed a mapping form titleid to image url. I have almost everything in place for it.
Only thing i have to figure out (again) is how to get the image url in home-assitant. I have different html tables as a possibility. But the software also allows to get a reply in json. But for now it only works for the administrator.
Getting a response as json would be the easiest way right? Getting data from html table is not so easy i think.

So for now i will figure out if i can get it to work for anyone.

Hey there.
I updated your script. It didn’t seem to work and only generated 3 spaces in the automation files.

Would love feedback.

Nice that you took the time to improve it. it looks nice.
But did you also see the custom component @hmn made?

This component automatically add games as they are started to the dropdown list. And you don’t need all the input selects and automations in your config. So i think it’s a much cleaner way to use that instead.

Oups. Missed that.

Thanks for pointing this out. Does that component have the same features as your scripts?

Yes, only it looks different in the home-assistant ui, because it sees it as a mediaplayer. When you click the three dots there will be a dropdown list to select your game. (first you need to start a game once manually, but it will be added to that dropdown list automatically every time a new game is detected.)

Just now images are not showing, but this will be added soon.

OK. Cool, thanks for the tipp.

Got an error that the JSON was not valid, it contains ’ and not ".

When adding this line of code, it worked fine:

could try running ps4-waker search and add the raw json

I added some debug so you can get the raw json, but I tried “faking” the json and added a game with a colom in the name and it worked fine for me

this is the generated ps4-games.json i got out of it

{
"CUSA02491": "Mass Effect\u2122", 
"CUSA01116": "YouTube", 
"CUSA01262": "Tom Clancy's The Division\u2122", 
"CUSA02152": "Mittelerde: Mordors Schatten - Game of the Year Edition"
}

the text change you made for replacing ’ with " seems to be problematic, because ’ should not be replaced or escaped inside the string

I thought the same about replacing the ’ with ". But strangely it worked even on titles that contain '.

Here is the JSON the component gets:
17-05-01 17:42:28 DEBUG (Thread-6) [custom_components.media_player.ps4] Return value: b"{ type: ‘device’,\n statusLine: ‘200 Ok’ …

As you see, they use ’ which is not valid for JSON.

Edit 1:

Edit 2:

I opened a ticket on the ps4-waker repo: https://github.com/dhleong/ps4-waker/issues/38

I created a PR on the ps4-waker repo, which should fix the dumping of the ugly JSON. So no more regex hacks.

https://github.com/dhleong/ps4-waker/pull/39

Edit 1:

After looking deeper into the API, we will have a little Problem.
When getting the status, the running-app-name is a short version of the full game (Which is not being exposed).
Now, when adding a game to the game.json file, they may not be distingishable by the user. This is in cases when

  1. Two or more games have the same name. (Example: BioShock: The Collection)
  2. Two or more games have similar names. (Example: Hatsune Miku: Project DIVA X, Hatsune Miku: Project DIVA Future Tone and Hatsune Miku: VR Future Live)