def test_loading_games_returns_dict(hass): """Test that loading games always returns a dict.""" with patch("homeassistant.components.ps4.load_json", side_effect=HomeAssistantError), patch( "homeassistant.components.ps4.save_json", side_effect=MagicMock()), patch("os.path.isfile", return_value=True): mock_games = ps4.load_games(hass) assert isinstance(mock_games, dict) assert not mock_games with patch("homeassistant.components.ps4.load_json", return_value="Some String"), patch( "homeassistant.components.ps4.save_json", side_effect=MagicMock()), patch("os.path.isfile", return_value=True): mock_games = ps4.load_games(hass) assert isinstance(mock_games, dict) assert not mock_games 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) assert isinstance(mock_games, dict) assert not mock_games
def test_load_games(hass): """Test that games are loaded correctly.""" with patch("homeassistant.components.ps4.load_json", return_value=MOCK_GAMES), patch( "homeassistant.components.ps4.save_json", side_effect=MagicMock()), patch("os.path.isfile", return_value=True): mock_games = ps4.load_games(hass) assert isinstance(mock_games, dict) mock_data = mock_games[MOCK_ID] assert isinstance(mock_data, dict) assert mock_data[ATTR_MEDIA_TITLE] == MOCK_TITLE assert mock_data[ATTR_MEDIA_IMAGE_URL] == MOCK_URL assert mock_data[ATTR_LOCKED] is False assert mock_data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_GAME
def update_list(self): """Update Game List, Correct data if different.""" if self._media_content_id in self._games: store = self._games[self._media_content_id] if (store.get(ATTR_MEDIA_TITLE) != self._media_title or store.get(ATTR_MEDIA_IMAGE_URL) != self._media_image): self._games.pop(self._media_content_id) if self._media_content_id not in self._games: self.add_games( self._media_content_id, self._media_title, self._media_image, self._media_type, ) self._games = load_games(self.hass) self.get_source_list()
def _parse_status(self): """Parse status.""" status = self._ps4.status if status is not None: self._games = load_games(self.hass) if self._games: self.get_source_list() self._retry = 0 self._disconnected = False if status.get("status") == "Ok": title_id = status.get("running-app-titleid") name = status.get("running-app-name") if title_id and name is not None: self._state = STATE_PLAYING if self._media_content_id != title_id: self._media_content_id = title_id if self._use_saved(): _LOGGER.debug("Using saved data for media: %s", title_id) self.schedule_update() return self._media_title = name self._source = self._media_title self._media_type = None # Get data from PS Store. asyncio.ensure_future( self.async_get_title_data(title_id, name)) else: if self._state != STATE_IDLE: self.idle() else: if self._state != STATE_STANDBY: self.state_standby() elif self._retry > DEFAULT_RETRIES: self.state_unknown() else: self._retry += 1
def test_games_reformat_to_dict(hass): """Test old data format is converted to new format.""" with patch( "homeassistant.components.ps4.load_json", return_value=MOCK_GAMES_DATA_OLD_STR_FORMAT, ), patch("homeassistant.components.ps4.save_json", side_effect=MagicMock()), patch("os.path.isfile", return_value=True): mock_games = ps4.load_games(hass) # New format is a nested dict. assert isinstance(mock_games, dict) assert mock_games["mock_id"][ATTR_MEDIA_TITLE] == "mock_title" assert mock_games["mock_id2"][ATTR_MEDIA_TITLE] == "mock_title2" for mock_game in mock_games: mock_data = mock_games[mock_game] assert isinstance(mock_data, dict) assert mock_data assert mock_data[ATTR_MEDIA_IMAGE_URL] is None assert mock_data[ATTR_LOCKED] is False assert mock_data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_GAME