mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Use library accessors for Vallox power and bypass lock (#193675)
This commit is contained in:
@@ -9,15 +9,11 @@ DEFAULT_NAME = "Vallox"
|
||||
|
||||
STATE_SCAN_INTERVAL = timedelta(seconds=60)
|
||||
|
||||
# Common metric keys and (default) values.
|
||||
METRIC_KEY_MODE = "A_CYC_MODE"
|
||||
# Common metric keys.
|
||||
METRIC_KEY_PROFILE_FAN_SPEED_HOME = "A_CYC_HOME_SPEED_SETTING"
|
||||
METRIC_KEY_PROFILE_FAN_SPEED_AWAY = "A_CYC_AWAY_SPEED_SETTING"
|
||||
METRIC_KEY_PROFILE_FAN_SPEED_BOOST = "A_CYC_BOOST_SPEED_SETTING"
|
||||
|
||||
MODE_ON = 0
|
||||
MODE_OFF = 5
|
||||
|
||||
DEFAULT_FAN_SPEED_HOME = 50
|
||||
DEFAULT_FAN_SPEED_AWAY = 25
|
||||
DEFAULT_FAN_SPEED_BOOST = 65
|
||||
|
||||
@@ -14,12 +14,9 @@ from homeassistant.helpers.typing import StateType
|
||||
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
METRIC_KEY_MODE,
|
||||
METRIC_KEY_PROFILE_FAN_SPEED_AWAY,
|
||||
METRIC_KEY_PROFILE_FAN_SPEED_BOOST,
|
||||
METRIC_KEY_PROFILE_FAN_SPEED_HOME,
|
||||
MODE_OFF,
|
||||
MODE_ON,
|
||||
PRESET_MODE_TO_VALLOX_PROFILE,
|
||||
VALLOX_PROFILE_TO_PRESET_MODE,
|
||||
)
|
||||
@@ -94,9 +91,7 @@ class ValloxFanEntity(ValloxEntity, FanEntity):
|
||||
@override
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return if device is on."""
|
||||
if (mode := self.coordinator.data.get(METRIC_KEY_MODE)) is None:
|
||||
return None
|
||||
return mode == MODE_ON
|
||||
return self.coordinator.data.is_switched_on
|
||||
|
||||
@property
|
||||
@override
|
||||
@@ -184,11 +179,12 @@ class ValloxFanEntity(ValloxEntity, FanEntity):
|
||||
await self._async_set_percentage_internal(percentage)
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
async def _async_set_power(self, mode: bool) -> None:
|
||||
async def _async_set_power(self, on: bool) -> None:
|
||||
try:
|
||||
await self.coordinator.client.set_values(
|
||||
{METRIC_KEY_MODE: MODE_ON if mode else MODE_OFF}
|
||||
)
|
||||
if on:
|
||||
await self.coordinator.client.switch_on()
|
||||
else:
|
||||
await self.coordinator.client.switch_off()
|
||||
except ValloxApiException as err:
|
||||
raise HomeAssistantError("Failed to set power mode") from err
|
||||
|
||||
|
||||
@@ -27,8 +27,6 @@ from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import (
|
||||
METRIC_KEY_MODE,
|
||||
MODE_ON,
|
||||
PRESET_MODE_TO_VALLOX_PROFILE,
|
||||
VALLOX_CELL_STATE_TO_STR,
|
||||
VALLOX_PROFILE_TO_PRESET_MODE,
|
||||
@@ -95,11 +93,11 @@ class ValloxComputedSensorEntity(ValloxSensorEntity):
|
||||
|
||||
|
||||
def _fan_speed(data: MetricData, metric_key: str) -> StateType:
|
||||
"""Return a fan speed, or 0 while the unit is off."""
|
||||
if (mode := data.get(METRIC_KEY_MODE)) is None:
|
||||
"""Return a fan speed, or 0 while the unit is switched off."""
|
||||
if (switched_on := data.is_switched_on) is None:
|
||||
return None
|
||||
# The unit keeps reporting its last fan speeds after it is turned off.
|
||||
return data.get(metric_key) if mode == MODE_ON else 0
|
||||
# The unit keeps reporting its last fan speeds after it is switched off.
|
||||
return data.get(metric_key) if switched_on else 0
|
||||
|
||||
|
||||
def _next_filter_change(data: MetricData) -> datetime | None:
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
"""Support for Vallox ventilation unit switches."""
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, override
|
||||
|
||||
from vallox_websocket_api import MetricData, Vallox
|
||||
|
||||
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
||||
from homeassistant.const import CONF_NAME, EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
@@ -35,26 +38,21 @@ class ValloxSwitchEntity(ValloxEntity, SwitchEntity):
|
||||
@override
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if the switch is on."""
|
||||
if (
|
||||
value := self.coordinator.data.get(self.entity_description.metric_key)
|
||||
) is None:
|
||||
return None
|
||||
return value == 1
|
||||
return self.entity_description.is_on_fn(self.coordinator.data)
|
||||
|
||||
@override
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on."""
|
||||
await self._set_value(True)
|
||||
await self._async_set(True)
|
||||
|
||||
@override
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn off."""
|
||||
await self._set_value(False)
|
||||
await self._async_set(False)
|
||||
|
||||
async def _set_value(self, value: bool) -> None:
|
||||
async def _async_set(self, on: bool) -> None:
|
||||
"""Update the current value."""
|
||||
metric_key = self.entity_description.metric_key
|
||||
await self.coordinator.client.set_values({metric_key: 1 if value else 0})
|
||||
await self.entity_description.set_fn(self.coordinator.client, on)
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
|
||||
@@ -62,14 +60,16 @@ class ValloxSwitchEntity(ValloxEntity, SwitchEntity):
|
||||
class ValloxSwitchEntityDescription(SwitchEntityDescription):
|
||||
"""Describes Vallox switch entity."""
|
||||
|
||||
metric_key: str
|
||||
is_on_fn: Callable[[MetricData], bool | None]
|
||||
set_fn: Callable[[Vallox, bool], Awaitable[None]]
|
||||
|
||||
|
||||
SWITCH_ENTITIES: tuple[ValloxSwitchEntityDescription, ...] = (
|
||||
ValloxSwitchEntityDescription(
|
||||
key="bypass_locked",
|
||||
translation_key="bypass_locked",
|
||||
metric_key="A_CYC_BYPASS_LOCKED",
|
||||
is_on_fn=lambda data: data.is_bypass_locked,
|
||||
set_fn=lambda client, on: client.set_bypass_locked(on),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ TEST_NAME = "Vallox"
|
||||
|
||||
# Values for the metrics fixture, see conftest.py.
|
||||
METRICS_UNIT_ON = {"A_CYC_MODE": 0}
|
||||
METRICS_UNIT_TESTING = {"A_CYC_MODE": 1}
|
||||
METRICS_UNIT_OFF = {"A_CYC_MODE": 5}
|
||||
|
||||
# The fan shows the unit's profiles as preset modes. Home, Away and Auto are
|
||||
|
||||
@@ -6,7 +6,6 @@ import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry, snapshot_platform
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
from vallox_websocket_api import (
|
||||
MetricValue,
|
||||
Profile,
|
||||
ValloxApiException,
|
||||
ValloxInvalidInputException,
|
||||
@@ -44,6 +43,7 @@ from . import (
|
||||
METRICS_PROFILE_UNKNOWN,
|
||||
METRICS_UNIT_OFF,
|
||||
METRICS_UNIT_ON,
|
||||
METRICS_UNIT_TESTING,
|
||||
setup_integration,
|
||||
)
|
||||
|
||||
@@ -69,6 +69,7 @@ async def test_all_entities(
|
||||
("metrics", "expected_state"),
|
||||
[
|
||||
pytest.param(METRICS_UNIT_ON, STATE_ON, id="on"),
|
||||
pytest.param(METRICS_UNIT_TESTING, STATE_ON, id="testing"),
|
||||
pytest.param(METRICS_UNIT_OFF, STATE_OFF, id="off"),
|
||||
],
|
||||
)
|
||||
@@ -131,19 +132,17 @@ async def test_fan_mode_missing(hass: HomeAssistant) -> None:
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
@pytest.mark.parametrize(
|
||||
("service", "metrics", "expected_called_with"),
|
||||
("service", "metrics", "expected_method"),
|
||||
[
|
||||
pytest.param(SERVICE_TURN_ON, METRICS_UNIT_OFF, METRICS_UNIT_ON, id="turn_on"),
|
||||
pytest.param(
|
||||
SERVICE_TURN_OFF, METRICS_UNIT_ON, METRICS_UNIT_OFF, id="turn_off"
|
||||
),
|
||||
pytest.param(SERVICE_TURN_ON, METRICS_UNIT_OFF, "switch_on", id="turn_on"),
|
||||
pytest.param(SERVICE_TURN_OFF, METRICS_UNIT_ON, "switch_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"
|
||||
SERVICE_TURN_ON, METRICS_UNIT_ON, "switch_on", id="turn_on_when_on"
|
||||
),
|
||||
pytest.param(
|
||||
SERVICE_TURN_OFF, METRICS_UNIT_OFF, METRICS_UNIT_OFF, id="turn_off_when_off"
|
||||
SERVICE_TURN_OFF, METRICS_UNIT_OFF, "switch_off", id="turn_off_when_off"
|
||||
),
|
||||
],
|
||||
)
|
||||
@@ -151,31 +150,36 @@ async def test_turn_on_off(
|
||||
hass: HomeAssistant,
|
||||
mock_vallox_client: AsyncMock,
|
||||
service: str,
|
||||
expected_called_with: dict[str, MetricValue],
|
||||
expected_method: str,
|
||||
) -> None:
|
||||
"""Test turn on/off."""
|
||||
await hass.services.async_call(
|
||||
FAN_DOMAIN, service, {ATTR_ENTITY_ID: FAN_ENTITY_ID}, blocking=True
|
||||
)
|
||||
|
||||
mock_vallox_client.set_values.assert_called_once_with(expected_called_with)
|
||||
getattr(mock_vallox_client, expected_method).assert_called_once_with()
|
||||
# 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")
|
||||
@pytest.mark.parametrize(
|
||||
("service", "metrics"),
|
||||
("service", "metrics", "failing_method"),
|
||||
[
|
||||
pytest.param(SERVICE_TURN_ON, METRICS_UNIT_OFF, id="turn_on"),
|
||||
pytest.param(SERVICE_TURN_OFF, METRICS_UNIT_ON, id="turn_off"),
|
||||
pytest.param(SERVICE_TURN_ON, METRICS_UNIT_OFF, "switch_on", id="turn_on"),
|
||||
pytest.param(SERVICE_TURN_OFF, METRICS_UNIT_ON, "switch_off", id="turn_off"),
|
||||
],
|
||||
)
|
||||
async def test_turn_on_off_exception(
|
||||
hass: HomeAssistant, mock_vallox_client: AsyncMock, service: str
|
||||
hass: HomeAssistant,
|
||||
mock_vallox_client: AsyncMock,
|
||||
service: str,
|
||||
failing_method: str,
|
||||
) -> None:
|
||||
"""Test turn on/off when the unit returns an error."""
|
||||
mock_vallox_client.set_values.side_effect = ValloxApiException("Fake failure")
|
||||
getattr(mock_vallox_client, failing_method).side_effect = ValloxApiException(
|
||||
"Fake failure"
|
||||
)
|
||||
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
@@ -193,7 +197,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.switch_on(),
|
||||
call.fetch_metric_data(),
|
||||
],
|
||||
id="preset_and_percentage_unit_off",
|
||||
@@ -204,7 +208,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.switch_on(),
|
||||
call.fetch_metric_data(),
|
||||
],
|
||||
id="preset_and_percentage_unit_on",
|
||||
@@ -214,7 +218,7 @@ async def test_turn_on_off_exception(
|
||||
{ATTR_PRESET_MODE: "away"},
|
||||
[
|
||||
call.set_profile(Profile.AWAY),
|
||||
call.set_values(METRICS_UNIT_ON),
|
||||
call.switch_on(),
|
||||
call.fetch_metric_data(),
|
||||
],
|
||||
id="preset_only",
|
||||
@@ -227,7 +231,7 @@ async def test_turn_on_off_exception(
|
||||
{ATTR_PRESET_MODE: "home"},
|
||||
[
|
||||
call.set_profile(Profile.HOME),
|
||||
call.set_values(METRICS_UNIT_ON),
|
||||
call.switch_on(),
|
||||
call.fetch_metric_data(),
|
||||
],
|
||||
id="active_preset",
|
||||
@@ -237,7 +241,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.switch_on(),
|
||||
call.fetch_metric_data(),
|
||||
],
|
||||
id="percentage_only",
|
||||
@@ -246,7 +250,7 @@ async def test_turn_on_off_exception(
|
||||
METRICS_UNIT_ON,
|
||||
{ATTR_PERCENTAGE: 0},
|
||||
[
|
||||
call.set_values(METRICS_UNIT_OFF),
|
||||
call.switch_off(),
|
||||
call.fetch_metric_data(),
|
||||
],
|
||||
id="zero_percentage",
|
||||
@@ -255,7 +259,7 @@ async def test_turn_on_off_exception(
|
||||
METRICS_UNIT_ON,
|
||||
{ATTR_PERCENTAGE: 0, ATTR_PRESET_MODE: "away"},
|
||||
[
|
||||
call.set_values(METRICS_UNIT_OFF),
|
||||
call.switch_off(),
|
||||
call.fetch_metric_data(),
|
||||
],
|
||||
id="zero_percentage_with_preset",
|
||||
@@ -264,7 +268,7 @@ async def test_turn_on_off_exception(
|
||||
METRICS_UNIT_OFF,
|
||||
{ATTR_PERCENTAGE: 0},
|
||||
[
|
||||
call.set_values(METRICS_UNIT_OFF),
|
||||
call.switch_off(),
|
||||
call.fetch_metric_data(),
|
||||
],
|
||||
id="zero_percentage_unit_off",
|
||||
@@ -312,11 +316,11 @@ async def test_turn_on_with_parameters(
|
||||
id="preset",
|
||||
),
|
||||
pytest.param(
|
||||
"set_values",
|
||||
"switch_on",
|
||||
[
|
||||
call.set_fan_speed(Profile.AWAY, 15),
|
||||
call.set_profile(Profile.AWAY),
|
||||
call.set_values(METRICS_UNIT_ON),
|
||||
call.switch_on(),
|
||||
call.fetch_metric_data(),
|
||||
],
|
||||
id="power",
|
||||
@@ -461,7 +465,7 @@ async def test_set_preset_mode_exception(
|
||||
"metrics",
|
||||
"percentage",
|
||||
"expected_set_fan_speed_calls",
|
||||
"expected_set_values_calls",
|
||||
"expected_switch_off_calls",
|
||||
),
|
||||
[
|
||||
pytest.param(METRICS_PROFILE_HOME, 40, [call(Profile.HOME, 40)], [], id="home"),
|
||||
@@ -469,9 +473,7 @@ async def test_set_preset_mode_exception(
|
||||
pytest.param(
|
||||
METRICS_PROFILE_BOOST, 60, [call(Profile.BOOST, 60)], [], id="boost"
|
||||
),
|
||||
pytest.param(
|
||||
METRICS_PROFILE_HOME, 0, [], [call(METRICS_UNIT_OFF)], id="turn_off"
|
||||
),
|
||||
pytest.param(METRICS_PROFILE_HOME, 0, [], [call()], id="turn_off"),
|
||||
],
|
||||
)
|
||||
async def test_set_percentage(
|
||||
@@ -479,7 +481,7 @@ async def test_set_percentage(
|
||||
mock_vallox_client: AsyncMock,
|
||||
percentage: int,
|
||||
expected_set_fan_speed_calls: list[_Call],
|
||||
expected_set_values_calls: list[_Call],
|
||||
expected_switch_off_calls: list[_Call],
|
||||
) -> None:
|
||||
"""Test set fan speed percentage."""
|
||||
await hass.services.async_call(
|
||||
@@ -492,7 +494,7 @@ async def test_set_percentage(
|
||||
assert (
|
||||
mock_vallox_client.set_fan_speed.call_args_list == expected_set_fan_speed_calls
|
||||
)
|
||||
assert mock_vallox_client.set_values.call_args_list == expected_set_values_calls
|
||||
assert mock_vallox_client.switch_off.call_args_list == expected_switch_off_calls
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
|
||||
@@ -18,6 +18,7 @@ from . import (
|
||||
METRICS_PROFILE_UNKNOWN,
|
||||
METRICS_UNIT_OFF,
|
||||
METRICS_UNIT_ON,
|
||||
METRICS_UNIT_TESTING,
|
||||
setup_integration,
|
||||
)
|
||||
|
||||
@@ -100,7 +101,9 @@ async def test_current_profile_sensor(hass: HomeAssistant, expected_state: str)
|
||||
[
|
||||
# The fan speeds come from metrics.json: 30%, 1310 rpm and 1270 rpm.
|
||||
pytest.param(METRICS_UNIT_ON, ("30", "1310", "1270"), id="unit_on"),
|
||||
# The unit keeps reporting its last fan speeds after it is turned off.
|
||||
pytest.param(METRICS_UNIT_TESTING, ("30", "1310", "1270"), id="testing"),
|
||||
# The unit keeps reporting its last fan speeds after it is switched
|
||||
# off, so the sensors report 0 instead.
|
||||
pytest.param(METRICS_UNIT_OFF, ("0", "0", "0"), id="unit_off"),
|
||||
# Without the power state, the reported fan speeds cannot be trusted.
|
||||
pytest.param(
|
||||
|
||||
@@ -58,14 +58,14 @@ async def test_bypass_locked_switch_state(
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
@pytest.mark.parametrize(
|
||||
("service", "value"),
|
||||
("service", "on"),
|
||||
[
|
||||
pytest.param(SERVICE_TURN_ON, 1, id="turn_on"),
|
||||
pytest.param(SERVICE_TURN_OFF, 0, id="turn_off"),
|
||||
pytest.param(SERVICE_TURN_ON, True, id="turn_on"),
|
||||
pytest.param(SERVICE_TURN_OFF, False, id="turn_off"),
|
||||
],
|
||||
)
|
||||
async def test_bypass_locked_switch_set(
|
||||
hass: HomeAssistant, mock_vallox_client: AsyncMock, service: str, value: int
|
||||
hass: HomeAssistant, mock_vallox_client: AsyncMock, service: str, on: bool
|
||||
) -> None:
|
||||
"""Test setting the bypass locked switch."""
|
||||
await hass.services.async_call(
|
||||
@@ -75,8 +75,6 @@ async def test_bypass_locked_switch_set(
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_vallox_client.set_values.assert_called_once_with(
|
||||
{"A_CYC_BYPASS_LOCKED": value}
|
||||
)
|
||||
mock_vallox_client.set_bypass_locked.assert_called_once_with(on)
|
||||
# One fetch at setup, and one refresh after the change.
|
||||
assert mock_vallox_client.fetch_metric_data.call_count == 2
|
||||
|
||||
Reference in New Issue
Block a user