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:
1 parent
0fbdb47dcf
commit
1e9bc278e0
6 files changed
+85
-160
No files matched your search
@@ -1,4 +1,7 @@
|
||||
"""Common methods used across tests for Bond."""
|
||||
from typing import Any, Dict
|
||||
|
||||
from homeassistant import core
|
||||
from homeassistant.components.bond.const import DOMAIN as BOND_DOMAIN
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
|
||||
from homeassistant.setup import async_setup_component
|
||||
@@ -7,7 +10,9 @@ from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def setup_platform(hass, platform):
|
||||
async def setup_platform(
|
||||
hass: core.HomeAssistant, platform: str, discovered_device: Dict[str, Any]
|
||||
):
|
||||
"""Set up the specified Bond platform."""
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=BOND_DOMAIN,
|
||||
@@ -15,8 +20,15 @@ async def setup_platform(hass, platform):
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
with patch("homeassistant.components.bond.PLATFORMS", [platform]):
|
||||
with patch("homeassistant.components.bond.PLATFORMS", [platform]), patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceIds",
|
||||
return_value=["bond-device-id"],
|
||||
), patch(
|
||||
"homeassistant.components.bond.Bond.getDevice", return_value=discovered_device
|
||||
), patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
|
||||
):
|
||||
assert await async_setup_component(hass, BOND_DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return mock_entry
|
||||
@@ -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}
|
||||
|
||||
@@ -15,25 +15,19 @@ from .common import setup_platform
|
||||
|
||||
from tests.async_mock import patch
|
||||
|
||||
TEST_DEVICE_IDS = ["device-1"]
|
||||
TEST_FAN_DEVICE = {
|
||||
"name": "name-1",
|
||||
"type": BOND_DEVICE_TYPE_CEILING_FAN,
|
||||
"actions": ["SetSpeed"],
|
||||
}
|
||||
|
||||
def ceiling_fan(name: str):
|
||||
"""Create a ceiling fan with given name."""
|
||||
return {
|
||||
"name": name,
|
||||
"type": BOND_DEVICE_TYPE_CEILING_FAN,
|
||||
"actions": ["SetSpeed"],
|
||||
}
|
||||
|
||||
|
||||
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_FAN_DEVICE
|
||||
), patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
|
||||
):
|
||||
await setup_platform(hass, FAN_DOMAIN)
|
||||
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
||||
|
||||
registry: EntityRegistry = await hass.helpers.entity_registry.async_get_registry()
|
||||
assert [key for key in registry.entities.keys()] == ["fan.name_1"]
|
||||
@@ -41,15 +35,7 @@ async def test_entity_registry(hass: core.HomeAssistant):
|
||||
|
||||
async def test_turn_on_fan(hass: core.HomeAssistant):
|
||||
"""Tests that turn on 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_FAN_DEVICE
|
||||
), patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
|
||||
):
|
||||
await setup_platform(hass, FAN_DOMAIN)
|
||||
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"
|
||||
@@ -68,15 +54,7 @@ async def test_turn_on_fan(hass: core.HomeAssistant):
|
||||
|
||||
async def test_turn_off_fan(hass: core.HomeAssistant):
|
||||
"""Tests that turn off 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_FAN_DEVICE
|
||||
), patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
|
||||
):
|
||||
await setup_platform(hass, FAN_DOMAIN)
|
||||
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
||||
|
||||
with patch("homeassistant.components.bond.Bond.turnOff") as mock_turn_off:
|
||||
await hass.services.async_call(
|
||||
@@ -88,15 +66,7 @@ async def test_turn_off_fan(hass: core.HomeAssistant):
|
||||
|
||||
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."""
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
|
||||
), patch(
|
||||
"homeassistant.components.bond.Bond.getDevice", return_value=TEST_FAN_DEVICE
|
||||
), patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
|
||||
):
|
||||
await setup_platform(hass, FAN_DOMAIN)
|
||||
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceState",
|
||||
@@ -110,15 +80,7 @@ async def test_update_reports_fan_on(hass: core.HomeAssistant):
|
||||
|
||||
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."""
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
|
||||
), patch(
|
||||
"homeassistant.components.bond.Bond.getDevice", return_value=TEST_FAN_DEVICE
|
||||
), patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceState", return_value={}
|
||||
):
|
||||
await setup_platform(hass, FAN_DOMAIN)
|
||||
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceState",
|
||||
|
||||
Reference in new issue
Block a user