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.""" """Set up Bond cover devices."""
hub: BondHub = hass.data[DOMAIN][entry.entry_id] hub: BondHub = hass.data[DOMAIN][entry.entry_id]
devices = await hass.async_add_executor_job(hub.get_bond_devices)
covers = [ covers = [
BondCover(hub, device) BondCover(hub, device)
for device in devices for device in hub.devices
if device.type == DeviceTypes.MOTORIZED_SHADES if device.type == DeviceTypes.MOTORIZED_SHADES
] ]

View File

@ -32,11 +32,9 @@ async def async_setup_entry(
"""Set up Bond fan devices.""" """Set up Bond fan devices."""
hub: BondHub = hass.data[DOMAIN][entry.entry_id] hub: BondHub = hass.data[DOMAIN][entry.entry_id]
devices = await hass.async_add_executor_job(hub.get_bond_devices)
fans = [ fans = [
BondFan(hub, device) BondFan(hub, device)
for device in devices for device in hub.devices
if device.type == DeviceTypes.CEILING_FAN if device.type == DeviceTypes.CEILING_FAN
] ]

View File

@ -26,18 +26,16 @@ async def async_setup_entry(
"""Set up Bond light devices.""" """Set up Bond light devices."""
hub: BondHub = hass.data[DOMAIN][entry.entry_id] hub: BondHub = hass.data[DOMAIN][entry.entry_id]
devices = await hass.async_add_executor_job(hub.get_bond_devices)
lights = [ lights = [
BondLight(hub, device) BondLight(hub, device)
for device in devices for device in hub.devices
if device.type == DeviceTypes.CEILING_FAN and device.supports_light() if device.type == DeviceTypes.CEILING_FAN and device.supports_light()
] ]
async_add_entities(lights, True) async_add_entities(lights, True)
fireplaces = [ fireplaces = [
BondFireplace(hub, device) BondFireplace(hub, device)
for device in devices for device in hub.devices
if device.type == DeviceTypes.FIREPLACE if device.type == DeviceTypes.FIREPLACE
] ]
async_add_entities(fireplaces, True) async_add_entities(fireplaces, True)

View File

@ -21,11 +21,9 @@ async def async_setup_entry(
"""Set up Bond generic devices.""" """Set up Bond generic devices."""
hub: BondHub = hass.data[DOMAIN][entry.entry_id] hub: BondHub = hass.data[DOMAIN][entry.entry_id]
devices = await hass.async_add_executor_job(hub.get_bond_devices)
switches = [ switches = [
BondSwitch(hub, device) BondSwitch(hub, device)
for device in devices for device in hub.devices
if device.type == DeviceTypes.GENERIC_DEVICE if device.type == DeviceTypes.GENERIC_DEVICE
] ]

View File

@ -59,15 +59,15 @@ class BondHub:
"""Initialize Bond Hub.""" """Initialize Bond Hub."""
self.bond: Bond = bond self.bond: Bond = bond
self._version: Optional[dict] = None self._version: Optional[dict] = None
self._devices: Optional[List[BondDevice]] = None
def setup(self): def setup(self):
"""Read hub version information.""" """Read hub version information."""
self._version = self.bond.getVersion() 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() device_ids = self.bond.getDeviceIds()
devices = [ self._devices = [
BondDevice( BondDevice(
device_id, device_id,
self.bond.getDevice(device_id), self.bond.getDevice(device_id),
@ -75,7 +75,6 @@ class BondHub:
) )
for device_id in device_ids for device_id in device_ids
] ]
return devices
@property @property
def bond_id(self) -> str: def bond_id(self) -> str:
@ -91,3 +90,8 @@ class BondHub:
def fw_ver(self) -> str: def fw_ver(self) -> str:
"""Return this hub firmware version.""" """Return this hub firmware version."""
return self._version.get("fw_ver") 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( with patch("homeassistant.components.bond.PLATFORMS", [platform]), patch(
"homeassistant.components.bond.Bond.getVersion", return_value=MOCK_HUB_VERSION "homeassistant.components.bond.Bond.getVersion", return_value=MOCK_HUB_VERSION
), patch( ), patch_bond_device_ids(return_value=[bond_device_id],), patch(
"homeassistant.components.bond.Bond.getDeviceIds",
return_value=[bond_device_id],
), patch(
"homeassistant.components.bond.Bond.getDevice", return_value=discovered_device "homeassistant.components.bond.Bond.getDevice", return_value=discovered_device
), patch_bond_device_state( ), patch_bond_device_state(
return_value={} return_value={}
@ -62,6 +59,16 @@ async def setup_platform(
return mock_entry 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(): def patch_bond_turn_on():
"""Patch Bond API turnOn command.""" """Patch Bond API turnOn command."""
return patch("homeassistant.components.bond.Bond.turnOn") 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.helpers import device_registry as dr
from homeassistant.setup import async_setup_component 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.async_mock import patch
from tests.common import MockConfigEntry 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"}, 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" "fan"
) as mock_fan_async_setup_entry, patch_setup_entry( ) as mock_fan_async_setup_entry, patch_setup_entry(
"light" "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"}, 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( with patch_bond_device_ids(), patch_setup_entry("cover"), patch_setup_entry(
"light" "fan"
), patch_setup_entry("switch"): ), patch_setup_entry("light"), patch_setup_entry("switch"):
result = await setup_bond_entity(hass, config_entry) result = await setup_bond_entity(hass, config_entry)
assert result is True assert result is True
await hass.async_block_till_done() await hass.async_block_till_done()