예제 #1
0
    def test_ps4(self):
        playstation = pyps4.Ps4('10.10.10.10')
        eq_(playstation._host, '10.10.10.10')
        eq_(playstation._broadcast, False)

        playstation = pyps4.Ps4(None, broadcast=True)
        eq_(playstation._host, None)
        eq_(playstation._broadcast, True)

        playstation = pyps4.Ps4('10.10.10.10', credential='abcdef')
        eq_(playstation._credential, 'abcdef')

        playstation = pyps4.Ps4('0.0.0.0', credentials_file=CREDENTIALS_FILE)
        eq_(playstation._credential, '1234567890')
예제 #2
0
    def test_get_host_id(self):
        mock = MagicMock()
        mock.side_effect = [
            {
                'host-id': 'test-A'
            },
            {
                'host-name': 'test-B'
            },
            {
                'running-app-titleid': 'test-C'
            },
            {
                'running-app-name': 'test-D'
            },
            {
                'system-version': 'test-E'
            },
        ]

        playstation = pyps4.Ps4('10.10.10.10')
        playstation.get_status = mock

        eq_(playstation.get_host_id(), 'test-A')
        eq_(playstation.get_host_name(), 'test-B')
        eq_(playstation.get_running_app_titleid(), 'test-C')
        eq_(playstation.get_running_app_name(), 'test-D')
        eq_(playstation.get_system_version(), 'test-E')
예제 #3
0
def _check_ps4(host, credentials):
    """Check if PS4 is responding."""
    import pyps4

    if host is None:
        return False

    if credentials is None:
        return False

    try:
        try:
            playstation = pyps4.Ps4(host, credentials)
            info = playstation.get_status()
            _LOGGER.debug("Searched for PS4 [%s] on network and got : %s",
                          host, info)
        except IOError as error:
            _LOGGER.error("Error connecting to PS4 [%s] : %s", host, error)
            return False
        finally:
            pass

    except (IOError, OSError) as error:
        _LOGGER.error("Error loading PS4 [%s] credentials : %s", host, error)
        return False

    return True
예제 #4
0
def setup_ps4(host, name, hass, config, add_devices, credentials):
    """Set up PS4."""
    import pyps4
    games_filename = hass.config.path(config.get(CONF_GAMES_FILENAME))
    local_store = config.get(CONF_LOCAL_STORE)

    try:
        ps4 = pyps4.Ps4(host, credentials)
    except (IOError, OSError) as error:
        _LOGGER.error("Error loading PS4 credentials [%s] : %s", host, error)

    add_devices([PS4Device(name, ps4, local_store, games_filename)], True)
예제 #5
0
    def test_get_host_status(self):
        mock = MagicMock()
        mock.side_effect = [
            {
                'status_code': 200
            },
            {
                'status_code': 620
            },
        ]

        playstation = pyps4.Ps4('10.10.10.10')
        playstation.get_status = mock
        eq_(playstation.get_host_status(), 200)
        eq_(playstation.get_host_status(), 620)
예제 #6
0
    def test_is_standby(self):
        mock = MagicMock()
        mock.side_effect = [
            {
                'status_code': 620
            },
            {
                'status_code': 200
            },
            {
                'status_code': 100
            },
        ]

        playstation = pyps4.Ps4('10.10.10.10')
        playstation.get_status = mock
        eq_(playstation.is_standby(), True)
        eq_(playstation.is_standby(), False)
        eq_(playstation.is_standby(), False)
예제 #7
0
def main(args=None):
    """The main function."""
    parser = argparse.ArgumentParser(description='PS4 CLI tool.')
    parser.add_argument('-v',
                        action='store_true',
                        dest='verbose',
                        help='be more verbose')
    parser.add_argument('-H',
                        '--host',
                        type=str,
                        dest='host',
                        help='PS4 IP address',
                        default=None)
    parser.add_argument('-c',
                        '--credential_file',
                        type=str,
                        dest='credential_file',
                        default=None,
                        help='The credential file')
    parser.add_argument('-C',
                        '--credential',
                        type=str,
                        dest='credential',
                        default=None,
                        help='The credential as string')
    parser.add_argument('-V',
                        '--version',
                        action='version',
                        version='{version}'.format(version=__version__),
                        help='Print version')

    _sub = parser.add_subparsers(title='Commands')
    _sub.required = True

    # search all devices
    subparser = _sub.add_parser('search', help='Search for PS4 devices')
    subparser.set_defaults(func=cmd_search)

    # info
    subparser = _sub.add_parser('status', help='Show current status')
    subparser.set_defaults(func=cmd_status)

    # launch
    subparser = _sub.add_parser('launch', help='Show current status')
    subparser.set_defaults(func=cmd_launch)

    # wake
    subparser = _sub.add_parser('wakeup', help='Wakeup the PS4')
    subparser.set_defaults(func=cmd_wakeup)

    # login
    subparser = _sub.add_parser('login', help='Login the PS4')
    subparser.set_defaults(func=cmd_login)

    # standby
    subparser = _sub.add_parser('standby', help='Standby the PS4')
    subparser.set_defaults(func=cmd_standby)

    # start
    subparser = _sub.add_parser('start', help='Start a title')
    subparser.add_argument('title_id',
                           type=str,
                           metavar="TITLE",
                           help='Game title')
    subparser.set_defaults(func=cmd_start_title)

    # remote
    subparser = _sub.add_parser('remote', help='Send remote control')
    subparser.add_argument('button', type=str, metavar="BUTTON", help='button')
    subparser.add_argument('hold_time',
                           type=int,
                           default=0,
                           metavar="HOLD_TIME",
                           help='hold time')
    subparser.set_defaults(func=cmd_remote_control)

    args = parser.parse_args(args)

    playstation = None

    if not hasattr(args, 'func'):
        parser.print_help()
        sys.exit()

    if args.verbose:
        logging.basicConfig()
        logging.getLogger('pyps4').setLevel(logging.DEBUG)

    try:
        playstation = pyps4.Ps4(args.host,
                                credential=args.credential,
                                credentials_file=args.credential_file)
        args.func(playstation, args)
    finally:
        pass