Refactor Bond integration to remove duplication (#37740)

* Refactor Bond integration to remove duplication in Entity code and unit tests

* Refactor Bond integration to remove duplication in Entity code and unit tests (PR feedback)
This commit is contained in:
Eugene Prystupa authored and GitHub committed 2020-07-10 20:20:50 -05:00
1 parent 0fbdb47dcf
commit 1e9bc278e0
6 files changed
+85 -160

No files matched your search

+10 -56
View File
@@ -22,21 +22,15 @@ from tests.common import async_fire_time_changed
_LOGGER = logging.getLogger(__name__)
TEST_DEVICE_IDS = ["device-1"]
TEST_COVER_DEVICE = {"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES}
def shades(name: str):
"""Create motorized shades with given name."""
return {"name": name, "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES}
async def test_entity_registry(hass: core.HomeAssistant):
"""Tests that the devices are registered in the entity registry."""
with patch(
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
), patch(
"homeassistant.components.bond.Bond.getDevice", return_value=TEST_COVER_DEVICE
), patch(
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
):
await setup_platform(hass, COVER_DOMAIN)
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
registry: EntityRegistry = await hass.helpers.entity_registry.async_get_registry()
assert [key for key in registry.entities.keys()] == ["cover.name_1"]
@@ -44,15 +38,7 @@ async def test_entity_registry(hass: core.HomeAssistant):
async def test_open_cover(hass: core.HomeAssistant):
"""Tests that open cover command delegates to API."""
with patch(
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
), patch(
"homeassistant.components.bond.Bond.getDevice", return_value=TEST_COVER_DEVICE
), patch(
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
):
await setup_platform(hass, COVER_DOMAIN)
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
with patch("homeassistant.components.bond.Bond.open") as mock_open:
await hass.services.async_call(
@@ -67,15 +53,7 @@ async def test_open_cover(hass: core.HomeAssistant):
async def test_close_cover(hass: core.HomeAssistant):
"""Tests that close cover command delegates to API."""
with patch(
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
), patch(
"homeassistant.components.bond.Bond.getDevice", return_value=TEST_COVER_DEVICE
), patch(
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
):
await setup_platform(hass, COVER_DOMAIN)
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
with patch("homeassistant.components.bond.Bond.close") as mock_close:
await hass.services.async_call(
@@ -90,15 +68,7 @@ async def test_close_cover(hass: core.HomeAssistant):
async def test_stop_cover(hass: core.HomeAssistant):
"""Tests that stop cover command delegates to API."""
with patch(
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
), patch(
"homeassistant.components.bond.Bond.getDevice", return_value=TEST_COVER_DEVICE
), patch(
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
):
await setup_platform(hass, COVER_DOMAIN)
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
with patch("homeassistant.components.bond.Bond.hold") as mock_hold:
await hass.services.async_call(
@@ -113,15 +83,7 @@ async def test_stop_cover(hass: core.HomeAssistant):
async def test_update_reports_open_cover(hass: core.HomeAssistant):
"""Tests that update command sets correct state when Bond API reports cover is open."""
with patch(
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
), patch(
"homeassistant.components.bond.Bond.getDevice", return_value=TEST_COVER_DEVICE
), patch(
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
):
await setup_platform(hass, COVER_DOMAIN)
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
with patch(
"homeassistant.components.bond.Bond.getDeviceState", return_value={"open": 1}
@@ -134,15 +96,7 @@ async def test_update_reports_open_cover(hass: core.HomeAssistant):
async def test_update_reports_closed_cover(hass: core.HomeAssistant):
"""Tests that update command sets correct state when Bond API reports cover is closed."""
with patch(
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
), patch(
"homeassistant.components.bond.Bond.getDevice", return_value=TEST_COVER_DEVICE
), patch(
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
):
await setup_platform(hass, COVER_DOMAIN)
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
with patch(
"homeassistant.components.bond.Bond.getDeviceState", return_value={"open": 0}