From 5b14c1da7314a6b84cb1e5475169967d99902c46 Mon Sep 17 00:00:00 2001 From: ktnrg45 <38207570+ktnrg45@users.noreply.github.com> Date: Fri, 18 Dec 2020 00:46:59 -0700 Subject: [PATCH] Bump pyps4-2ndscreen to 1.2.0 (#44273) * Bump PS4 to 1.2.0 * Add additional media_player tests * Fix tests --- homeassistant/components/ps4/config_flow.py | 11 ++++++++-- homeassistant/components/ps4/manifest.json | 2 +- homeassistant/components/ps4/media_player.py | 3 ++- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/ps4/test_config_flow.py | 3 ++- tests/components/ps4/test_media_player.py | 22 ++++++++++++++++++-- 7 files changed, 36 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/ps4/config_flow.py b/homeassistant/components/ps4/config_flow.py index a8bdb2b9c43..a494b96abf1 100644 --- a/homeassistant/components/ps4/config_flow.py +++ b/homeassistant/components/ps4/config_flow.py @@ -23,6 +23,7 @@ CONF_MODE = "Config Mode" CONF_AUTO = "Auto Discover" CONF_MANUAL = "Manual Entry" +LOCAL_UDP_PORT = 1988 UDP_PORT = 987 TCP_PORT = 997 PORT_MSG = {UDP_PORT: "port_987_bind_error", TCP_PORT: "port_997_bind_error"} @@ -107,8 +108,9 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow): if user_input is None: # Search for device. + # If LOCAL_UDP_PORT cannot be used, a random port will be selected. devices = await self.hass.async_add_executor_job( - self.helper.has_devices, self.m_device + self.helper.has_devices, self.m_device, LOCAL_UDP_PORT ) # Abort if can't find device. @@ -147,7 +149,12 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow): self.host = user_input[CONF_IP_ADDRESS] is_ready, is_login = await self.hass.async_add_executor_job( - self.helper.link, self.host, self.creds, self.pin, DEFAULT_ALIAS + self.helper.link, + self.host, + self.creds, + self.pin, + DEFAULT_ALIAS, + LOCAL_UDP_PORT, ) if is_ready is False: diff --git a/homeassistant/components/ps4/manifest.json b/homeassistant/components/ps4/manifest.json index 3527a05e5b3..500c243b8c9 100644 --- a/homeassistant/components/ps4/manifest.json +++ b/homeassistant/components/ps4/manifest.json @@ -3,6 +3,6 @@ "name": "Sony PlayStation 4", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/ps4", - "requirements": ["pyps4-2ndscreen==1.1.1"], + "requirements": ["pyps4-2ndscreen==1.2.0"], "codeowners": ["@ktnrg45"] } diff --git a/homeassistant/components/ps4/media_player.py b/homeassistant/components/ps4/media_player.py index 8ef9413edbf..24a1589db0d 100644 --- a/homeassistant/components/ps4/media_player.py +++ b/homeassistant/components/ps4/media_player.py @@ -3,6 +3,7 @@ import asyncio import logging from pyps4_2ndscreen.errors import NotReady, PSDataIncomplete +from pyps4_2ndscreen.media_art import TYPE_APP as PS_TYPE_APP import pyps4_2ndscreen.ps4 as pyps4 from homeassistant.components.media_player import MediaPlayerEntity @@ -262,7 +263,7 @@ class PS4Device(MediaPlayerEntity): app_name = title.name art = title.cover_art # Assume media type is game if not app. - if title.game_type != "App": + if title.game_type != PS_TYPE_APP: media_type = MEDIA_TYPE_GAME else: media_type = MEDIA_TYPE_APP diff --git a/requirements_all.txt b/requirements_all.txt index 3d610228601..fadf7ea3f20 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1622,7 +1622,7 @@ pypoint==2.0.0 pyprof2calltree==1.4.5 # homeassistant.components.ps4 -pyps4-2ndscreen==1.1.1 +pyps4-2ndscreen==1.2.0 # homeassistant.components.qvr_pro pyqvrpro==0.52 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 7ffaf2f83c3..755a5d6fac5 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -823,7 +823,7 @@ pypoint==2.0.0 pyprof2calltree==1.4.5 # homeassistant.components.ps4 -pyps4-2ndscreen==1.1.1 +pyps4-2ndscreen==1.2.0 # homeassistant.components.qwikswitch pyqwikswitch==0.93 diff --git a/tests/components/ps4/test_config_flow.py b/tests/components/ps4/test_config_flow.py index 0febf142d02..8ef11335199 100644 --- a/tests/components/ps4/test_config_flow.py +++ b/tests/components/ps4/test_config_flow.py @@ -4,6 +4,7 @@ import pytest from homeassistant import data_entry_flow from homeassistant.components import ps4 +from homeassistant.components.ps4.config_flow import LOCAL_UDP_PORT from homeassistant.components.ps4.const import ( DEFAULT_ALIAS, DEFAULT_NAME, @@ -360,7 +361,7 @@ async def test_0_pin(hass): result["flow_id"], mock_config ) mock_call.assert_called_once_with( - MOCK_HOST, MOCK_CREDS, MOCK_CODE_LEAD_0_STR, DEFAULT_ALIAS + MOCK_HOST, MOCK_CREDS, MOCK_CODE_LEAD_0_STR, DEFAULT_ALIAS, LOCAL_UDP_PORT ) diff --git a/tests/components/ps4/test_media_player.py b/tests/components/ps4/test_media_player.py index 4e22591d3b0..8a03f13beda 100644 --- a/tests/components/ps4/test_media_player.py +++ b/tests/components/ps4/test_media_player.py @@ -1,5 +1,7 @@ """Tests for the PS4 media player platform.""" from pyps4_2ndscreen.credential import get_ddp_message +from pyps4_2ndscreen.ddp import DEFAULT_UDP_PORT +from pyps4_2ndscreen.media_art import TYPE_APP as PS_TYPE_APP from homeassistant.components import ps4 from homeassistant.components.media_player.const import ( @@ -8,6 +10,7 @@ from homeassistant.components.media_player.const import ( ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_CONTENT_TYPE, ATTR_MEDIA_TITLE, + MEDIA_TYPE_APP, MEDIA_TYPE_GAME, ) from homeassistant.components.ps4.const import ( @@ -149,7 +152,7 @@ async def setup_mock_component(hass, entry=None): async def mock_ddp_response(hass, mock_status_data): """Mock raw UDP response from device.""" mock_protocol = hass.data[PS4_DATA].protocol - + assert mock_protocol.local_port == DEFAULT_UDP_PORT mock_code = mock_status_data.get("status_code") mock_status = mock_status_data.get("status") mock_status_header = f"{mock_code} {mock_status}" @@ -224,7 +227,7 @@ async def test_media_attributes_are_fetched(hass): mock_result = MagicMock() mock_result.name = MOCK_TITLE_NAME mock_result.cover_art = MOCK_TITLE_ART_URL - mock_result.game_type = "game" + mock_result.game_type = "not_an_app" with patch(mock_func, return_value=mock_result) as mock_fetch: await mock_ddp_response(hass, MOCK_STATUS_PLAYING) @@ -241,6 +244,21 @@ async def test_media_attributes_are_fetched(hass): assert mock_attrs.get(ATTR_MEDIA_TITLE) == MOCK_TITLE_NAME assert mock_attrs.get(ATTR_MEDIA_CONTENT_TYPE) == MOCK_TITLE_TYPE + # Change state so that the next fetch is called. + await mock_ddp_response(hass, MOCK_STATUS_STANDBY) + + # Test that content type of app is set. + mock_result.game_type = PS_TYPE_APP + + with patch(mock_func, return_value=mock_result) as mock_fetch_app: + await mock_ddp_response(hass, MOCK_STATUS_PLAYING) + + mock_state = hass.states.get(mock_entity_id) + mock_attrs = dict(mock_state.attributes) + + assert len(mock_fetch_app.mock_calls) == 1 + assert mock_attrs.get(ATTR_MEDIA_CONTENT_TYPE) == MEDIA_TYPE_APP + async def test_media_attributes_are_loaded(hass, patch_load_json): """Test that media attributes are loaded."""