Add PlayStation Network Integration (#133901)

* clean pull request

* Create one device per console

* Requested changes

* Pr/tr4nt0r/1 (#2)

* clean pull request

* Create one device per console

* device setup

* Merge PR1 - Dynamic Device Support

* Merge PR1 - Dynamic Device Support

---------

Co-authored-by: tr4nt0r <4445816+tr4nt0r@users.noreply.github.com>

* nitpicks

* Update config_flow test

* Update quality_scale.yaml

* repair integrations.json

* minor updates

* Add translation string for invalid account

* misc changes post review

* Minor strings updates

* strengthen config_flow test

* Requested changes

* Applied patch to commit a358725

* migrate PlayStationNetwork helper classes to HA

* Revert to standard psn library

* Updates to media_player logic

* add default_factory, change registered_platforms to set

* Improve test coverage

* Add snapshot test for media_player platform

* fix token parse error

* Parametrize media player test

* Add PS3 support

* Add PS3 support

* Add concurrent console support

* Adjust psnawp rate limit

* Convert to package PlatformType

* Update dependency to PSNAWP==3.0.0

* small improvements

* Add PlayStation PC Support

* Refactor active sessions list

* shift async logic to helper

* Implemented suggested changes

* Suggested changes

* Updated tests

* Suggested changes

* Fix test

* Suggested changes

* Suggested changes

* Update config_flow tests

* Group remaining api call in single executor

---------

Co-authored-by: tr4nt0r <4445816+tr4nt0r@users.noreply.github.com>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Jack Powell
2025-06-23 17:46:06 -04:00
committed by GitHub
parent 646ddf9c2d
commit c671ff3cf1
21 changed files with 1317 additions and 1 deletions

View File

@ -0,0 +1,113 @@
"""Common fixtures for the Playstation Network tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from homeassistant.components.playstation_network.const import CONF_NPSSO, DOMAIN
from tests.common import MockConfigEntry
NPSSO_TOKEN: str = "npsso-token"
NPSSO_TOKEN_INVALID_JSON: str = "{'npsso': 'npsso-token'"
PSN_ID: str = "my-psn-id"
@pytest.fixture(name="config_entry")
def mock_config_entry() -> MockConfigEntry:
"""Mock PlayStation Network configuration entry."""
return MockConfigEntry(
domain=DOMAIN,
title="test-user",
data={
CONF_NPSSO: NPSSO_TOKEN,
},
unique_id=PSN_ID,
)
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.playstation_network.async_setup_entry",
return_value=True,
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def mock_user() -> Generator[MagicMock]:
"""Mock psnawp_api User object."""
with patch(
"homeassistant.components.playstation_network.helpers.User",
autospec=True,
) as mock_client:
client = mock_client.return_value
client.account_id = PSN_ID
client.online_id = "testuser"
client.get_presence.return_value = {
"basicPresence": {
"availability": "availableToPlay",
"primaryPlatformInfo": {"onlineStatus": "online", "platform": "PS5"},
"gameTitleInfoList": [
{
"npTitleId": "PPSA07784_00",
"titleName": "STAR WARS Jedi: Survivor™",
"format": "PS5",
"launchPlatform": "PS5",
"conceptIconUrl": "https://image.api.playstation.com/vulcan/ap/rnd/202211/2222/l8QTN7ThQK3lRBHhB3nX1s7h.png",
}
],
}
}
yield client
@pytest.fixture
def mock_psnawpapi(mock_user: MagicMock) -> Generator[MagicMock]:
"""Mock psnawp_api."""
with patch(
"homeassistant.components.playstation_network.helpers.PSNAWP",
autospec=True,
) as mock_client:
client = mock_client.return_value
client.user.return_value = mock_user
client.me.return_value.get_account_devices.return_value = [
{
"deviceId": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234",
"deviceType": "PS5",
"activationType": "PRIMARY",
"activationDate": "2021-01-14T18:00:00.000Z",
"accountDeviceVector": "abcdefghijklmnopqrstuv",
}
]
yield client
@pytest.fixture
def mock_psnawp_npsso(mock_user: MagicMock) -> Generator[MagicMock]:
"""Mock psnawp_api."""
with patch(
"psnawp_api.utils.misc.parse_npsso_token",
autospec=True,
) as mock_parse_npsso_token:
npsso = mock_parse_npsso_token.return_value
npsso.parse_npsso_token.return_value = NPSSO_TOKEN
yield npsso
@pytest.fixture
def mock_token() -> Generator[MagicMock]:
"""Mock token generator."""
with patch("secrets.token_hex", return_value="123456789") as token:
yield token