diff --git a/homeassistant/components/bond/cover.py b/homeassistant/components/bond/cover.py index e3bb30ad5d1..518c8b81f64 100644 --- a/homeassistant/components/bond/cover.py +++ b/homeassistant/components/bond/cover.py @@ -1,5 +1,4 @@ """Support for Bond covers.""" -import asyncio import logging from typing import Any, Callable, Dict, List, Optional @@ -23,19 +22,17 @@ async def async_setup_entry( async_add_entities: Callable[[List[Entity]], None], ) -> None: """Set up Bond cover devices.""" - bond: Bond = hass.data[DOMAIN][entry.entry_id] - async def discover(): - devices = await get_bond_devices(hass, bond) - covers = [ - BondCover(bond, device) - for device in devices - if device.type == BOND_DEVICE_TYPE_MOTORIZED_SHADES - ] - async_add_entities(covers) + devices = await hass.async_add_executor_job(get_bond_devices, hass, bond) - asyncio.create_task(discover()) + covers = [ + BondCover(bond, device) + for device in devices + if device.type == BOND_DEVICE_TYPE_MOTORIZED_SHADES + ] + + async_add_entities(covers) class BondCover(CoverEntity): diff --git a/homeassistant/components/bond/utils.py b/homeassistant/components/bond/utils.py index e352a9b7ffb..9f05252965b 100644 --- a/homeassistant/components/bond/utils.py +++ b/homeassistant/components/bond/utils.py @@ -26,13 +26,10 @@ class BondDevice: return self._attrs["type"] -async def get_bond_devices(hass: HomeAssistant, bond: Bond) -> List[BondDevice]: +def get_bond_devices(hass: HomeAssistant, bond: Bond) -> List[BondDevice]: """Fetch all available devices using Bond API.""" - device_ids = await hass.async_add_executor_job(bond.getDeviceIds) + device_ids = bond.getDeviceIds() devices = [ - BondDevice( - device_id, await hass.async_add_executor_job(bond.getDevice, device_id) - ) - for device_id in device_ids + BondDevice(device_id, bond.getDevice(device_id)) for device_id in device_ids ] return devices diff --git a/tests/components/bond/test_cover.py b/tests/components/bond/test_cover.py index 51f7f4f0999..579221de7a0 100644 --- a/tests/components/bond/test_cover.py +++ b/tests/components/bond/test_cover.py @@ -3,7 +3,6 @@ import logging from bond import BOND_DEVICE_TYPE_MOTORIZED_SHADES -from homeassistant.components.bond.utils import BondDevice from homeassistant.components.cover import DOMAIN as COVER_DOMAIN from homeassistant.const import ( ATTR_ENTITY_ID, @@ -19,19 +18,16 @@ from tests.async_mock import patch _LOGGER = logging.getLogger(__name__) +TEST_DEVICE_IDS = ["device-1"] +TEST_DEVICE = {"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES} + async def test_entity_registry(hass): """Tests that the devices are registered in the entity registry.""" with patch( - "homeassistant.components.bond.utils.get_bond_devices", - return_value=[ - BondDevice( - "device-1", - {"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES}, - ) - ], - ): + "homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS + ), patch("homeassistant.components.bond.Bond.getDevice", return_value=TEST_DEVICE): await setup_platform(hass, COVER_DOMAIN) registry: EntityRegistry = await hass.helpers.entity_registry.async_get_registry() @@ -42,17 +38,11 @@ async def test_open_cover(hass): """Tests that open cover command delegates to API.""" with patch( - "homeassistant.components.bond.utils.get_bond_devices", - return_value=[ - BondDevice( - "device-1", - {"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES}, - ) - ], - ): + "homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS + ), patch("homeassistant.components.bond.Bond.getDevice", return_value=TEST_DEVICE): await setup_platform(hass, COVER_DOMAIN) - with patch("bond.Bond.open") as mock_open: + with patch("homeassistant.components.bond.Bond.open") as mock_open: await hass.services.async_call( COVER_DOMAIN, SERVICE_OPEN_COVER, @@ -67,17 +57,11 @@ async def test_close_cover(hass): """Tests that close cover command delegates to API.""" with patch( - "homeassistant.components.bond.utils.get_bond_devices", - return_value=[ - BondDevice( - "device-1", - {"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES}, - ) - ], - ): + "homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS + ), patch("homeassistant.components.bond.Bond.getDevice", return_value=TEST_DEVICE): await setup_platform(hass, COVER_DOMAIN) - with patch("bond.Bond.close") as mock_close: + with patch("homeassistant.components.bond.Bond.close") as mock_close: await hass.services.async_call( COVER_DOMAIN, SERVICE_CLOSE_COVER, @@ -92,17 +76,11 @@ async def test_stop_cover(hass): """Tests that stop cover command delegates to API.""" with patch( - "homeassistant.components.bond.utils.get_bond_devices", - return_value=[ - BondDevice( - "device-1", - {"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES}, - ) - ], - ): + "homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS + ), patch("homeassistant.components.bond.Bond.getDevice", return_value=TEST_DEVICE): await setup_platform(hass, COVER_DOMAIN) - with patch("bond.Bond.hold") as mock_hold: + with patch("homeassistant.components.bond.Bond.hold") as mock_hold: await hass.services.async_call( COVER_DOMAIN, SERVICE_STOP_COVER, diff --git a/tests/components/bond/test_utils.py b/tests/components/bond/test_utils.py deleted file mode 100644 index 5e45d1609bf..00000000000 --- a/tests/components/bond/test_utils.py +++ /dev/null @@ -1,24 +0,0 @@ -"""Tests for the Bond cover device.""" -import logging - -from bond import Bond - -from homeassistant.components.bond.utils import get_bond_devices -from homeassistant.core import HomeAssistant - -from tests.async_mock import patch - -_LOGGER = logging.getLogger(__name__) - - -async def test_get_bond_devices(hass: HomeAssistant): - """Tests that the querying for devices delegates to API.""" - bond: Bond = Bond("1.1.1.1", "test-token") - - with patch( - "bond.Bond.getDeviceIds", return_value=["device-1", "device-2"], - ) as mock_get_device_ids, patch("bond.Bond.getDevice") as mock_get_device: - await get_bond_devices(hass, bond) - - assert len(mock_get_device_ids.mock_calls) == 1 - assert len(mock_get_device.mock_calls) == 2