Always send Vallox fan commands (#191586)

This commit is contained in:
Andre Richter
2026-11-09 14:28:24 +01:00
committed by GitHub
parent 0da34b87b6
commit ffb6496fae
2 changed files with 60 additions and 98 deletions
+20 -45
View File
@@ -129,13 +129,12 @@ class ValloxFanEntity(ValloxEntity, FanEntity):
@override
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
update_needed = await self._async_set_preset_mode_internal(preset_mode)
await self._async_set_preset_mode_internal(preset_mode)
if update_needed:
# This state change affects other entities like
# sensors. Force an immediate update that can be
# observed by all parties involved.
await self.coordinator.async_request_refresh()
# This state change affects other entities like
# sensors. Force an immediate update that can be
# observed by all parties involved.
await self.coordinator.async_request_refresh()
@override
async def async_turn_on(
@@ -157,32 +156,25 @@ class ValloxFanEntity(ValloxEntity, FanEntity):
# the call fails without changing the unit. And a unit goes straight
# to the new speed when the preset switches below.
if percentage is not None:
update_needed |= await self._async_set_percentage_internal(
percentage, preset_mode
)
await self._async_set_percentage_internal(percentage, preset_mode)
update_needed = True
if preset_mode:
update_needed |= await self._async_set_preset_mode_internal(preset_mode)
await self._async_set_preset_mode_internal(preset_mode)
update_needed = True
# Last, so the unit starts in its final profile and speed.
if not self.is_on:
update_needed |= await self._async_set_power(True)
await self._async_set_power(True)
update_needed = True
finally:
# This state change affects other entities like sensors. After a
# failure, the commands sent before it are still applied.
if update_needed:
await self.coordinator.async_request_refresh()
@override
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
if self.is_on is False:
return
update_needed = await self._async_set_power(False)
if update_needed:
await self.coordinator.async_request_refresh()
await self._async_set_power(False)
await self.coordinator.async_request_refresh()
@override
async def async_set_percentage(self, percentage: int) -> None:
@@ -191,12 +183,10 @@ class ValloxFanEntity(ValloxEntity, FanEntity):
await self.async_turn_off()
return
update_needed = await self._async_set_percentage_internal(percentage)
await self._async_set_percentage_internal(percentage)
await self.coordinator.async_request_refresh()
if update_needed:
await self.coordinator.async_request_refresh()
async def _async_set_power(self, mode: bool) -> bool:
async def _async_set_power(self, mode: bool) -> None:
try:
await self.coordinator.client.set_values(
{METRIC_KEY_MODE: MODE_ON if mode else MODE_OFF}
@@ -204,16 +194,8 @@ class ValloxFanEntity(ValloxEntity, FanEntity):
except ValloxApiException as err:
raise HomeAssistantError("Failed to set power mode") from err
return True
async def _async_set_preset_mode_internal(self, preset_mode: str) -> bool:
"""Set new preset mode.
Returns true if the mode has been changed, false otherwise.
"""
if preset_mode == self.preset_mode:
return False
async def _async_set_preset_mode_internal(self, preset_mode: str) -> None:
"""Set new preset mode."""
try:
profile = PRESET_MODE_TO_VALLOX_PROFILE[preset_mode]
await self.coordinator.client.set_profile(profile)
@@ -221,15 +203,10 @@ class ValloxFanEntity(ValloxEntity, FanEntity):
except ValloxApiException as err:
raise HomeAssistantError(f"Failed to set profile: {preset_mode}") from err
return True
async def _async_set_percentage_internal(
self, percentage: int, preset_mode: str | None = None
) -> bool:
"""Set fan speed percentage for current profile.
Returns true if speed has been changed, false otherwise.
"""
) -> None:
"""Set the fan speed of the given preset, or of the current profile."""
vallox_profile = (
PRESET_MODE_TO_VALLOX_PROFILE[preset_mode]
if preset_mode is not None
@@ -252,5 +229,3 @@ class ValloxFanEntity(ValloxEntity, FanEntity):
) from err
except ValloxApiException as err:
raise HomeAssistantError("Failed to set fan speed") from err
return True
+40 -53
View File
@@ -121,21 +121,12 @@ async def test_fan_speed_missing(hass: HomeAssistant) -> None:
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize("metrics", [{"A_CYC_MODE": None}])
async def test_fan_mode_missing(
hass: HomeAssistant, mock_vallox_client: AsyncMock
) -> None:
async def test_fan_mode_missing(hass: HomeAssistant) -> None:
"""Test the fan when the unit does not report whether it is on."""
state = hass.states.get(FAN_ENTITY_ID)
assert state
assert state.state == STATE_UNKNOWN
# The unit may well be on, so turning it off must not be skipped.
await hass.services.async_call(
FAN_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: FAN_ENTITY_ID}, blocking=True
)
mock_vallox_client.set_values.assert_called_once_with(METRICS_UNIT_OFF)
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize(
@@ -145,6 +136,14 @@ async def test_fan_mode_missing(
pytest.param(
SERVICE_TURN_OFF, METRICS_UNIT_ON, METRICS_UNIT_OFF, id="turn_off"
),
# The unit is already in the requested state according to the last poll.
# It may have changed since, so check that the command is sent regardless.
pytest.param(
SERVICE_TURN_ON, METRICS_UNIT_ON, METRICS_UNIT_ON, id="turn_on_when_on"
),
pytest.param(
SERVICE_TURN_OFF, METRICS_UNIT_OFF, METRICS_UNIT_OFF, id="turn_off_when_off"
),
],
)
async def test_turn_on_off(
@@ -163,26 +162,6 @@ async def test_turn_on_off(
assert mock_vallox_client.fetch_metric_data.call_count == 2
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize(
("service", "metrics"),
[
pytest.param(SERVICE_TURN_ON, METRICS_UNIT_ON, id="turn_on_when_on"),
pytest.param(SERVICE_TURN_OFF, METRICS_UNIT_OFF, id="turn_off_when_off"),
],
)
async def test_turn_on_off_unchanged(
hass: HomeAssistant, mock_vallox_client: AsyncMock, service: str
) -> None:
"""Test that turning the fan on or off when it already is does nothing."""
await hass.services.async_call(
FAN_DOMAIN, service, {ATTR_ENTITY_ID: FAN_ENTITY_ID}, blocking=True
)
mock_vallox_client.set_values.assert_not_called()
mock_vallox_client.fetch_metric_data.assert_called_once()
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize(
("service", "metrics"),
@@ -224,6 +203,7 @@ async def test_turn_on_off_exception(
[
call.set_fan_speed(Profile.AWAY, 15),
call.set_profile(Profile.AWAY),
call.set_values(METRICS_UNIT_ON),
call.fetch_metric_data(),
],
id="preset_and_percentage_unit_on",
@@ -233,14 +213,22 @@ async def test_turn_on_off_exception(
{ATTR_PRESET_MODE: "Away"},
[
call.set_profile(Profile.AWAY),
call.set_values(METRICS_UNIT_ON),
call.fetch_metric_data(),
],
id="preset_only",
),
# The unit is already on and in Home according to the last poll. It may
# have changed since, so check that the preset and power-on are sent
# regardless.
pytest.param(
METRICS_UNIT_ON,
{ATTR_PRESET_MODE: "Home"},
[],
[
call.set_profile(Profile.HOME),
call.set_values(METRICS_UNIT_ON),
call.fetch_metric_data(),
],
id="active_preset",
),
pytest.param(
@@ -248,6 +236,7 @@ async def test_turn_on_off_exception(
{ATTR_PERCENTAGE: 40},
[
call.set_fan_speed(Profile.HOME, 40),
call.set_values(METRICS_UNIT_ON),
call.fetch_metric_data(),
],
id="percentage_only",
@@ -273,7 +262,10 @@ async def test_turn_on_off_exception(
pytest.param(
METRICS_UNIT_OFF,
{ATTR_PERCENTAGE: 0},
[],
[
call.set_values(METRICS_UNIT_OFF),
call.fetch_metric_data(),
],
id="zero_percentage_unit_off",
),
],
@@ -391,34 +383,28 @@ async def test_turn_on_unsupported_profile(
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize(
("preset", "metrics", "expected_set_profile_calls", "expected_fetch_count"),
("preset", "metrics", "expected_profile"),
[
pytest.param("Home", METRICS_PROFILE_AWAY, [call(Profile.HOME)], 2, id="home"),
pytest.param("Away", METRICS_PROFILE_HOME, [call(Profile.AWAY)], 2, id="away"),
pytest.param("Home", METRICS_PROFILE_AWAY, Profile.HOME, id="home"),
pytest.param("Away", METRICS_PROFILE_HOME, Profile.AWAY, id="away"),
pytest.param("Boost", METRICS_PROFILE_HOME, Profile.BOOST, id="boost"),
pytest.param(
"Boost", METRICS_PROFILE_HOME, [call(Profile.BOOST)], 2, id="boost"
"Fireplace", METRICS_PROFILE_HOME, Profile.FIREPLACE, id="fireplace"
),
pytest.param(
"Fireplace",
METRICS_PROFILE_HOME,
[call(Profile.FIREPLACE)],
2,
id="fireplace",
),
pytest.param(
"Extra", METRICS_PROFILE_HOME, [call(Profile.EXTRA)], 2, id="extra"
),
pytest.param("Auto", METRICS_PROFILE_HOME, [call(Profile.AUTO)], 2, id="auto"),
# Selecting the active preset changes nothing, so there is no refresh.
pytest.param("Home", METRICS_PROFILE_HOME, [], 1, id="no_change"),
pytest.param("Extra", METRICS_PROFILE_HOME, Profile.EXTRA, id="extra"),
pytest.param("Auto", METRICS_PROFILE_HOME, Profile.AUTO, id="auto"),
# The preset is already active according to the last poll. The unit may
# have changed since, for example when its Boost timer ran out, so check
# that the preset is sent regardless.
pytest.param("Home", METRICS_PROFILE_HOME, Profile.HOME, id="active_home"),
pytest.param("Boost", METRICS_PROFILE_BOOST, Profile.BOOST, id="active_boost"),
],
)
async def test_set_preset_mode(
hass: HomeAssistant,
mock_vallox_client: AsyncMock,
preset: str,
expected_set_profile_calls: list[_Call],
expected_fetch_count: int,
expected_profile: Profile,
) -> None:
"""Test set preset mode."""
await hass.services.async_call(
@@ -428,8 +414,9 @@ async def test_set_preset_mode(
blocking=True,
)
assert mock_vallox_client.set_profile.call_args_list == expected_set_profile_calls
assert mock_vallox_client.fetch_metric_data.call_count == expected_fetch_count
mock_vallox_client.set_profile.assert_called_once_with(expected_profile)
# One fetch at setup, and one refresh after the change.
assert mock_vallox_client.fetch_metric_data.call_count == 2
@pytest.mark.usefixtures("init_integration")