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

This commit is contained in:
Boris Kaplounovsky authored and GitHub committed 2020-06-02 17:55:55 -07:00
1 parent e94228fddf
commit 2f6ffe7068
4 files changed
+16 -16

No files matched your search

+7 -7
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
+1 -1
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")
+3 -3
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."""