From 0bfcd8c2ab4aa50d9b5ac927199951bc787afc48 Mon Sep 17 00:00:00 2001 From: Eugene Prystupa Date: Wed, 15 Jul 2020 16:49:58 -0400 Subject: [PATCH] Refactor bond tests (#37868) --- tests/components/bond/common.py | 64 +++++++++++++++++++++++++++- tests/components/bond/test_cover.py | 23 +++++----- tests/components/bond/test_fan.py | 63 +++++++++++---------------- tests/components/bond/test_init.py | 25 ++++++----- tests/components/bond/test_light.py | 36 +++++++--------- tests/components/bond/test_switch.py | 20 ++++----- 6 files changed, 139 insertions(+), 92 deletions(-) diff --git a/tests/components/bond/common.py b/tests/components/bond/common.py index 2a3f727f8bd..2aebc3aa2ac 100644 --- a/tests/components/bond/common.py +++ b/tests/components/bond/common.py @@ -51,8 +51,8 @@ async def setup_platform( return_value=[bond_device_id], ), patch( "homeassistant.components.bond.Bond.getDevice", return_value=discovered_device - ), patch( - "homeassistant.components.bond.Bond.getDeviceState", return_value={} + ), patch_bond_device_state( + return_value={} ), patch( "homeassistant.components.bond.Bond.getProperties", return_value=props ): @@ -60,3 +60,63 @@ async def setup_platform( await hass.async_block_till_done() return mock_entry + + +def patch_bond_turn_on(): + """Patch Bond API turnOn command.""" + return patch("homeassistant.components.bond.Bond.turnOn") + + +def patch_bond_turn_off(): + """Patch Bond API turnOff command.""" + return patch("homeassistant.components.bond.Bond.turnOff") + + +def patch_bond_set_speed(): + """Patch Bond API setSpeed command.""" + return patch("homeassistant.components.bond.Bond.setSpeed") + + +def patch_bond_set_flame(): + """Patch Bond API setFlame command.""" + return patch("homeassistant.components.bond.Bond.setFlame") + + +def patch_bond_open(): + """Patch Bond API open command.""" + return patch("homeassistant.components.bond.Bond.open") + + +def patch_bond_close(): + """Patch Bond API close command.""" + return patch("homeassistant.components.bond.Bond.close") + + +def patch_bond_hold(): + """Patch Bond API hold command.""" + return patch("homeassistant.components.bond.Bond.hold") + + +def patch_bond_set_direction(): + """Patch Bond API setDirection command.""" + return patch("homeassistant.components.bond.Bond.setDirection") + + +def patch_turn_light_on(): + """Patch Bond API turnLightOn command.""" + return patch("homeassistant.components.bond.Bond.turnLightOn") + + +def patch_turn_light_off(): + """Patch Bond API turnLightOff command.""" + return patch("homeassistant.components.bond.Bond.turnLightOff") + + +def patch_bond_device_state(return_value=None): + """Patch Bond API getDeviceState command.""" + if return_value is None: + return_value = {} + + return patch( + "homeassistant.components.bond.Bond.getDeviceState", return_value=return_value + ) diff --git a/tests/components/bond/test_cover.py b/tests/components/bond/test_cover.py index 9e1f87e8c0d..dacb612add9 100644 --- a/tests/components/bond/test_cover.py +++ b/tests/components/bond/test_cover.py @@ -15,9 +15,14 @@ from homeassistant.const import ( from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.util import utcnow -from .common import setup_platform +from .common import ( + patch_bond_close, + patch_bond_device_state, + patch_bond_hold, + patch_bond_open, + setup_platform, +) -from tests.async_mock import patch from tests.common import async_fire_time_changed _LOGGER = logging.getLogger(__name__) @@ -40,7 +45,7 @@ async def test_open_cover(hass: core.HomeAssistant): """Tests that open cover command delegates to API.""" await setup_platform(hass, COVER_DOMAIN, shades("name-1")) - with patch("homeassistant.components.bond.Bond.open") as mock_open: + with patch_bond_open() as mock_open, patch_bond_device_state(): await hass.services.async_call( COVER_DOMAIN, SERVICE_OPEN_COVER, @@ -55,7 +60,7 @@ async def test_close_cover(hass: core.HomeAssistant): """Tests that close cover command delegates to API.""" await setup_platform(hass, COVER_DOMAIN, shades("name-1")) - with patch("homeassistant.components.bond.Bond.close") as mock_close: + with patch_bond_close() as mock_close, patch_bond_device_state(): await hass.services.async_call( COVER_DOMAIN, SERVICE_CLOSE_COVER, @@ -70,7 +75,7 @@ async def test_stop_cover(hass: core.HomeAssistant): """Tests that stop cover command delegates to API.""" await setup_platform(hass, COVER_DOMAIN, shades("name-1")) - with patch("homeassistant.components.bond.Bond.hold") as mock_hold: + with patch_bond_hold() as mock_hold, patch_bond_device_state(): await hass.services.async_call( COVER_DOMAIN, SERVICE_STOP_COVER, @@ -85,9 +90,7 @@ async def test_update_reports_open_cover(hass: core.HomeAssistant): """Tests that update command sets correct state when Bond API reports cover is open.""" await setup_platform(hass, COVER_DOMAIN, shades("name-1")) - with patch( - "homeassistant.components.bond.Bond.getDeviceState", return_value={"open": 1} - ): + with patch_bond_device_state(return_value={"open": 1}): async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() @@ -98,9 +101,7 @@ async def test_update_reports_closed_cover(hass: core.HomeAssistant): """Tests that update command sets correct state when Bond API reports cover is closed.""" await setup_platform(hass, COVER_DOMAIN, shades("name-1")) - with patch( - "homeassistant.components.bond.Bond.getDeviceState", return_value={"open": 0} - ): + with patch_bond_device_state(return_value={"open": 0}): async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() diff --git a/tests/components/bond/test_fan.py b/tests/components/bond/test_fan.py index 0c2df04e2a9..b518f72f326 100644 --- a/tests/components/bond/test_fan.py +++ b/tests/components/bond/test_fan.py @@ -17,9 +17,15 @@ from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_O from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.util import utcnow -from .common import setup_platform +from .common import ( + patch_bond_device_state, + patch_bond_set_direction, + patch_bond_set_speed, + patch_bond_turn_off, + patch_bond_turn_on, + setup_platform, +) -from tests.async_mock import patch from tests.common import async_fire_time_changed @@ -69,32 +75,25 @@ async def test_entity_non_standard_speed_list(hass: core.HomeAssistant): fan.SPEED_HIGH, ] - with patch("homeassistant.components.bond.Bond.turnOn"), patch( - "homeassistant.components.bond.Bond.setSpeed" - ) as mock_set_speed_low: - await turn_fan_on(hass, "fan.name_1", fan.SPEED_LOW) - mock_set_speed_low.assert_called_once_with("test-device-id", speed=1) + with patch_bond_device_state(): + with patch_bond_turn_on(), patch_bond_set_speed() as mock_set_speed_low: + await turn_fan_on(hass, "fan.name_1", fan.SPEED_LOW) + mock_set_speed_low.assert_called_once_with("test-device-id", speed=1) - with patch("homeassistant.components.bond.Bond.turnOn"), patch( - "homeassistant.components.bond.Bond.setSpeed" - ) as mock_set_speed_medium: - await turn_fan_on(hass, "fan.name_1", fan.SPEED_MEDIUM) - mock_set_speed_medium.assert_called_once_with("test-device-id", speed=3) + with patch_bond_turn_on(), patch_bond_set_speed() as mock_set_speed_medium: + await turn_fan_on(hass, "fan.name_1", fan.SPEED_MEDIUM) + mock_set_speed_medium.assert_called_once_with("test-device-id", speed=3) - with patch("homeassistant.components.bond.Bond.turnOn"), patch( - "homeassistant.components.bond.Bond.setSpeed" - ) as mock_set_speed_high: - await turn_fan_on(hass, "fan.name_1", fan.SPEED_HIGH) - mock_set_speed_high.assert_called_once_with("test-device-id", speed=6) + with patch_bond_turn_on(), patch_bond_set_speed() as mock_set_speed_high: + await turn_fan_on(hass, "fan.name_1", fan.SPEED_HIGH) + mock_set_speed_high.assert_called_once_with("test-device-id", speed=6) async def test_turn_on_fan(hass: core.HomeAssistant): """Tests that turn on command delegates to API.""" await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1")) - with patch("homeassistant.components.bond.Bond.turnOn") as mock_turn_on, patch( - "homeassistant.components.bond.Bond.setSpeed" - ) as mock_set_speed: + with patch_bond_turn_on() as mock_turn_on, patch_bond_set_speed() as mock_set_speed, patch_bond_device_state(): await turn_fan_on(hass, "fan.name_1", fan.SPEED_LOW) mock_set_speed.assert_called_once() @@ -105,7 +104,7 @@ async def test_turn_off_fan(hass: core.HomeAssistant): """Tests that turn off command delegates to API.""" await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1")) - with patch("homeassistant.components.bond.Bond.turnOff") as mock_turn_off: + with patch_bond_turn_off() as mock_turn_off, patch_bond_device_state(): await hass.services.async_call( FAN_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "fan.name_1"}, blocking=True, ) @@ -118,10 +117,7 @@ async def test_update_reports_fan_on(hass: core.HomeAssistant): """Tests that update command sets correct state when Bond API reports fan power is on.""" await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1")) - with patch( - "homeassistant.components.bond.Bond.getDeviceState", - return_value={"power": 1, "speed": 1}, - ): + with patch_bond_device_state(return_value={"power": 1, "speed": 1}): async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() @@ -132,10 +128,7 @@ async def test_update_reports_fan_off(hass: core.HomeAssistant): """Tests that update command sets correct state when Bond API reports fan power is off.""" await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1")) - with patch( - "homeassistant.components.bond.Bond.getDeviceState", - return_value={"power": 0, "speed": 1}, - ): + with patch_bond_device_state(return_value={"power": 0, "speed": 1}): async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() @@ -146,10 +139,7 @@ async def test_update_reports_direction_forward(hass: core.HomeAssistant): """Tests that update command sets correct direction when Bond API reports fan direction is forward.""" await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1")) - with patch( - "homeassistant.components.bond.Bond.getDeviceState", - return_value={"direction": Directions.FORWARD}, - ): + with patch_bond_device_state(return_value={"direction": Directions.FORWARD}): async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() @@ -160,10 +150,7 @@ async def test_update_reports_direction_reverse(hass: core.HomeAssistant): """Tests that update command sets correct direction when Bond API reports fan direction is reverse.""" await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1")) - with patch( - "homeassistant.components.bond.Bond.getDeviceState", - return_value={"direction": Directions.REVERSE}, - ): + with patch_bond_device_state(return_value={"direction": Directions.REVERSE}): async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() @@ -174,7 +161,7 @@ async def test_set_fan_direction(hass: core.HomeAssistant): """Tests that set direction command delegates to API.""" await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1")) - with patch("homeassistant.components.bond.Bond.setDirection") as mock_set_direction: + with patch_bond_set_direction() as mock_set_direction, patch_bond_device_state(): await hass.services.async_call( FAN_DOMAIN, SERVICE_SET_DIRECTION, diff --git a/tests/components/bond/test_init.py b/tests/components/bond/test_init.py index 23c11199879..7f6250abd37 100644 --- a/tests/components/bond/test_init.py +++ b/tests/components/bond/test_init.py @@ -12,6 +12,11 @@ from tests.async_mock import patch from tests.common import MockConfigEntry +def patch_setup_entry(domain: str): + """Patch async_setup_entry for specified domain.""" + return patch(f"homeassistant.components.bond.{domain}.async_setup_entry") + + async def test_async_setup_no_domain_config(hass: HomeAssistant): """Test setup without configuration is noop.""" result = await async_setup_component(hass, DOMAIN, {}) @@ -25,14 +30,12 @@ async def test_async_setup_entry_sets_up_hub_and_supported_domains(hass: HomeAss domain=DOMAIN, data={CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"}, ) - with patch( - "homeassistant.components.bond.cover.async_setup_entry" - ) as mock_cover_async_setup_entry, patch( - "homeassistant.components.bond.fan.async_setup_entry" - ) as mock_fan_async_setup_entry, patch( - "homeassistant.components.bond.light.async_setup_entry" - ) as mock_light_async_setup_entry, patch( - "homeassistant.components.bond.switch.async_setup_entry" + with patch_setup_entry("cover") as mock_cover_async_setup_entry, patch_setup_entry( + "fan" + ) as mock_fan_async_setup_entry, patch_setup_entry( + "light" + ) as mock_light_async_setup_entry, patch_setup_entry( + "switch" ) as mock_switch_async_setup_entry: result = await setup_bond_entity( hass, @@ -72,9 +75,9 @@ async def test_unload_config_entry(hass: HomeAssistant): domain=DOMAIN, data={CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"}, ) - with patch("homeassistant.components.bond.cover.async_setup_entry"), patch( - "homeassistant.components.bond.fan.async_setup_entry" - ): + with patch_setup_entry("cover"), patch_setup_entry("fan"), patch_setup_entry( + "light" + ), patch_setup_entry("switch"): result = await setup_bond_entity(hass, config_entry) assert result is True await hass.async_block_till_done() diff --git a/tests/components/bond/test_light.py b/tests/components/bond/test_light.py index fc9edf64727..edcdc3e63bd 100644 --- a/tests/components/bond/test_light.py +++ b/tests/components/bond/test_light.py @@ -10,9 +10,16 @@ from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_O from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.util import utcnow -from .common import setup_platform +from .common import ( + patch_bond_device_state, + patch_bond_set_flame, + patch_bond_turn_off, + patch_bond_turn_on, + patch_turn_light_off, + patch_turn_light_on, + setup_platform, +) -from tests.async_mock import patch from tests.common import async_fire_time_changed _LOGGER = logging.getLogger(__name__) @@ -44,7 +51,7 @@ async def test_turn_on_light(hass: core.HomeAssistant): """Tests that turn on command delegates to API.""" await setup_platform(hass, LIGHT_DOMAIN, ceiling_fan("name-1")) - with patch("homeassistant.components.bond.Bond.turnLightOn") as mock_turn_light_on: + with patch_turn_light_on() as mock_turn_light_on, patch_bond_device_state(): await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, @@ -59,9 +66,7 @@ async def test_turn_off_light(hass: core.HomeAssistant): """Tests that turn off command delegates to API.""" await setup_platform(hass, LIGHT_DOMAIN, ceiling_fan("name-1")) - with patch( - "homeassistant.components.bond.Bond.turnLightOff" - ) as mock_turn_light_off: + with patch_turn_light_off() as mock_turn_light_off, patch_bond_device_state(): await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_OFF, @@ -76,9 +81,7 @@ async def test_update_reports_light_is_on(hass: core.HomeAssistant): """Tests that update command sets correct state when Bond API reports the light is on.""" await setup_platform(hass, LIGHT_DOMAIN, ceiling_fan("name-1")) - with patch( - "homeassistant.components.bond.Bond.getDeviceState", return_value={"light": 1} - ): + with patch_bond_device_state(return_value={"light": 1}): async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() @@ -89,9 +92,7 @@ async def test_update_reports_light_is_off(hass: core.HomeAssistant): """Tests that update command sets correct state when Bond API reports the light is off.""" await setup_platform(hass, LIGHT_DOMAIN, ceiling_fan("name-1")) - with patch( - "homeassistant.components.bond.Bond.getDeviceState", return_value={"light": 0} - ): + with patch_bond_device_state(return_value={"light": 0}): async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() @@ -104,9 +105,7 @@ async def test_turn_on_fireplace(hass: core.HomeAssistant): hass, LIGHT_DOMAIN, fireplace("name-1"), bond_device_id="test-device-id" ) - with patch("homeassistant.components.bond.Bond.turnOn") as mock_turn_on, patch( - "homeassistant.components.bond.Bond.setFlame" - ) as mock_set_flame: + with patch_bond_turn_on() as mock_turn_on, patch_bond_set_flame() as mock_set_flame, patch_bond_device_state(): await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, @@ -123,7 +122,7 @@ async def test_turn_off_fireplace(hass: core.HomeAssistant): """Tests that turn off command delegates to API.""" await setup_platform(hass, LIGHT_DOMAIN, fireplace("name-1")) - with patch("homeassistant.components.bond.Bond.turnOff") as mock_turn_off: + with patch_bond_turn_off() as mock_turn_off, patch_bond_device_state(): await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_OFF, @@ -138,10 +137,7 @@ async def test_flame_converted_to_brightness(hass: core.HomeAssistant): """Tests that reported flame level (0..100) converted to HA brightness (0...255).""" await setup_platform(hass, LIGHT_DOMAIN, fireplace("name-1")) - with patch( - "homeassistant.components.bond.Bond.getDeviceState", - return_value={"power": 1, "flame": 50}, - ): + with patch_bond_device_state(return_value={"power": 1, "flame": 50}): async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() diff --git a/tests/components/bond/test_switch.py b/tests/components/bond/test_switch.py index 121bb505cdb..b2d77150907 100644 --- a/tests/components/bond/test_switch.py +++ b/tests/components/bond/test_switch.py @@ -10,9 +10,13 @@ from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_O from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.util import utcnow -from .common import setup_platform +from .common import ( + patch_bond_device_state, + patch_bond_turn_off, + patch_bond_turn_on, + setup_platform, +) -from tests.async_mock import patch from tests.common import async_fire_time_changed _LOGGER = logging.getLogger(__name__) @@ -35,7 +39,7 @@ async def test_turn_on_switch(hass: core.HomeAssistant): """Tests that turn on command delegates to API.""" await setup_platform(hass, SWITCH_DOMAIN, generic_device("name-1")) - with patch("homeassistant.components.bond.Bond.turnOn") as mock_turn_on: + with patch_bond_turn_on() as mock_turn_on, patch_bond_device_state(): await hass.services.async_call( SWITCH_DOMAIN, SERVICE_TURN_ON, @@ -50,7 +54,7 @@ async def test_turn_off_switch(hass: core.HomeAssistant): """Tests that turn off command delegates to API.""" await setup_platform(hass, SWITCH_DOMAIN, generic_device("name-1")) - with patch("homeassistant.components.bond.Bond.turnOff") as mock_turn_off: + with patch_bond_turn_off() as mock_turn_off, patch_bond_device_state(): await hass.services.async_call( SWITCH_DOMAIN, SERVICE_TURN_OFF, @@ -65,9 +69,7 @@ async def test_update_reports_switch_is_on(hass: core.HomeAssistant): """Tests that update command sets correct state when Bond API reports the device is on.""" await setup_platform(hass, SWITCH_DOMAIN, generic_device("name-1")) - with patch( - "homeassistant.components.bond.Bond.getDeviceState", return_value={"power": 1} - ): + with patch_bond_device_state(return_value={"power": 1}): async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() @@ -78,9 +80,7 @@ async def test_update_reports_switch_is_off(hass: core.HomeAssistant): """Tests that update command sets correct state when Bond API reports the device is off.""" await setup_platform(hass, SWITCH_DOMAIN, generic_device("name-1")) - with patch( - "homeassistant.components.bond.Bond.getDeviceState", return_value={"power": 0} - ): + with patch_bond_device_state(return_value={"power": 0}): async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done()