From ad17acc6ca4e597bdf9d89076408a7caa07cdcb2 Mon Sep 17 00:00:00 2001 From: Christopher Fenner <9592452+CFenner@users.noreply.github.com> Date: Sun, 26 Nov 2023 15:28:58 +0100 Subject: [PATCH] Fix async issue in ViCare integration (#104541) * use async executor for get_circuits * use async executor for get_burners and get_compressors --- homeassistant/components/vicare/binary_sensor.py | 9 ++++++--- homeassistant/components/vicare/climate.py | 12 ++++++++---- homeassistant/components/vicare/number.py | 5 +++-- homeassistant/components/vicare/sensor.py | 9 ++++++--- homeassistant/components/vicare/water_heater.py | 2 +- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/vicare/binary_sensor.py b/homeassistant/components/vicare/binary_sensor.py index 3b49fcb9134..bab132121f6 100644 --- a/homeassistant/components/vicare/binary_sensor.py +++ b/homeassistant/components/vicare/binary_sensor.py @@ -159,16 +159,19 @@ async def async_setup_entry( if entity is not None: entities.append(entity) + circuits = await hass.async_add_executor_job(get_circuits, api) await _entities_from_descriptions( - hass, entities, CIRCUIT_SENSORS, get_circuits(api), config_entry + hass, entities, CIRCUIT_SENSORS, circuits, config_entry ) + burners = await hass.async_add_executor_job(get_burners, api) await _entities_from_descriptions( - hass, entities, BURNER_SENSORS, get_burners(api), config_entry + hass, entities, BURNER_SENSORS, burners, config_entry ) + compressors = await hass.async_add_executor_job(get_compressors, api) await _entities_from_descriptions( - hass, entities, COMPRESSOR_SENSORS, get_compressors(api), config_entry + hass, entities, COMPRESSOR_SENSORS, compressors, config_entry ) async_add_entities(entities) diff --git a/homeassistant/components/vicare/climate.py b/homeassistant/components/vicare/climate.py index 97df501f80b..b32b6e28480 100644 --- a/homeassistant/components/vicare/climate.py +++ b/homeassistant/components/vicare/climate.py @@ -103,7 +103,7 @@ async def async_setup_entry( entities = [] api = hass.data[DOMAIN][config_entry.entry_id][VICARE_API] device_config = hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG] - circuits = get_circuits(api) + circuits = await hass.async_add_executor_job(get_circuits, api) for circuit in circuits: entity = ViCareClimate( @@ -154,7 +154,7 @@ class ViCareClimate(ViCareEntity, ClimateEntity): self._current_program = None self._attr_translation_key = translation_key - def update(self) -> None: + async def async_update(self) -> None: """Let HA know there has been an update from the ViCare API.""" try: _room_temperature = None @@ -205,11 +205,15 @@ class ViCareClimate(ViCareEntity, ClimateEntity): self._current_action = False # Update the specific device attributes with suppress(PyViCareNotSupportedFeatureError): - for burner in get_burners(self._api): + burners = await self.hass.async_add_executor_job(get_burners, self._api) + for burner in burners: self._current_action = self._current_action or burner.getActive() with suppress(PyViCareNotSupportedFeatureError): - for compressor in get_compressors(self._api): + compressors = await self.hass.async_add_executor_job( + get_compressors, self._api + ) + for compressor in compressors: self._current_action = ( self._current_action or compressor.getActive() ) diff --git a/homeassistant/components/vicare/number.py b/homeassistant/components/vicare/number.py index cfbe13e1267..577bb8257ea 100644 --- a/homeassistant/components/vicare/number.py +++ b/homeassistant/components/vicare/number.py @@ -28,7 +28,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import ViCareRequiredKeysMixin from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG from .entity import ViCareEntity -from .utils import is_supported +from .utils import get_circuits, is_supported _LOGGER = logging.getLogger(__name__) @@ -93,10 +93,11 @@ async def async_setup_entry( ) -> None: """Create the ViCare number devices.""" api = hass.data[DOMAIN][config_entry.entry_id][VICARE_API] + circuits = await hass.async_add_executor_job(get_circuits, api) entities: list[ViCareNumber] = [] try: - for circuit in api.circuits: + for circuit in circuits: for description in CIRCUIT_ENTITY_DESCRIPTIONS: entity = await hass.async_add_executor_job( _build_entity, diff --git a/homeassistant/components/vicare/sensor.py b/homeassistant/components/vicare/sensor.py index a3f05cfc84b..caf1151f5ec 100644 --- a/homeassistant/components/vicare/sensor.py +++ b/homeassistant/components/vicare/sensor.py @@ -650,16 +650,19 @@ async def async_setup_entry( if entity is not None: entities.append(entity) + circuits = await hass.async_add_executor_job(get_circuits, api) await _entities_from_descriptions( - hass, entities, CIRCUIT_SENSORS, get_circuits(api), config_entry + hass, entities, CIRCUIT_SENSORS, circuits, config_entry ) + burners = await hass.async_add_executor_job(get_burners, api) await _entities_from_descriptions( - hass, entities, BURNER_SENSORS, get_burners(api), config_entry + hass, entities, BURNER_SENSORS, burners, config_entry ) + compressors = await hass.async_add_executor_job(get_compressors, api) await _entities_from_descriptions( - hass, entities, COMPRESSOR_SENSORS, get_compressors(api), config_entry + hass, entities, COMPRESSOR_SENSORS, compressors, config_entry ) async_add_entities(entities) diff --git a/homeassistant/components/vicare/water_heater.py b/homeassistant/components/vicare/water_heater.py index 9b33ca9947a..9b154da2bc2 100644 --- a/homeassistant/components/vicare/water_heater.py +++ b/homeassistant/components/vicare/water_heater.py @@ -67,7 +67,7 @@ async def async_setup_entry( entities = [] api = hass.data[DOMAIN][config_entry.entry_id][VICARE_API] device_config = hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG] - circuits = get_circuits(api) + circuits = await hass.async_add_executor_job(get_circuits, api) for circuit in circuits: entity = ViCareWater(