Use None for Unknown state in Whirlpool sensor (#143582)

This commit is contained in:
Abílio Costa 2025-04-24 15:12:45 +01:00 committed by GitHub
parent 993ebc9eba
commit f86e85b931
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 4 deletions

View File

@ -25,7 +25,7 @@ from .entity import WhirlpoolEntity
SCAN_INTERVAL = timedelta(minutes=5)
WASHER_TANK_FILL = {
0: "unknown",
0: None,
1: "empty",
2: "25",
3: "50",
@ -120,7 +120,7 @@ WASHER_SENSORS: tuple[WhirlpoolSensorEntityDescription, ...] = (
translation_key="whirlpool_tank",
entity_registry_enabled_default=False,
device_class=SensorDeviceClass.ENUM,
options=list(WASHER_TANK_FILL.values()),
options=[value for value in WASHER_TANK_FILL.values() if value],
value_fn=lambda washer: WASHER_TANK_FILL.get(washer.get_dispense_1_level()),
),
)

View File

@ -160,7 +160,6 @@
'area_id': None,
'capabilities': dict({
'options': list([
'unknown',
'empty',
'25',
'50',
@ -202,7 +201,6 @@
'device_class': 'enum',
'friendly_name': 'Washer Detergent level',
'options': list([
'unknown',
'empty',
'25',
'50',

View File

@ -299,3 +299,44 @@ async def test_washer_dryer_door_open_state(
await trigger_attr_callback(hass, mock_instance)
state = hass.states.get(entity_id)
assert state.state == "running_maincycle"
@pytest.mark.parametrize(
("entity_id", "mock_fixture", "mock_method_name", "values"),
[
(
"sensor.washer_detergent_level",
"mock_washer_api",
"get_dispense_1_level",
[
(0, STATE_UNKNOWN),
(1, "empty"),
(2, "25"),
(3, "50"),
(4, "100"),
(5, "active"),
],
),
],
)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_simple_enum_sensors(
hass: HomeAssistant,
entity_id: str,
mock_fixture: str,
mock_method_name: str,
values: list[tuple[int, str]],
request: pytest.FixtureRequest,
) -> None:
"""Test simple enum sensors where state maps directly from a single API value."""
await init_integration(hass)
mock_instance = request.getfixturevalue(mock_fixture)
mock_method = getattr(mock_instance, mock_method_name)
for raw_value, expected_state in values:
mock_method.return_value = raw_value
await trigger_attr_callback(hass, mock_instance)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == expected_state