diff --git a/homeassistant/components/vicare/binary_sensor.py b/homeassistant/components/vicare/binary_sensor.py index e9e379f45f205c..3b49fcb91344a7 100644 --- a/homeassistant/components/vicare/binary_sensor.py +++ b/homeassistant/components/vicare/binary_sensor.py @@ -26,7 +26,7 @@ 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_burners, get_circuits, get_compressors, is_supported _LOGGER = logging.getLogger(__name__) @@ -159,26 +159,17 @@ async def async_setup_entry( if entity is not None: entities.append(entity) - try: - await _entities_from_descriptions( - hass, entities, CIRCUIT_SENSORS, api.circuits, config_entry - ) - except PyViCareNotSupportedFeatureError: - _LOGGER.info("No circuits found") + await _entities_from_descriptions( + hass, entities, CIRCUIT_SENSORS, get_circuits(api), config_entry + ) - try: - await _entities_from_descriptions( - hass, entities, BURNER_SENSORS, api.burners, config_entry - ) - except PyViCareNotSupportedFeatureError: - _LOGGER.info("No burners found") + await _entities_from_descriptions( + hass, entities, BURNER_SENSORS, get_burners(api), config_entry + ) - try: - await _entities_from_descriptions( - hass, entities, COMPRESSOR_SENSORS, api.compressors, config_entry - ) - except PyViCareNotSupportedFeatureError: - _LOGGER.info("No compressors found") + await _entities_from_descriptions( + hass, entities, COMPRESSOR_SENSORS, get_compressors(api), config_entry + ) async_add_entities(entities) diff --git a/homeassistant/components/vicare/climate.py b/homeassistant/components/vicare/climate.py index fa41242036a310..97df501f80bfd0 100644 --- a/homeassistant/components/vicare/climate.py +++ b/homeassistant/components/vicare/climate.py @@ -40,6 +40,7 @@ from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG from .entity import ViCareEntity +from .utils import get_burners, get_circuits, get_compressors _LOGGER = logging.getLogger(__name__) @@ -93,15 +94,6 @@ } -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, @@ -111,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 = await hass.async_add_executor_job(_get_circuits, api) + circuits = get_circuits(api) for circuit in circuits: entity = ViCareClimate( @@ -213,11 +205,11 @@ def update(self) -> None: self._current_action = False # Update the specific device attributes with suppress(PyViCareNotSupportedFeatureError): - for burner in self._api.burners: + for burner in get_burners(self._api): self._current_action = self._current_action or burner.getActive() with suppress(PyViCareNotSupportedFeatureError): - for compressor in self._api.compressors: + for compressor in get_compressors(self._api): self._current_action = ( self._current_action or compressor.getActive() ) diff --git a/homeassistant/components/vicare/sensor.py b/homeassistant/components/vicare/sensor.py index cc147f695c69e9..a3f05cfc84bb76 100644 --- a/homeassistant/components/vicare/sensor.py +++ b/homeassistant/components/vicare/sensor.py @@ -45,7 +45,7 @@ VICARE_UNIT_TO_UNIT_OF_MEASUREMENT, ) from .entity import ViCareEntity -from .utils import is_supported +from .utils import get_burners, get_circuits, get_compressors, is_supported _LOGGER = logging.getLogger(__name__) @@ -634,39 +634,33 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Create the ViCare sensor devices.""" - api = hass.data[DOMAIN][config_entry.entry_id][VICARE_API] + api: Device = hass.data[DOMAIN][config_entry.entry_id][VICARE_API] + device_config: PyViCareDeviceConfig = hass.data[DOMAIN][config_entry.entry_id][ + VICARE_DEVICE_CONFIG + ] entities = [] for description in GLOBAL_SENSORS: entity = await hass.async_add_executor_job( _build_entity, api, - hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG], + device_config, description, ) if entity is not None: entities.append(entity) - try: - await _entities_from_descriptions( - hass, entities, CIRCUIT_SENSORS, api.circuits, config_entry - ) - except PyViCareNotSupportedFeatureError: - _LOGGER.info("No circuits found") + await _entities_from_descriptions( + hass, entities, CIRCUIT_SENSORS, get_circuits(api), config_entry + ) - try: - await _entities_from_descriptions( - hass, entities, BURNER_SENSORS, api.burners, config_entry - ) - except PyViCareNotSupportedFeatureError: - _LOGGER.info("No burners found") + await _entities_from_descriptions( + hass, entities, BURNER_SENSORS, get_burners(api), config_entry + ) - try: - await _entities_from_descriptions( - hass, entities, COMPRESSOR_SENSORS, api.compressors, config_entry - ) - except PyViCareNotSupportedFeatureError: - _LOGGER.info("No compressors found") + await _entities_from_descriptions( + hass, entities, COMPRESSOR_SENSORS, get_compressors(api), config_entry + ) async_add_entities(entities) diff --git a/homeassistant/components/vicare/utils.py b/homeassistant/components/vicare/utils.py index 19a75c00962458..5b3fb38337ff3c 100644 --- a/homeassistant/components/vicare/utils.py +++ b/homeassistant/components/vicare/utils.py @@ -1,6 +1,10 @@ """ViCare helpers functions.""" import logging +from PyViCare.PyViCareDevice import Device as PyViCareDevice +from PyViCare.PyViCareHeatingDevice import ( + HeatingDeviceWithComponent as PyViCareHeatingDeviceComponent, +) from PyViCare.PyViCareUtils import PyViCareNotSupportedFeatureError from . import ViCareRequiredKeysMixin @@ -24,3 +28,30 @@ def is_supported( _LOGGER.debug("Attribute Error %s: %s", name, error) return False return True + + +def get_burners(device: PyViCareDevice) -> list[PyViCareHeatingDeviceComponent]: + """Return the list of burners.""" + try: + return device.burners + except PyViCareNotSupportedFeatureError: + _LOGGER.debug("No burners found") + return [] + + +def get_circuits(device: PyViCareDevice) -> list[PyViCareHeatingDeviceComponent]: + """Return the list of circuits.""" + try: + return device.circuits + except PyViCareNotSupportedFeatureError: + _LOGGER.debug("No circuits found") + return [] + + +def get_compressors(device: PyViCareDevice) -> list[PyViCareHeatingDeviceComponent]: + """Return the list of compressors.""" + try: + return device.compressors + except PyViCareNotSupportedFeatureError: + _LOGGER.debug("No compressors found") + return [] diff --git a/homeassistant/components/vicare/water_heater.py b/homeassistant/components/vicare/water_heater.py index 570474f4186f1f..9b33ca9947ad9f 100644 --- a/homeassistant/components/vicare/water_heater.py +++ b/homeassistant/components/vicare/water_heater.py @@ -24,6 +24,7 @@ from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG from .entity import ViCareEntity +from .utils import get_circuits _LOGGER = logging.getLogger(__name__) @@ -57,15 +58,6 @@ } -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,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 = await hass.async_add_executor_job(_get_circuits, api) + circuits = get_circuits(api) for circuit in circuits: entity = ViCareWater(