diff --git a/homeassistant/components/ps4/__init__.py b/homeassistant/components/ps4/__init__.py index d2c6e9859de..2a7a667088e 100644 --- a/homeassistant/components/ps4/__init__.py +++ b/homeassistant/components/ps4/__init__.py @@ -157,9 +157,9 @@ def format_unique_id(creds, mac_address): return f"{mac_address}_{suffix}" -def load_games(hass: HomeAssistantType) -> dict: +def load_games(hass: HomeAssistantType, unique_id: str) -> dict: """Load games for sources.""" - g_file = hass.config.path(GAMES_FILE) + g_file = hass.config.path(GAMES_FILE.format(unique_id)) try: games = load_json(g_file, dict) except HomeAssistantError as error: @@ -172,20 +172,20 @@ def load_games(hass: HomeAssistantType) -> dict: # If file exists if os.path.isfile(g_file): - games = _reformat_data(hass, games) + games = _reformat_data(hass, games, unique_id) return games -def save_games(hass: HomeAssistantType, games: dict): +def save_games(hass: HomeAssistantType, games: dict, unique_id: str): """Save games to file.""" - g_file = hass.config.path(GAMES_FILE) + g_file = hass.config.path(GAMES_FILE.format(unique_id)) try: save_json(g_file, games) except OSError as error: _LOGGER.error("Could not save game list, %s", error) -def _reformat_data(hass: HomeAssistantType, games: dict) -> dict: +def _reformat_data(hass: HomeAssistantType, games: dict, unique_id: str) -> dict: """Reformat data to correct format.""" data_reformatted = False @@ -204,7 +204,7 @@ def _reformat_data(hass: HomeAssistantType, games: dict) -> dict: _LOGGER.debug("Reformatting media data for item: %s, %s", game, data) if data_reformatted: - save_games(hass, games) + save_games(hass, games, unique_id) return games diff --git a/homeassistant/components/ps4/const.py b/homeassistant/components/ps4/const.py index 779da61ca48..0974286ebe8 100644 --- a/homeassistant/components/ps4/const.py +++ b/homeassistant/components/ps4/const.py @@ -5,7 +5,7 @@ DEFAULT_NAME = "PlayStation 4" DEFAULT_REGION = "United States" DEFAULT_ALIAS = "Home-Assistant" DOMAIN = "ps4" -GAMES_FILE = ".ps4-games.json" +GAMES_FILE = ".ps4-games.{}.json" PS4_DATA = "ps4_data" COMMANDS = ("up", "down", "right", "left", "enter", "back", "option", "ps", "ps_hold") diff --git a/homeassistant/components/ps4/media_player.py b/homeassistant/components/ps4/media_player.py index 39b60be0493..9bd4ddbefbe 100644 --- a/homeassistant/components/ps4/media_player.py +++ b/homeassistant/components/ps4/media_player.py @@ -163,7 +163,7 @@ class PS4Device(MediaPlayerEntity): status = self._ps4.status if status is not None: - self._games = load_games(self.hass) + self._games = load_games(self.hass, self._unique_id) if self._games: self.get_source_list() @@ -300,7 +300,7 @@ class PS4Device(MediaPlayerEntity): self._media_image, self._media_type, ) - self._games = load_games(self.hass) + self._games = load_games(self.hass, self._unique_id) self.get_source_list() @@ -324,7 +324,7 @@ class PS4Device(MediaPlayerEntity): } } games.update(game) - save_games(self.hass, games) + save_games(self.hass, games, self._unique_id) async def async_get_device_info(self, status): """Set device info for registry.""" diff --git a/tests/components/ps4/test_init.py b/tests/components/ps4/test_init.py index 7f7eff33ebb..75a983cc97a 100644 --- a/tests/components/ps4/test_init.py +++ b/tests/components/ps4/test_init.py @@ -201,7 +201,7 @@ def test_games_reformat_to_dict(hass): ), patch("homeassistant.components.ps4.save_json", side_effect=MagicMock()), patch( "os.path.isfile", return_value=True ): - mock_games = ps4.load_games(hass) + mock_games = ps4.load_games(hass, MOCK_ENTRY_ID) # New format is a nested dict. assert isinstance(mock_games, dict) @@ -223,7 +223,7 @@ def test_load_games(hass): ), patch("homeassistant.components.ps4.save_json", side_effect=MagicMock()), patch( "os.path.isfile", return_value=True ): - mock_games = ps4.load_games(hass) + mock_games = ps4.load_games(hass, MOCK_ENTRY_ID) assert isinstance(mock_games, dict) @@ -242,7 +242,7 @@ def test_loading_games_returns_dict(hass): ), patch("homeassistant.components.ps4.save_json", side_effect=MagicMock()), patch( "os.path.isfile", return_value=True ): - mock_games = ps4.load_games(hass) + mock_games = ps4.load_games(hass, MOCK_ENTRY_ID) assert isinstance(mock_games, dict) assert not mock_games @@ -252,7 +252,7 @@ def test_loading_games_returns_dict(hass): ), patch("homeassistant.components.ps4.save_json", side_effect=MagicMock()), patch( "os.path.isfile", return_value=True ): - mock_games = ps4.load_games(hass) + mock_games = ps4.load_games(hass, MOCK_ENTRY_ID) assert isinstance(mock_games, dict) assert not mock_games @@ -260,7 +260,7 @@ def test_loading_games_returns_dict(hass): with patch("homeassistant.components.ps4.load_json", return_value=[]), patch( "homeassistant.components.ps4.save_json", side_effect=MagicMock() ), patch("os.path.isfile", return_value=True): - mock_games = ps4.load_games(hass) + mock_games = ps4.load_games(hass, MOCK_ENTRY_ID) assert isinstance(mock_games, dict) assert not mock_games