Skip to content

Commit

Permalink
Add reuse functions to access circuits, burners and compressors in Vi…
Browse files Browse the repository at this point in the history
…Care integration (home-assistant#104371)
  • Loading branch information
CFenner committed Nov 27, 2023
1 parent 7e5fa0e commit 5479ef0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 62 deletions.
29 changes: 10 additions & 19 deletions homeassistant/components/vicare/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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)

Expand Down
16 changes: 4 additions & 12 deletions homeassistant/components/vicare/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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()
)
Expand Down
36 changes: 15 additions & 21 deletions homeassistant/components/vicare/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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)

Expand Down
31 changes: 31 additions & 0 deletions homeassistant/components/vicare/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 []
12 changes: 2 additions & 10 deletions homeassistant/components/vicare/water_heater.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit 5479ef0

Please sign in to comment.