Add reuse functions to access circuits, burners and compressors in ViCare integration (#104371)

This commit is contained in:
Christopher Fenner 2023-11-25 15:47:45 +01:00 committed by GitHub
parent 03caa21a51
commit 48f8cec84b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 62 deletions

View File

@ -26,7 +26,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_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)

View File

@ -40,6 +40,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
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 @@ HA_TO_VICARE_PRESET_HEATING = {
}
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 @@ class ViCareClimate(ViCareEntity, ClimateEntity):
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()
)

View File

@ -45,7 +45,7 @@ from .const import (
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)

View File

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

View File

@ -24,6 +24,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
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 @@ HA_TO_VICARE_HVAC_DHW = {
}
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(