Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 10 additions & 19 deletions homeassistant/components/vicare/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 @@ -165,26 +165,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 @@ -37,6 +37,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 @@ -90,15 +91,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 @@ -107,7 +99,7 @@ async def async_setup_entry(
"""Set up the ViCare climate platform."""
entities = []
api = hass.data[DOMAIN][config_entry.entry_id][VICARE_API]
circuits = await hass.async_add_executor_job(_get_circuits, api)
circuits = get_circuits(api)
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved

for circuit in circuits:
suffix = ""
Expand Down Expand Up @@ -207,11 +199,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 @@ -641,40 +641,34 @@ 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,
description.name,
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 @@ -21,6 +21,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 @@ -54,15 +55,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 @@ -71,7 +63,7 @@ async def async_setup_entry(
"""Set up the ViCare climate platform."""
entities = []
api = hass.data[DOMAIN][config_entry.entry_id][VICARE_API]
circuits = await hass.async_add_executor_job(_get_circuits, api)
circuits = get_circuits(api)

for circuit in circuits:
suffix = ""
Expand Down