diff --git a/homeassistant/components/bond/cover.py b/homeassistant/components/bond/cover.py index 79ccfa9210e..809bf3d7da5 100644 --- a/homeassistant/components/bond/cover.py +++ b/homeassistant/components/bond/cover.py @@ -21,11 +21,9 @@ async def async_setup_entry( """Set up Bond cover devices.""" hub: BondHub = hass.data[DOMAIN][entry.entry_id] - devices = await hass.async_add_executor_job(hub.get_bond_devices) - covers = [ BondCover(hub, device) - for device in devices + for device in hub.devices if device.type == DeviceTypes.MOTORIZED_SHADES ] diff --git a/homeassistant/components/bond/fan.py b/homeassistant/components/bond/fan.py index 0d7013b4ccf..80ae5d7f6ac 100644 --- a/homeassistant/components/bond/fan.py +++ b/homeassistant/components/bond/fan.py @@ -32,11 +32,9 @@ async def async_setup_entry( """Set up Bond fan devices.""" hub: BondHub = hass.data[DOMAIN][entry.entry_id] - devices = await hass.async_add_executor_job(hub.get_bond_devices) - fans = [ BondFan(hub, device) - for device in devices + for device in hub.devices if device.type == DeviceTypes.CEILING_FAN ] diff --git a/homeassistant/components/bond/light.py b/homeassistant/components/bond/light.py index 949c5a54070..9a3e49952aa 100644 --- a/homeassistant/components/bond/light.py +++ b/homeassistant/components/bond/light.py @@ -26,18 +26,16 @@ async def async_setup_entry( """Set up Bond light devices.""" hub: BondHub = hass.data[DOMAIN][entry.entry_id] - devices = await hass.async_add_executor_job(hub.get_bond_devices) - lights = [ BondLight(hub, device) - for device in devices + for device in hub.devices if device.type == DeviceTypes.CEILING_FAN and device.supports_light() ] async_add_entities(lights, True) fireplaces = [ BondFireplace(hub, device) - for device in devices + for device in hub.devices if device.type == DeviceTypes.FIREPLACE ] async_add_entities(fireplaces, True) diff --git a/homeassistant/components/bond/switch.py b/homeassistant/components/bond/switch.py index e7892272bbf..4768bbf8eda 100644 --- a/homeassistant/components/bond/switch.py +++ b/homeassistant/components/bond/switch.py @@ -21,11 +21,9 @@ async def async_setup_entry( """Set up Bond generic devices.""" hub: BondHub = hass.data[DOMAIN][entry.entry_id] - devices = await hass.async_add_executor_job(hub.get_bond_devices) - switches = [ BondSwitch(hub, device) - for device in devices + for device in hub.devices if device.type == DeviceTypes.GENERIC_DEVICE ] diff --git a/homeassistant/components/bond/utils.py b/homeassistant/components/bond/utils.py index 48fbcd80210..5e1360bcd41 100644 --- a/homeassistant/components/bond/utils.py +++ b/homeassistant/components/bond/utils.py @@ -59,15 +59,15 @@ class BondHub: """Initialize Bond Hub.""" self.bond: Bond = bond self._version: Optional[dict] = None + self._devices: Optional[List[BondDevice]] = None def setup(self): """Read hub version information.""" self._version = self.bond.getVersion() - def get_bond_devices(self) -> List[BondDevice]: - """Fetch all available devices using Bond API.""" + # Fetch all available devices using Bond API. device_ids = self.bond.getDeviceIds() - devices = [ + self._devices = [ BondDevice( device_id, self.bond.getDevice(device_id), @@ -75,7 +75,6 @@ class BondHub: ) for device_id in device_ids ] - return devices @property def bond_id(self) -> str: @@ -91,3 +90,8 @@ class BondHub: def fw_ver(self) -> str: """Return this hub firmware version.""" return self._version.get("fw_ver") + + @property + def devices(self) -> List[BondDevice]: + """Return a list of all devices controlled by this hub.""" + return self._devices diff --git a/tests/components/bond/common.py b/tests/components/bond/common.py index 2aebc3aa2ac..780e235d5c9 100644 --- a/tests/components/bond/common.py +++ b/tests/components/bond/common.py @@ -46,10 +46,7 @@ async def setup_platform( with patch("homeassistant.components.bond.PLATFORMS", [platform]), patch( "homeassistant.components.bond.Bond.getVersion", return_value=MOCK_HUB_VERSION - ), patch( - "homeassistant.components.bond.Bond.getDeviceIds", - return_value=[bond_device_id], - ), patch( + ), patch_bond_device_ids(return_value=[bond_device_id],), patch( "homeassistant.components.bond.Bond.getDevice", return_value=discovered_device ), patch_bond_device_state( return_value={} @@ -62,6 +59,16 @@ async def setup_platform( return mock_entry +def patch_bond_device_ids(return_value=None): + """Patch Bond API getDeviceIds command.""" + if return_value is None: + return_value = [] + + return patch( + "homeassistant.components.bond.Bond.getDeviceIds", return_value=return_value, + ) + + def patch_bond_turn_on(): """Patch Bond API turnOn command.""" return patch("homeassistant.components.bond.Bond.turnOn") diff --git a/tests/components/bond/test_init.py b/tests/components/bond/test_init.py index 7f6250abd37..4d5fd9f4568 100644 --- a/tests/components/bond/test_init.py +++ b/tests/components/bond/test_init.py @@ -6,7 +6,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr from homeassistant.setup import async_setup_component -from .common import setup_bond_entity +from .common import patch_bond_device_ids, setup_bond_entity from tests.async_mock import patch from tests.common import MockConfigEntry @@ -30,7 +30,9 @@ 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_setup_entry("cover") as mock_cover_async_setup_entry, patch_setup_entry( + with patch_bond_device_ids(), patch_setup_entry( + "cover" + ) as mock_cover_async_setup_entry, patch_setup_entry( "fan" ) as mock_fan_async_setup_entry, patch_setup_entry( "light" @@ -75,9 +77,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_setup_entry("cover"), patch_setup_entry("fan"), patch_setup_entry( - "light" - ), patch_setup_entry("switch"): + with patch_bond_device_ids(), 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()