From 1ecc883ef46120d569e39c288a0fd3cc4b35f8e7 Mon Sep 17 00:00:00 2001 From: ktnrg45 <38207570+ktnrg45@users.noreply.github.com> Date: Sun, 6 Oct 2019 05:43:34 -0700 Subject: [PATCH] PS4 bump to renamed dependency (#27144) * Change to renamed dependency pyps4-2ndscreen 0.9.0 * Rename / bump to ps4 dependency to 1.0.0 * update requirements * Rename test req * Fix import * Bump 1.0.1 * Fix flaky test leaving files behind --- homeassistant/components/ps4/__init__.py | 12 ++-- homeassistant/components/ps4/config_flow.py | 9 +-- homeassistant/components/ps4/manifest.json | 2 +- homeassistant/components/ps4/media_player.py | 6 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- script/gen_requirements_all.py | 2 +- tests/components/ps4/test_config_flow.py | 63 ++++++++++---------- tests/components/ps4/test_media_player.py | 24 ++++---- 9 files changed, 56 insertions(+), 66 deletions(-) diff --git a/homeassistant/components/ps4/__init__.py b/homeassistant/components/ps4/__init__.py index 60635bba525..205059be608 100644 --- a/homeassistant/components/ps4/__init__.py +++ b/homeassistant/components/ps4/__init__.py @@ -3,8 +3,8 @@ import logging import os import voluptuous as vol -from pyps4_homeassistant.ddp import async_create_ddp_endpoint -from pyps4_homeassistant.media_art import COUNTRIES +from pyps4_2ndscreen.ddp import async_create_ddp_endpoint +from pyps4_2ndscreen.media_art import COUNTRIES from homeassistant.components.media_player.const import ( ATTR_MEDIA_CONTENT_TYPE, @@ -172,12 +172,8 @@ def load_games(hass: HomeAssistantType) -> dict: _LOGGER.error("Games file was not parsed correctly") games = {} - # If file does not exist, create empty file. - if not os.path.isfile(g_file): - _LOGGER.info("Creating PS4 Games File") - games = {} - save_games(hass, games) - else: + # If file exists + if os.path.isfile(g_file): games = _reformat_data(hass, games) return games diff --git a/homeassistant/components/ps4/config_flow.py b/homeassistant/components/ps4/config_flow.py index a4b74077793..44523aea85a 100644 --- a/homeassistant/components/ps4/config_flow.py +++ b/homeassistant/components/ps4/config_flow.py @@ -2,6 +2,9 @@ from collections import OrderedDict import logging +from pyps4_2ndscreen.errors import CredentialTimeout +from pyps4_2ndscreen.helpers import Helper +from pyps4_2ndscreen.media_art import COUNTRIES import voluptuous as vol from homeassistant import config_entries @@ -37,8 +40,6 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow): def __init__(self): """Initialize the config flow.""" - from pyps4_homeassistant import Helper - self.helper = Helper() self.creds = None self.name = None @@ -61,8 +62,6 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow): async def async_step_creds(self, user_input=None): """Return PS4 credentials from 2nd Screen App.""" - from pyps4_homeassistant.errors import CredentialTimeout - errors = {} if user_input is not None: try: @@ -103,8 +102,6 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow): async def async_step_link(self, user_input=None): """Prompt user input. Create or edit entry.""" - from pyps4_homeassistant.media_art import COUNTRIES - regions = sorted(COUNTRIES.keys()) default_region = None errors = {} diff --git a/homeassistant/components/ps4/manifest.json b/homeassistant/components/ps4/manifest.json index 98a14d877e8..361711c400c 100644 --- a/homeassistant/components/ps4/manifest.json +++ b/homeassistant/components/ps4/manifest.json @@ -4,7 +4,7 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/ps4", "requirements": [ - "pyps4-homeassistant==0.8.7" + "pyps4-2ndscreen==1.0.1" ], "dependencies": [], "codeowners": [ diff --git a/homeassistant/components/ps4/media_player.py b/homeassistant/components/ps4/media_player.py index e1ec32ddd1f..3e8b667cd13 100644 --- a/homeassistant/components/ps4/media_player.py +++ b/homeassistant/components/ps4/media_player.py @@ -2,8 +2,8 @@ import logging import asyncio -import pyps4_homeassistant.ps4 as pyps4 -from pyps4_homeassistant.errors import NotReady +import pyps4_2ndscreen.ps4 as pyps4 +from pyps4_2ndscreen.errors import NotReady from homeassistant.core import callback from homeassistant.components.media_player import ENTITY_IMAGE_URL, MediaPlayerDevice @@ -254,7 +254,7 @@ class PS4Device(MediaPlayerDevice): async def async_get_title_data(self, title_id, name): """Get PS Store Data.""" - from pyps4_homeassistant.errors import PSDataIncomplete + from pyps4_2ndscreen.errors import PSDataIncomplete app_name = None art = None diff --git a/requirements_all.txt b/requirements_all.txt index c2c2c96a26f..39d4ddcda62 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1396,7 +1396,7 @@ pypjlink2==1.2.0 pypoint==1.1.1 # homeassistant.components.ps4 -pyps4-homeassistant==0.8.7 +pyps4-2ndscreen==1.0.1 # homeassistant.components.qwikswitch pyqwikswitch==0.93 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 8627ddb0d86..187e50c4691 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -347,7 +347,7 @@ pyotgw==0.5b0 pyotp==2.3.0 # homeassistant.components.ps4 -pyps4-homeassistant==0.8.7 +pyps4-2ndscreen==1.0.1 # homeassistant.components.qwikswitch pyqwikswitch==0.93 diff --git a/script/gen_requirements_all.py b/script/gen_requirements_all.py index 3c0941fc887..61174e86a43 100755 --- a/script/gen_requirements_all.py +++ b/script/gen_requirements_all.py @@ -141,7 +141,7 @@ TEST_REQUIREMENTS = ( "pyopenuv", "pyotgw", "pyotp", - "pyps4-homeassistant", + "pyps4-2ndscreen", "pyqwikswitch", "PyRMVtransport", "pysma", diff --git a/tests/components/ps4/test_config_flow.py b/tests/components/ps4/test_config_flow.py index 42f319e7343..81f81093a67 100644 --- a/tests/components/ps4/test_config_flow.py +++ b/tests/components/ps4/test_config_flow.py @@ -1,6 +1,8 @@ """Define tests for the PlayStation 4 config flow.""" from unittest.mock import patch +from pyps4_2ndscreen.errors import CredentialTimeout + from homeassistant import data_entry_flow from homeassistant.components import ps4 from homeassistant.components.ps4.const import DEFAULT_NAME, DEFAULT_REGION @@ -73,28 +75,28 @@ async def test_full_flow_implementation(hass): manager = hass.config_entries # User Step Started, results in Step Creds - with patch("pyps4_homeassistant.Helper.port_bind", return_value=None): + with patch("pyps4_2ndscreen.Helper.port_bind", return_value=None): result = await flow.async_step_user() assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "creds" # Step Creds results with form in Step Mode. - with patch("pyps4_homeassistant.Helper.get_creds", return_value=MOCK_CREDS): + with patch("pyps4_2ndscreen.Helper.get_creds", return_value=MOCK_CREDS): result = await flow.async_step_creds({}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "mode" # Step Mode with User Input which is not manual, results in Step Link. with patch( - "pyps4_homeassistant.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}] + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}] ): result = await flow.async_step_mode(MOCK_AUTO) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "link" # User Input results in created entry. - with patch("pyps4_homeassistant.Helper.link", return_value=(True, True)), patch( - "pyps4_homeassistant.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}] + with patch("pyps4_2ndscreen.Helper.link", return_value=(True, True)), patch( + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}] ): result = await flow.async_step_link(MOCK_CONFIG) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY @@ -126,20 +128,20 @@ async def test_multiple_flow_implementation(hass): manager = hass.config_entries # User Step Started, results in Step Creds - with patch("pyps4_homeassistant.Helper.port_bind", return_value=None): + with patch("pyps4_2ndscreen.Helper.port_bind", return_value=None): result = await flow.async_step_user() assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "creds" # Step Creds results with form in Step Mode. - with patch("pyps4_homeassistant.Helper.get_creds", return_value=MOCK_CREDS): + with patch("pyps4_2ndscreen.Helper.get_creds", return_value=MOCK_CREDS): result = await flow.async_step_creds({}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "mode" # Step Mode with User Input which is not manual, results in Step Link. with patch( - "pyps4_homeassistant.Helper.has_devices", + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}], ): result = await flow.async_step_mode(MOCK_AUTO) @@ -147,8 +149,8 @@ async def test_multiple_flow_implementation(hass): assert result["step_id"] == "link" # User Input results in created entry. - with patch("pyps4_homeassistant.Helper.link", return_value=(True, True)), patch( - "pyps4_homeassistant.Helper.has_devices", + with patch("pyps4_2ndscreen.Helper.link", return_value=(True, True)), patch( + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}], ): result = await flow.async_step_link(MOCK_CONFIG) @@ -175,8 +177,8 @@ async def test_multiple_flow_implementation(hass): # Test additional flow. # User Step Started, results in Step Mode: - with patch("pyps4_homeassistant.Helper.port_bind", return_value=None), patch( - "pyps4_homeassistant.Helper.has_devices", + with patch("pyps4_2ndscreen.Helper.port_bind", return_value=None), patch( + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}], ): result = await flow.async_step_user() @@ -184,14 +186,14 @@ async def test_multiple_flow_implementation(hass): assert result["step_id"] == "creds" # Step Creds results with form in Step Mode. - with patch("pyps4_homeassistant.Helper.get_creds", return_value=MOCK_CREDS): + with patch("pyps4_2ndscreen.Helper.get_creds", return_value=MOCK_CREDS): result = await flow.async_step_creds({}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "mode" # Step Mode with User Input which is not manual, results in Step Link. with patch( - "pyps4_homeassistant.Helper.has_devices", + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}], ): result = await flow.async_step_mode(MOCK_AUTO) @@ -200,9 +202,9 @@ async def test_multiple_flow_implementation(hass): # Step Link with patch( - "pyps4_homeassistant.Helper.has_devices", + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}], - ), patch("pyps4_homeassistant.Helper.link", return_value=(True, True)): + ), patch("pyps4_2ndscreen.Helper.link", return_value=(True, True)): result = await flow.async_step_link(MOCK_CONFIG_ADDITIONAL) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["data"][CONF_TOKEN] == MOCK_CREDS @@ -232,13 +234,13 @@ async def test_port_bind_abort(hass): flow = ps4.PlayStation4FlowHandler() flow.hass = hass - with patch("pyps4_homeassistant.Helper.port_bind", return_value=MOCK_UDP_PORT): + with patch("pyps4_2ndscreen.Helper.port_bind", return_value=MOCK_UDP_PORT): reason = "port_987_bind_error" result = await flow.async_step_user(user_input=None) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == reason - with patch("pyps4_homeassistant.Helper.port_bind", return_value=MOCK_TCP_PORT): + with patch("pyps4_2ndscreen.Helper.port_bind", return_value=MOCK_TCP_PORT): reason = "port_997_bind_error" result = await flow.async_step_user(user_input=None) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT @@ -253,7 +255,7 @@ async def test_duplicate_abort(hass): flow.creds = MOCK_CREDS with patch( - "pyps4_homeassistant.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}] + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}] ): result = await flow.async_step_link(user_input=None) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT @@ -274,9 +276,9 @@ async def test_additional_device(hass): assert len(manager.async_entries()) == 1 with patch( - "pyps4_homeassistant.Helper.has_devices", + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}], - ), patch("pyps4_homeassistant.Helper.link", return_value=(True, True)): + ), patch("pyps4_2ndscreen.Helper.link", return_value=(True, True)): result = await flow.async_step_link(MOCK_CONFIG_ADDITIONAL) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["data"][CONF_TOKEN] == MOCK_CREDS @@ -296,7 +298,7 @@ async def test_no_devices_found_abort(hass): flow = ps4.PlayStation4FlowHandler() flow.hass = hass - with patch("pyps4_homeassistant.Helper.has_devices", return_value=[]): + with patch("pyps4_2ndscreen.Helper.has_devices", return_value=[]): result = await flow.async_step_link() assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "no_devices_found" @@ -310,8 +312,7 @@ async def test_manual_mode(hass): # Step Mode with User Input: manual, results in Step Link. with patch( - "pyps4_homeassistant.Helper.has_devices", - return_value=[{"host-ip": flow.m_device}], + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": flow.m_device}] ): result = await flow.async_step_mode(MOCK_MANUAL) assert flow.m_device == MOCK_HOST @@ -324,7 +325,7 @@ async def test_credential_abort(hass): flow = ps4.PlayStation4FlowHandler() flow.hass = hass - with patch("pyps4_homeassistant.Helper.get_creds", return_value=None): + with patch("pyps4_2ndscreen.Helper.get_creds", return_value=None): result = await flow.async_step_creds({}) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "credential_error" @@ -332,12 +333,10 @@ async def test_credential_abort(hass): async def test_credential_timeout(hass): """Test that Credential Timeout shows error.""" - from pyps4_homeassistant.errors import CredentialTimeout - flow = ps4.PlayStation4FlowHandler() flow.hass = hass - with patch("pyps4_homeassistant.Helper.get_creds", side_effect=CredentialTimeout): + with patch("pyps4_2ndscreen.Helper.get_creds", side_effect=CredentialTimeout): result = await flow.async_step_creds({}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["errors"] == {"base": "credential_timeout"} @@ -349,8 +348,8 @@ async def test_wrong_pin_error(hass): flow.hass = hass flow.location = MOCK_LOCATION - with patch("pyps4_homeassistant.Helper.link", return_value=(True, False)), patch( - "pyps4_homeassistant.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}] + with patch("pyps4_2ndscreen.Helper.link", return_value=(True, False)), patch( + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}] ): result = await flow.async_step_link(MOCK_CONFIG) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM @@ -364,8 +363,8 @@ async def test_device_connection_error(hass): flow.hass = hass flow.location = MOCK_LOCATION - with patch("pyps4_homeassistant.Helper.link", return_value=(False, True)), patch( - "pyps4_homeassistant.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}] + with patch("pyps4_2ndscreen.Helper.link", return_value=(False, True)), patch( + "pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}] ): result = await flow.async_step_link(MOCK_CONFIG) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM diff --git a/tests/components/ps4/test_media_player.py b/tests/components/ps4/test_media_player.py index e4f2033c3cb..d6eeb31695c 100644 --- a/tests/components/ps4/test_media_player.py +++ b/tests/components/ps4/test_media_player.py @@ -1,7 +1,7 @@ """Tests for the PS4 media player platform.""" from unittest.mock import MagicMock, patch -from pyps4_homeassistant.credential import get_ddp_message +from pyps4_2ndscreen.credential import get_ddp_message from homeassistant.components import ps4 from homeassistant.components.media_player.const import ( @@ -295,9 +295,7 @@ async def test_media_attributes_are_loaded(hass): async def test_device_info_is_set_from_status_correctly(hass): """Test that device info is set correctly from status update.""" mock_d_registry = mock_device_registry(hass) - with patch( - "pyps4_homeassistant.ps4.get_status", return_value=MOCK_STATUS_OFF - ), patch(MOCK_SAVE, side_effect=MagicMock()): + with patch("pyps4_2ndscreen.ps4.get_status", return_value=MOCK_STATUS_OFF): mock_entity_id = await setup_mock_component(hass) await hass.async_block_till_done() @@ -447,9 +445,9 @@ async def test_media_stop(hass): async def test_select_source(hass): """Test that select source service calls function with title.""" mock_data = {MOCK_TITLE_ID: MOCK_GAMES_DATA} - with patch( - "pyps4_homeassistant.ps4.get_status", return_value=MOCK_STATUS_IDLE - ), patch(MOCK_LOAD, return_value=mock_data): + with patch("pyps4_2ndscreen.ps4.get_status", return_value=MOCK_STATUS_IDLE), patch( + MOCK_LOAD, return_value=mock_data + ): mock_entity_id = await setup_mock_component(hass) mock_func = "{}{}".format( @@ -473,9 +471,9 @@ async def test_select_source(hass): async def test_select_source_caps(hass): """Test that select source service calls function with upper case title.""" mock_data = {MOCK_TITLE_ID: MOCK_GAMES_DATA} - with patch( - "pyps4_homeassistant.ps4.get_status", return_value=MOCK_STATUS_IDLE - ), patch(MOCK_LOAD, return_value=mock_data): + with patch("pyps4_2ndscreen.ps4.get_status", return_value=MOCK_STATUS_IDLE), patch( + MOCK_LOAD, return_value=mock_data + ): mock_entity_id = await setup_mock_component(hass) mock_func = "{}{}".format( @@ -502,9 +500,9 @@ async def test_select_source_caps(hass): async def test_select_source_id(hass): """Test that select source service calls function with Title ID.""" mock_data = {MOCK_TITLE_ID: MOCK_GAMES_DATA} - with patch( - "pyps4_homeassistant.ps4.get_status", return_value=MOCK_STATUS_IDLE - ), patch(MOCK_LOAD, return_value=mock_data): + with patch("pyps4_2ndscreen.ps4.get_status", return_value=MOCK_STATUS_IDLE), patch( + MOCK_LOAD, return_value=mock_data + ): mock_entity_id = await setup_mock_component(hass) mock_func = "{}{}".format(