Fix json overwriting if you have >1 PS4 connected (#35778)

This commit is contained in:
Boris Kaplounovsky 2020-06-03 03:55:55 +03:00 committed by GitHub
parent e94228fddf
commit 2f6ffe7068
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 16 deletions

View File

@ -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

View File

@ -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")

View File

@ -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."""

View File

@ -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