Fix fan speed in auto mode in ViCare integration (#134256)

This commit is contained in:
Christopher Fenner 2025-01-19 13:39:38 +01:00 committed by GitHub
parent 9d5fe77b71
commit 654e111c23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 38 additions and 8 deletions

View File

@ -28,7 +28,7 @@ from homeassistant.util.percentage import (
from .entity import ViCareEntity
from .types import ViCareConfigEntry, ViCareDevice
from .utils import get_device_serial
from .utils import filter_state, get_device_serial
_LOGGER = logging.getLogger(__name__)
@ -143,15 +143,20 @@ class ViCareFan(ViCareEntity, FanEntity):
def update(self) -> None:
"""Update state of fan."""
level: str | None = None
try:
with suppress(PyViCareNotSupportedFeatureError):
self._attr_preset_mode = VentilationMode.from_vicare_mode(
self._api.getActiveMode()
)
with suppress(PyViCareNotSupportedFeatureError):
level = filter_state(self._api.getVentilationLevel())
if level is not None and level in ORDERED_NAMED_FAN_SPEEDS:
self._attr_percentage = ordered_list_item_to_percentage(
ORDERED_NAMED_FAN_SPEEDS, self._api.getActiveProgram()
ORDERED_NAMED_FAN_SPEEDS, VentilationProgram(level)
)
else:
self._attr_percentage = 0
except RequestConnectionError:
_LOGGER.error("Unable to retrieve data from ViCare server")
except ValueError:

View File

@ -49,6 +49,7 @@ from .const import (
from .entity import ViCareEntity
from .types import ViCareConfigEntry, ViCareDevice, ViCareRequiredKeysMixin
from .utils import (
filter_state,
get_burners,
get_circuits,
get_compressors,
@ -796,7 +797,7 @@ GLOBAL_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
translation_key="photovoltaic_status",
device_class=SensorDeviceClass.ENUM,
options=["ready", "production"],
value_getter=lambda api: _filter_states(api.getPhotovoltaicStatus()),
value_getter=lambda api: filter_state(api.getPhotovoltaicStatus()),
),
ViCareSensorEntityDescription(
key="room_temperature",
@ -815,7 +816,7 @@ GLOBAL_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
ViCareSensorEntityDescription(
key="ventilation_level",
translation_key="ventilation_level",
value_getter=lambda api: _filter_states(api.getVentilationLevel().lower()),
value_getter=lambda api: filter_state(api.getVentilationLevel().lower()),
device_class=SensorDeviceClass.ENUM,
options=["standby", "levelone", "leveltwo", "levelthree", "levelfour"],
),
@ -943,10 +944,6 @@ COMPRESSOR_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
)
def _filter_states(state: str) -> str | None:
return None if state in ("nothing", "unknown") else state
def _build_entities(
device_list: list[ViCareDevice],
) -> list[ViCareSensor]:

View File

@ -128,3 +128,8 @@ def get_compressors(device: PyViCareDevice) -> list[PyViCareHeatingDeviceCompone
except AttributeError as error:
_LOGGER.debug("No compressors found: %s", error)
return []
def filter_state(state: str) -> str | None:
"""Remove invalid states."""
return None if state in ("nothing", "unknown") else state

View File

@ -0,0 +1,23 @@
"""Test ViCare utils."""
import pytest
from homeassistant.components.vicare.utils import filter_state
@pytest.mark.parametrize(
("state", "expected_result"),
[
(None, None),
("unknown", None),
("nothing", None),
("levelOne", "levelOne"),
],
)
async def test_filter_state(
state: str | None,
expected_result: str | None,
) -> None:
"""Test filter_state."""
assert filter_state(state) == expected_result