Beispiel #1
0
def test_loading_games_returns_dict(opp):
    """Test that loading games always returns a dict."""
    with patch("openpeerpower.components.ps4.load_json",
               side_effect=OpenPeerPowerError), patch(
                   "openpeerpower.components.ps4.save_json",
                   side_effect=MagicMock()), patch("os.path.isfile",
                                                   return_value=True):
        mock_games = ps4.load_games(opp, MOCK_ENTRY_ID)

    assert isinstance(mock_games, dict)
    assert not mock_games

    with patch("openpeerpower.components.ps4.load_json",
               return_value="Some String"), patch(
                   "openpeerpower.components.ps4.save_json",
                   side_effect=MagicMock()), patch("os.path.isfile",
                                                   return_value=True):
        mock_games = ps4.load_games(opp, MOCK_ENTRY_ID)

    assert isinstance(mock_games, dict)
    assert not mock_games

    with patch("openpeerpower.components.ps4.load_json",
               return_value=[]), patch(
                   "openpeerpower.components.ps4.save_json",
                   side_effect=MagicMock()), patch("os.path.isfile",
                                                   return_value=True):
        mock_games = ps4.load_games(opp, MOCK_ENTRY_ID)

    assert isinstance(mock_games, dict)
    assert not mock_games
Beispiel #2
0
def test_load_games(opp):
    """Test that games are loaded correctly."""
    with patch("openpeerpower.components.ps4.load_json",
               return_value=MOCK_GAMES), patch(
                   "openpeerpower.components.ps4.save_json",
                   side_effect=MagicMock()), patch("os.path.isfile",
                                                   return_value=True):
        mock_games = ps4.load_games(opp, MOCK_ENTRY_ID)

    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
Beispiel #3
0
    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.opp, self._unique_id)

        self.get_source_list()
Beispiel #4
0
    def _parse_status(self):
        """Parse status."""
        status = self._ps4.status

        if status is not None:
            self._games = load_games(self.opp, self._unique_id)
            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)
                            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
Beispiel #5
0
def test_games_reformat_to_dict(opp):
    """Test old data format is converted to new format."""
    with patch(
            "openpeerpower.components.ps4.load_json",
            return_value=MOCK_GAMES_DATA_OLD_STR_FORMAT,
    ), patch("openpeerpower.components.ps4.save_json",
             side_effect=MagicMock()), patch("os.path.isfile",
                                             return_value=True):
        mock_games = ps4.load_games(opp, MOCK_ENTRY_ID)

    # 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