Improve bond startup performance (#37900)

This commit is contained in:
Eugene Prystupa 2020-07-16 08:31:15 -07:00 committed by GitHub
parent 93c6a9cd96
commit 37a70c73a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 26 deletions

View File

@ -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
]

View File

@ -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
]

View File

@ -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)

View File

@ -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
]

View File

@ -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

View File

@ -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")

View File

@ -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()