Add support for multiple devices for PS4 component (#21302)
* Support multiple devices. * Revert "Support multiple devices." This reverts commit 3f5d4462a98da13ebb1ab1c07d341dbd7020e6cc. * Support multiple devices * Bump to 0.3.3 * bump 0.3.4 * Add tests for multiple devices. * Update Requirements * Update config_flow.py * Update config_flow.py * fixed typo * Reordered functions * Added multiple flow implementation test. * fix * typo * fix tests * bump 0.4.0 * Bump 0.4.0 * 0.4.0 * bump version * bump version * bump version * Add keep alive feature with multiple devices * bump version * bump version * bump version * bump 0.4.7 * bump 0.4.7 * bump 0.4.7 * Edited tests. * bump/pylint * pylint * bump/pylint * bump/pylint * Change to add additional entry * Changed to multiple entries * pylint * Corrections to manage multiple devices. * lint * comments * Removed redundant for loop * Shorthand correction * Remove reference to private object * Test fix * Revert changes. Test failure. * Test fix * test fix * unindent assertions * pylint
This commit is contained in:
@@ -14,20 +14,32 @@ MOCK_TITLE = 'PlayStation 4'
|
||||
MOCK_CODE = '12345678'
|
||||
MOCK_CREDS = '000aa000'
|
||||
MOCK_HOST = '192.0.0.0'
|
||||
MOCK_HOST_ADDITIONAL = '192.0.0.1'
|
||||
MOCK_DEVICE = {
|
||||
CONF_HOST: MOCK_HOST,
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_REGION: DEFAULT_REGION
|
||||
}
|
||||
MOCK_DEVICE_ADDITIONAL = {
|
||||
CONF_HOST: MOCK_HOST_ADDITIONAL,
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_REGION: DEFAULT_REGION
|
||||
}
|
||||
MOCK_CONFIG = {
|
||||
CONF_IP_ADDRESS: MOCK_HOST,
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_REGION: DEFAULT_REGION,
|
||||
CONF_CODE: MOCK_CODE
|
||||
}
|
||||
MOCK_CONFIG_ADDITIONAL = {
|
||||
CONF_IP_ADDRESS: MOCK_HOST_ADDITIONAL,
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_REGION: DEFAULT_REGION,
|
||||
CONF_CODE: MOCK_CODE
|
||||
}
|
||||
MOCK_DATA = {
|
||||
CONF_TOKEN: MOCK_CREDS,
|
||||
'devices': MOCK_DEVICE
|
||||
'devices': [MOCK_DEVICE]
|
||||
}
|
||||
MOCK_UDP_PORT = int(987)
|
||||
MOCK_TCP_PORT = int(997)
|
||||
@@ -37,13 +49,14 @@ async def test_full_flow_implementation(hass):
|
||||
"""Test registering an implementation and flow works."""
|
||||
flow = ps4.PlayStation4FlowHandler()
|
||||
flow.hass = hass
|
||||
manager = hass.config_entries
|
||||
|
||||
# User Step Started, results in Step Creds
|
||||
with patch('pyps4_homeassistant.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'
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'creds'
|
||||
|
||||
# Step Creds results with form in Step Link.
|
||||
with patch('pyps4_homeassistant.Helper.get_creds',
|
||||
@@ -51,8 +64,8 @@ async def test_full_flow_implementation(hass):
|
||||
patch('pyps4_homeassistant.Helper.has_devices',
|
||||
return_value=[{'host-ip': MOCK_HOST}]):
|
||||
result = await flow.async_step_creds({})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'link'
|
||||
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',
|
||||
@@ -60,10 +73,110 @@ async def test_full_flow_implementation(hass):
|
||||
patch('pyps4_homeassistant.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
|
||||
assert result['data'][CONF_TOKEN] == MOCK_CREDS
|
||||
assert result['data']['devices'] == [MOCK_DEVICE]
|
||||
assert result['title'] == MOCK_TITLE
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result['data'][CONF_TOKEN] == MOCK_CREDS
|
||||
assert result['data']['devices'] == [MOCK_DEVICE]
|
||||
assert result['title'] == MOCK_TITLE
|
||||
|
||||
# Add entry using result data.
|
||||
mock_data = {
|
||||
CONF_TOKEN: result['data'][CONF_TOKEN],
|
||||
'devices': result['data']['devices']}
|
||||
entry = MockConfigEntry(domain=ps4.DOMAIN, data=mock_data)
|
||||
entry.add_to_manager(manager)
|
||||
|
||||
# Check if entry exists.
|
||||
assert len(manager.async_entries()) == 1
|
||||
# Check if there is a device config in entry.
|
||||
assert len(entry.data['devices']) == 1
|
||||
|
||||
|
||||
async def test_multiple_flow_implementation(hass):
|
||||
"""Test multiple device flows."""
|
||||
flow = ps4.PlayStation4FlowHandler()
|
||||
flow.hass = hass
|
||||
manager = hass.config_entries
|
||||
|
||||
# User Step Started, results in Step Creds
|
||||
with patch('pyps4_homeassistant.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 Link.
|
||||
with patch('pyps4_homeassistant.Helper.get_creds',
|
||||
return_value=MOCK_CREDS), \
|
||||
patch('pyps4_homeassistant.Helper.has_devices',
|
||||
return_value=[{'host-ip': MOCK_HOST},
|
||||
{'host-ip': MOCK_HOST_ADDITIONAL}]):
|
||||
result = await flow.async_step_creds({})
|
||||
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},
|
||||
{'host-ip': MOCK_HOST_ADDITIONAL}]):
|
||||
result = await flow.async_step_link(MOCK_CONFIG)
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result['data'][CONF_TOKEN] == MOCK_CREDS
|
||||
assert result['data']['devices'] == [MOCK_DEVICE]
|
||||
assert result['title'] == MOCK_TITLE
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Add entry using result data.
|
||||
mock_data = {
|
||||
CONF_TOKEN: result['data'][CONF_TOKEN],
|
||||
'devices': result['data']['devices']}
|
||||
entry = MockConfigEntry(domain=ps4.DOMAIN, data=mock_data)
|
||||
entry.add_to_manager(manager)
|
||||
|
||||
# Check if entry exists.
|
||||
assert len(manager.async_entries()) == 1
|
||||
# Check if there is a device config in entry.
|
||||
assert len(entry.data['devices']) == 1
|
||||
|
||||
# Test additional flow.
|
||||
|
||||
# User Step Started, results in Step Link:
|
||||
with patch('pyps4_homeassistant.Helper.port_bind',
|
||||
return_value=None), \
|
||||
patch('pyps4_homeassistant.Helper.has_devices',
|
||||
return_value=[{'host-ip': MOCK_HOST},
|
||||
{'host-ip': MOCK_HOST_ADDITIONAL}]):
|
||||
result = await flow.async_step_user()
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'link'
|
||||
|
||||
# Step Link
|
||||
with patch('pyps4_homeassistant.Helper.has_devices',
|
||||
return_value=[{'host-ip': MOCK_HOST},
|
||||
{'host-ip': MOCK_HOST_ADDITIONAL}]), \
|
||||
patch('pyps4_homeassistant.Helper.link',
|
||||
return_value=(True, True)):
|
||||
result = await flow.async_step_link(user_input=MOCK_CONFIG_ADDITIONAL)
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result['data'][CONF_TOKEN] == MOCK_CREDS
|
||||
assert len(result['data']['devices']) == 1
|
||||
assert result['title'] == MOCK_TITLE
|
||||
|
||||
mock_data = {
|
||||
CONF_TOKEN: result['data'][CONF_TOKEN],
|
||||
'devices': result['data']['devices']}
|
||||
|
||||
# Update config entries with result data
|
||||
entry = MockConfigEntry(domain=ps4.DOMAIN, data=mock_data)
|
||||
entry.add_to_manager(manager)
|
||||
manager.async_update_entry(entry)
|
||||
|
||||
# Check if there are 2 entries.
|
||||
assert len(manager.async_entries()) == 2
|
||||
# Check if there is device config in entry.
|
||||
assert len(entry.data['devices']) == 1
|
||||
|
||||
|
||||
async def test_port_bind_abort(hass):
|
||||
@@ -75,28 +188,62 @@ async def test_port_bind_abort(hass):
|
||||
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
|
||||
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):
|
||||
reason = 'port_997_bind_error'
|
||||
result = await flow.async_step_user(user_input=None)
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result['reason'] == reason
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result['reason'] == reason
|
||||
|
||||
|
||||
async def test_duplicate_abort(hass):
|
||||
"""Test that Flow aborts when already configured."""
|
||||
"""Test that Flow aborts when found devices already configured."""
|
||||
MockConfigEntry(domain=ps4.DOMAIN, data=MOCK_DATA).add_to_hass(hass)
|
||||
flow = ps4.PlayStation4FlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
result = await flow.async_step_user(user_input=None)
|
||||
with patch('pyps4_homeassistant.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
|
||||
assert result['reason'] == 'devices_configured'
|
||||
|
||||
|
||||
async def test_additional_device(hass):
|
||||
"""Test that Flow can configure another device."""
|
||||
flow = ps4.PlayStation4FlowHandler()
|
||||
flow.hass = hass
|
||||
flow.creds = MOCK_CREDS
|
||||
manager = hass.config_entries
|
||||
|
||||
# Mock existing entry.
|
||||
entry = MockConfigEntry(domain=ps4.DOMAIN, data=MOCK_DATA)
|
||||
entry.add_to_manager(manager)
|
||||
# Check that only 1 entry exists
|
||||
assert len(manager.async_entries()) == 1
|
||||
|
||||
with patch('pyps4_homeassistant.Helper.has_devices',
|
||||
return_value=[{'host-ip': MOCK_HOST},
|
||||
{'host-ip': MOCK_HOST_ADDITIONAL}]), \
|
||||
patch('pyps4_homeassistant.Helper.link',
|
||||
return_value=(True, True)):
|
||||
result = await flow.async_step_link(user_input=MOCK_CONFIG_ADDITIONAL)
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result['data'][CONF_TOKEN] == MOCK_CREDS
|
||||
assert len(result['data']['devices']) == 1
|
||||
assert result['title'] == MOCK_TITLE
|
||||
|
||||
# Add New Entry
|
||||
entry = MockConfigEntry(domain=ps4.DOMAIN, data=MOCK_DATA)
|
||||
entry.add_to_manager(manager)
|
||||
|
||||
# Check that there are 2 entries
|
||||
assert len(manager.async_entries()) == 2
|
||||
|
||||
|
||||
async def test_no_devices_found_abort(hass):
|
||||
"""Test that failure to find devices aborts flow."""
|
||||
flow = ps4.PlayStation4FlowHandler()
|
||||
@@ -104,8 +251,8 @@ async def test_no_devices_found_abort(hass):
|
||||
|
||||
with patch('pyps4_homeassistant.Helper.has_devices', return_value=None):
|
||||
result = await flow.async_step_link(MOCK_CONFIG)
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result['reason'] == 'no_devices_found'
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result['reason'] == 'no_devices_found'
|
||||
|
||||
|
||||
async def test_credential_abort(hass):
|
||||
@@ -115,8 +262,8 @@ async def test_credential_abort(hass):
|
||||
|
||||
with patch('pyps4_homeassistant.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'
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result['reason'] == 'credential_error'
|
||||
|
||||
|
||||
async def test_invalid_pin_error(hass):
|
||||
@@ -129,9 +276,9 @@ async def test_invalid_pin_error(hass):
|
||||
patch('pyps4_homeassistant.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
|
||||
assert result['step_id'] == 'link'
|
||||
assert result['errors'] == {'base': 'login_failed'}
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'link'
|
||||
assert result['errors'] == {'base': 'login_failed'}
|
||||
|
||||
|
||||
async def test_device_connection_error(hass):
|
||||
@@ -144,6 +291,6 @@ async def test_device_connection_error(hass):
|
||||
patch('pyps4_homeassistant.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
|
||||
assert result['step_id'] == 'link'
|
||||
assert result['errors'] == {'base': 'not_ready'}
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'link'
|
||||
assert result['errors'] == {'base': 'not_ready'}
|
||||
|
||||
Reference in New Issue
Block a user