Usage

API should be accessed initially like the following examples.

Most functions can be accessed from the high-level PS4 object. You will need to pass in the IP address of your PS4 and your PSN credentials.

There are several async/asyncio coroutine functions in this module. These functions should be accessed with an Asyncio loop.

There are two versions of the Ps4 object/class: pyps4_2ndscreen.ps4.Ps4Legacy and pyps4_2ndscreen.ps4.Ps4Async. The pyps4_2ndscreen.ps4.Ps4Async version is recommended over the pyps4_2ndscreen.ps4.Ps4Legacy version which may be deprecated in the future. The difference between the two is that the pyps4_2ndscreen.ps4.Ps4Legacy class uses synchronous sockets (socket.socket) while the pyps4_2ndscreen.ps4.Ps4Async class uses asyncio transports and protocols. If using the Async version, a running asyncio event loop is required.

pyps4_2ndscreen.ps4.Ps4Legacy is suited for running single commands.

pyps4_2ndscreen.ps4.Ps4Async is best suited for a runtime environment/application.

Using Ps4Async Example

Initializing

  • First start an asyncio event loop in the main thread.

import asyncio

task = asyncio.ensure_future(YourProgram())
loop = asyncio.get_event_loop()
loop.run_until_complete()
  • Next you need to init the Device Discovery Protocol. This will enable you to get regular status updates.

from pyps4_2ndscreen.ddp import async_create_ddp_endpoint

_, ddp_protocol = await async_create_ddp_endpoint()
  • Then you can instantiate the Ps4Async class and assign the ddp_protocol object to the ps4 object.

from pyps4_2ndscreen.ps4 import Ps4Async

ip_address = '192.168.0.3'
creds = 'yourcredentials'
ps4 = Ps4Async(ip_address, creds)
ps4.set_protocol(ddp_protocol)

Getting Status

Status messages include various details such as the PS4 Standby/On status and the current game playing.

To get the status of the PS4 simply call:

status = ps4.get_status()

When the PS4 console is on and you have assigned the DDP Protocol to the Ps4 instance, you can add a callback to be called when the PS4 status updates. The callback must be a callable with no arguments.

ps4.add_callback(YourCallback)

The DDP protocol will now handle polling the PS4 console. However, when the PS4 goes into standby or is turned off, you will have to poll the PS4 yourself.

Controlling

To turn on from standby call:

ps4.wakeup()

In order to control the PS4 in other ways, you have to login as a registered PSN user on your PS4.

  • First you should connect. This merely connects a TCP connection with the PS4. Sometimes the PS4 will refuse the connection, especially right after turning it on.

await ps4.async_connect()
  • Then you can login.

await ps4.login()

The following are supported actions. (Logging in should be handled automatically):

  • Turning Off

await ps4.standby()
  • Launching a game or an app

await ps4.start_title('CUS10000')
  • Navigation functions

await ps4.remote_control('ps')

To logout we simply drop the connection.

await ps4.close()

Getting Title Information

The PS4 object provides a coroutine to fetch information of a title from the PSN store. You will need the following information: - The title ID - The title name - PSN Region of title

The first two can be retreived from the status dictionary like so.

status = ps4.status
title_id = status.get('running-app-titleid')
title_name = status.get('running-app-name')

The PSN region needs to be a key in the following dictionary. You can verify like below:

from pyps4_2ndscreen.media_art import COUNTRIES

def check_region():
    YourRegion = 'United States'
    if YourRegion in COUNTRIES:
        return True
    return False

You can call the search coroutine now and retrieve info like the url for the cover:

result = await ps4.async_get_ps_store_data(title_name, title_id, YourRegion)
cover = result.cover_art

Getting Credentials

Your PSN Credentials can be generated by running a CLI command or by using the Python interpreter:

Terminal Command:

pyps4-2ndscreen credential

If your system does not have setcap utilities your may run the following command:

sudo ./bin/pyps4-2ndscreen credential

or

Python:

from pyps4_2ndscreen.credential import Credentials
creds = Credentials()

YourCredentials = creds.start()

This will start the credential service and will return the credentials for the PSN Account. You will need to get the PS4 Second Screen app for Android or iOS to do this. Once you have logged in with your acccount in the app and started the service, refresh the devices in the app and select the device named ‘pyps4-2ndscreen’.