Fix ps4 errors if pin begins with a 0 (#31198)

* Fix errors if pin begins with a 0

* Test PIN leading with zero

* Edit tests
This commit is contained in:
ktnrg45 2020-01-27 08:32:14 -07:00 committed by Charles Garwood
parent 7d9c8fdfa0
commit d3ac3e48a3
2 changed files with 51 additions and 5 deletions

View file

@ -30,6 +30,8 @@ UDP_PORT = 987
TCP_PORT = 997
PORT_MSG = {UDP_PORT: "port_987_bind_error", TCP_PORT: "port_997_bind_error"}
PIN_LENGTH = 8
@config_entries.HANDLERS.register(DOMAIN)
class PlayStation4FlowHandler(config_entries.ConfigFlow):
@ -143,7 +145,8 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
if user_input is not None:
self.region = user_input[CONF_REGION]
self.name = user_input[CONF_NAME]
self.pin = str(user_input[CONF_CODE])
# Assume pin had leading zeros, before coercing to int.
self.pin = str(user_input[CONF_CODE]).zfill(PIN_LENGTH)
self.host = user_input[CONF_IP_ADDRESS]
is_ready, is_login = await self.hass.async_add_executor_job(
@ -184,7 +187,7 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
list(regions)
)
link_schema[vol.Required(CONF_CODE)] = vol.All(
vol.Strip, vol.Length(min=8, max=8), vol.Coerce(int)
vol.Strip, vol.Length(max=PIN_LENGTH), vol.Coerce(int)
)
link_schema[vol.Required(CONF_NAME, default=DEFAULT_NAME)] = str

View file

@ -5,7 +5,12 @@ 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
from homeassistant.components.ps4.const import (
DEFAULT_ALIAS,
DEFAULT_NAME,
DEFAULT_REGION,
DOMAIN,
)
from homeassistant.const import (
CONF_CODE,
CONF_HOST,
@ -16,10 +21,12 @@ from homeassistant.const import (
)
from homeassistant.util import location
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, mock_coro
MOCK_TITLE = "PlayStation 4"
MOCK_CODE = "12345678"
MOCK_CODE = 12345678
MOCK_CODE_LEAD_0 = 1234567
MOCK_CODE_LEAD_0_STR = "01234567"
MOCK_CREDS = "000aa000"
MOCK_HOST = "192.0.0.0"
MOCK_HOST_ADDITIONAL = "192.0.0.1"
@ -293,6 +300,42 @@ async def test_additional_device(hass):
assert len(manager.async_entries()) == 2
async def test_0_pin(hass):
"""Test Pin with leading '0' is passed correctly."""
with patch("pyps4_2ndscreen.Helper.get_creds", return_value=MOCK_CREDS):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "creds"}, data={},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "mode"
with patch(
"pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}]
), patch(
"homeassistant.components.ps4.config_flow.location.async_detect_location_info",
return_value=mock_coro(MOCK_LOCATION),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], MOCK_AUTO
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "link"
mock_config = MOCK_CONFIG
mock_config[CONF_CODE] = MOCK_CODE_LEAD_0
with patch(
"pyps4_2ndscreen.Helper.link", return_value=(True, True)
) as mock_call, patch(
"pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}]
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], mock_config
)
mock_call.assert_called_once_with(
MOCK_HOST, MOCK_CREDS, MOCK_CODE_LEAD_0_STR, DEFAULT_ALIAS
)
async def test_no_devices_found_abort(hass):
"""Test that failure to find devices aborts flow."""
flow = ps4.PlayStation4FlowHandler()