From a9cc35d6b6c5d5791df49672aeaf69650541ba50 Mon Sep 17 00:00:00 2001 From: Hans Oischinger Date: Fri, 28 Jan 2022 12:06:05 +0100 Subject: [PATCH] Handle vicare I/O in executor (#65105) Co-authored-by: Martin Hjelmare --- homeassistant/components/vicare/climate.py | 41 ++++++++++-------- .../components/vicare/water_heater.py | 42 +++++++++++-------- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/vicare/climate.py b/homeassistant/components/vicare/climate.py index a528fd63614..451ea70edab 100644 --- a/homeassistant/components/vicare/climate.py +++ b/homeassistant/components/vicare/climate.py @@ -101,6 +101,15 @@ def _build_entity(name, vicare_api, circuit, device_config, heating_type): return ViCareClimate(name, vicare_api, device_config, circuit, heating_type) +def _get_circuits(vicare_api): + """Return the list of circuits.""" + try: + return vicare_api.circuits + except PyViCareNotSupportedFeatureError: + _LOGGER.info("No circuits found") + return [] + + async def async_setup_entry( hass: HomeAssistant, config_entry: ConfigEntry, @@ -108,25 +117,23 @@ async def async_setup_entry( ) -> None: """Set up the ViCare climate platform.""" name = VICARE_NAME - entities = [] + api = hass.data[DOMAIN][config_entry.entry_id][VICARE_API] + circuits = await hass.async_add_executor_job(_get_circuits, api) - try: - for circuit in hass.data[DOMAIN][config_entry.entry_id][VICARE_API].circuits: - suffix = "" - if len(hass.data[DOMAIN][config_entry.entry_id][VICARE_API].circuits) > 1: - suffix = f" {circuit.id}" - entity = _build_entity( - f"{name} Heating{suffix}", - hass.data[DOMAIN][config_entry.entry_id][VICARE_API], - hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG], - circuit, - config_entry.data[CONF_HEATING_TYPE], - ) - if entity is not None: - entities.append(entity) - except PyViCareNotSupportedFeatureError: - _LOGGER.info("No circuits found") + for circuit in circuits: + suffix = "" + if len(circuits) > 1: + suffix = f" {circuit.id}" + + entity = _build_entity( + f"{name} Heating{suffix}", + api, + hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG], + circuit, + config_entry.data[CONF_HEATING_TYPE], + ) + entities.append(entity) platform = entity_platform.async_get_current_platform() diff --git a/homeassistant/components/vicare/water_heater.py b/homeassistant/components/vicare/water_heater.py index 5912287a953..0107ff8fe4c 100644 --- a/homeassistant/components/vicare/water_heater.py +++ b/homeassistant/components/vicare/water_heater.py @@ -68,6 +68,15 @@ def _build_entity(name, vicare_api, circuit, device_config, heating_type): ) +def _get_circuits(vicare_api): + """Return the list of circuits.""" + try: + return vicare_api.circuits + except PyViCareNotSupportedFeatureError: + _LOGGER.info("No circuits found") + return [] + + async def async_setup_entry( hass: HomeAssistant, config_entry: ConfigEntry, @@ -75,24 +84,23 @@ async def async_setup_entry( ) -> None: """Set up the ViCare climate platform.""" name = VICARE_NAME - entities = [] - try: - for circuit in hass.data[DOMAIN][config_entry.entry_id][VICARE_API].circuits: - suffix = "" - if len(hass.data[DOMAIN][config_entry.entry_id][VICARE_API].circuits) > 1: - suffix = f" {circuit.id}" - entity = _build_entity( - f"{name} Water{suffix}", - hass.data[DOMAIN][config_entry.entry_id][VICARE_API], - circuit, - hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG], - config_entry.data[CONF_HEATING_TYPE], - ) - if entity is not None: - entities.append(entity) - except PyViCareNotSupportedFeatureError: - _LOGGER.info("No circuits found") + api = hass.data[DOMAIN][config_entry.entry_id][VICARE_API] + circuits = await hass.async_add_executor_job(_get_circuits, api) + + for circuit in circuits: + suffix = "" + if len(circuits) > 1: + suffix = f" {circuit.id}" + + entity = _build_entity( + f"{name} Water{suffix}", + api, + circuit, + hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG], + config_entry.data[CONF_HEATING_TYPE], + ) + entities.append(entity) async_add_entities(entities)