Improve tests for Broadlink devices (#39898)

pull/39960/head
Felipe Martins Diel 2020-09-11 21:57:07 -03:00 committed by GitHub
parent ac2e290d97
commit 9f08955fef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 80 deletions

View File

@ -20,16 +20,13 @@ from tests.common import mock_device_registry, mock_registry
async def test_device_setup(hass):
"""Test a successful setup."""
device = get_device("Office")
mock_api = device.get_mock_api()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
with patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward, patch.object(
hass.config_entries.flow, "async_init"
) as mock_init:
await hass.config_entries.async_setup(mock_entry.entry_id)
mock_api, mock_entry = await device.setup_entry(hass)
assert mock_entry.state == ENTRY_STATE_LOADED
assert mock_api.auth.call_count == 1
@ -46,15 +43,13 @@ async def test_device_setup_authentication_error(hass):
device = get_device("Living Room")
mock_api = device.get_mock_api()
mock_api.auth.side_effect = blke.AuthenticationError()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
with patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward, patch.object(
hass.config_entries.flow, "async_init"
) as mock_init:
await hass.config_entries.async_setup(mock_entry.entry_id)
mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
assert mock_entry.state == ENTRY_STATE_SETUP_ERROR
assert mock_api.auth.call_count == 1
@ -72,15 +67,13 @@ async def test_device_setup_device_offline(hass):
device = get_device("Office")
mock_api = device.get_mock_api()
mock_api.auth.side_effect = blke.DeviceOfflineError()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
with patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward, patch.object(
hass.config_entries.flow, "async_init"
) as mock_init:
await hass.config_entries.async_setup(mock_entry.entry_id)
mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
assert mock_entry.state == ENTRY_STATE_SETUP_RETRY
assert mock_api.auth.call_count == 1
@ -93,15 +86,13 @@ async def test_device_setup_os_error(hass):
device = get_device("Office")
mock_api = device.get_mock_api()
mock_api.auth.side_effect = OSError()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
with patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward, patch.object(
hass.config_entries.flow, "async_init"
) as mock_init:
await hass.config_entries.async_setup(mock_entry.entry_id)
mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
assert mock_entry.state == ENTRY_STATE_SETUP_RETRY
assert mock_api.auth.call_count == 1
@ -114,15 +105,13 @@ async def test_device_setup_broadlink_exception(hass):
device = get_device("Office")
mock_api = device.get_mock_api()
mock_api.auth.side_effect = blke.BroadlinkException()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
with patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward, patch.object(
hass.config_entries.flow, "async_init"
) as mock_init:
await hass.config_entries.async_setup(mock_entry.entry_id)
mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
assert mock_entry.state == ENTRY_STATE_SETUP_ERROR
assert mock_api.auth.call_count == 1
@ -135,15 +124,13 @@ async def test_device_setup_update_device_offline(hass):
device = get_device("Office")
mock_api = device.get_mock_api()
mock_api.check_sensors.side_effect = blke.DeviceOfflineError()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
with patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward, patch.object(
hass.config_entries.flow, "async_init"
) as mock_init:
await hass.config_entries.async_setup(mock_entry.entry_id)
mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
assert mock_entry.state == ENTRY_STATE_SETUP_RETRY
assert mock_api.auth.call_count == 1
@ -157,15 +144,13 @@ async def test_device_setup_update_authorization_error(hass):
device = get_device("Office")
mock_api = device.get_mock_api()
mock_api.check_sensors.side_effect = (blke.AuthorizationError(), None)
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
with patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward, patch.object(
hass.config_entries.flow, "async_init"
) as mock_init:
await hass.config_entries.async_setup(mock_entry.entry_id)
mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
assert mock_entry.state == ENTRY_STATE_LOADED
assert mock_api.auth.call_count == 2
@ -183,15 +168,13 @@ async def test_device_setup_update_authentication_error(hass):
mock_api = device.get_mock_api()
mock_api.check_sensors.side_effect = blke.AuthorizationError()
mock_api.auth.side_effect = (None, blke.AuthenticationError())
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
with patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward, patch.object(
hass.config_entries.flow, "async_init"
) as mock_init:
await hass.config_entries.async_setup(mock_entry.entry_id)
mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
assert mock_entry.state == ENTRY_STATE_SETUP_RETRY
assert mock_api.auth.call_count == 2
@ -210,15 +193,13 @@ async def test_device_setup_update_broadlink_exception(hass):
device = get_device("Living Room")
mock_api = device.get_mock_api()
mock_api.check_sensors.side_effect = blke.BroadlinkException()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
with patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward, patch.object(
hass.config_entries.flow, "async_init"
) as mock_init:
await hass.config_entries.async_setup(mock_entry.entry_id)
mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
assert mock_entry.state == ENTRY_STATE_SETUP_RETRY
assert mock_api.auth.call_count == 1
@ -232,13 +213,9 @@ async def test_device_setup_get_fwversion_broadlink_exception(hass):
device = get_device("Office")
mock_api = device.get_mock_api()
mock_api.get_fwversion.side_effect = blke.BroadlinkException()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward:
await hass.config_entries.async_setup(mock_entry.entry_id)
with patch.object(hass.config_entries, "async_forward_entry_setup") as mock_forward:
mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
assert mock_entry.state == ENTRY_STATE_LOADED
forward_entries = {c[1][1] for c in mock_forward.mock_calls}
@ -252,13 +229,9 @@ async def test_device_setup_get_fwversion_os_error(hass):
device = get_device("Office")
mock_api = device.get_mock_api()
mock_api.get_fwversion.side_effect = OSError()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward:
await hass.config_entries.async_setup(mock_entry.entry_id)
with patch.object(hass.config_entries, "async_forward_entry_setup") as mock_forward:
_, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
assert mock_entry.state == ENTRY_STATE_LOADED
forward_entries = {c[1][1] for c in mock_forward.mock_calls}
@ -270,16 +243,12 @@ async def test_device_setup_get_fwversion_os_error(hass):
async def test_device_setup_registry(hass):
"""Test we register the device and the entries correctly."""
device = get_device("Office")
mock_api = device.get_mock_api()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
device_registry = mock_device_registry(hass)
entity_registry = mock_registry(hass)
with patch("broadlink.gendevice", return_value=mock_api):
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
_, mock_entry = await device.setup_entry(hass)
await hass.async_block_till_done()
assert len(device_registry.devices) == 1
@ -299,14 +268,9 @@ async def test_device_setup_registry(hass):
async def test_device_unload_works(hass):
"""Test we unload the device."""
device = get_device("Office")
mock_api = device.get_mock_api()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
hass.config_entries, "async_forward_entry_setup"
):
await hass.config_entries.async_setup(mock_entry.entry_id)
with patch.object(hass.config_entries, "async_forward_entry_setup"):
mock_api, mock_entry = await device.setup_entry(hass)
with patch.object(
hass.config_entries, "async_forward_entry_unload", return_value=True
@ -325,13 +289,11 @@ async def test_device_unload_authentication_error(hass):
device = get_device("Living Room")
mock_api = device.get_mock_api()
mock_api.auth.side_effect = blke.AuthenticationError()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
hass.config_entries, "async_forward_entry_setup"
), patch.object(hass.config_entries.flow, "async_init"):
await hass.config_entries.async_setup(mock_entry.entry_id)
with patch.object(hass.config_entries, "async_forward_entry_setup"), patch.object(
hass.config_entries.flow, "async_init"
):
_, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
with patch.object(
hass.config_entries, "async_forward_entry_unload", return_value=True
@ -347,13 +309,9 @@ async def test_device_unload_update_failed(hass):
device = get_device("Office")
mock_api = device.get_mock_api()
mock_api.check_sensors.side_effect = blke.DeviceOfflineError()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
with patch("broadlink.gendevice", return_value=mock_api), patch.object(
hass.config_entries, "async_forward_entry_setup"
):
await hass.config_entries.async_setup(mock_entry.entry_id)
with patch.object(hass.config_entries, "async_forward_entry_setup"):
_, mock_entry = await device.setup_entry(hass, mock_api=mock_api)
with patch.object(
hass.config_entries, "async_forward_entry_unload", return_value=True
@ -367,17 +325,16 @@ async def test_device_unload_update_failed(hass):
async def test_device_update_listener(hass):
"""Test we update device and entity registry when the entry is renamed."""
device = get_device("Office")
mock_api = device.get_mock_api()
mock_entry = device.get_mock_entry()
mock_entry.add_to_hass(hass)
device_registry = mock_device_registry(hass)
entity_registry = mock_registry(hass)
with patch("broadlink.gendevice", return_value=mock_api):
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
mock_api, mock_entry = await device.setup_entry(hass)
await hass.async_block_till_done()
with patch(
"homeassistant.components.broadlink.device.blk.gendevice", return_value=mock_api
):
hass.config_entries.async_update_entry(mock_entry, title="New Name")
await hass.async_block_till_done()