Skip to content

Commit baf4382

Browse files
authored
Miele consumption sensors consistent behavior with RestoreSensor (home-assistant#151098)
1 parent 8263ea4 commit baf4382

File tree

3 files changed

+131
-8
lines changed

3 files changed

+131
-8
lines changed

homeassistant/components/miele/sensor.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ class MieleSensorDefinition:
270270
device_class=SensorDeviceClass.ENERGY,
271271
state_class=SensorStateClass.TOTAL_INCREASING,
272272
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
273+
suggested_display_precision=1,
273274
entity_category=EntityCategory.DIAGNOSTIC,
274275
),
275276
),
@@ -307,6 +308,7 @@ class MieleSensorDefinition:
307308
device_class=SensorDeviceClass.WATER,
308309
state_class=SensorStateClass.TOTAL_INCREASING,
309310
native_unit_of_measurement=UnitOfVolume.LITERS,
311+
suggested_display_precision=0,
310312
entity_category=EntityCategory.DIAGNOSTIC,
311313
),
312314
),
@@ -618,6 +620,8 @@ def _get_entity_class(definition: MieleSensorDefinition) -> type[MieleSensor]:
618620
"state_elapsed_time": MieleTimeSensor,
619621
"state_remaining_time": MieleTimeSensor,
620622
"state_start_time": MieleTimeSensor,
623+
"current_energy_consumption": MieleConsumptionSensor,
624+
"current_water_consumption": MieleConsumptionSensor,
621625
}.get(definition.description.key, MieleSensor)
622626

623627
def _is_entity_registered(unique_id: str) -> bool:
@@ -924,3 +928,58 @@ def _update_last_value(self) -> None:
924928
# otherwise, cache value and return it
925929
else:
926930
self._last_value = current_value
931+
932+
933+
class MieleConsumptionSensor(MieleRestorableSensor):
934+
"""Representation of consumption sensors keeping state from cache."""
935+
936+
_is_reporting: bool = False
937+
938+
def _update_last_value(self) -> None:
939+
"""Update the last value of the sensor."""
940+
current_value = self.entity_description.value_fn(self.device)
941+
current_status = StateStatus(self.device.state_status)
942+
last_value = (
943+
float(cast(str, self._last_value))
944+
if self._last_value is not None and self._last_value != STATE_UNKNOWN
945+
else 0
946+
)
947+
948+
# force unknown when appliance is not able to report consumption
949+
if current_status in (
950+
StateStatus.ON,
951+
StateStatus.OFF,
952+
StateStatus.PROGRAMMED,
953+
StateStatus.WAITING_TO_START,
954+
StateStatus.IDLE,
955+
StateStatus.SERVICE,
956+
):
957+
self._is_reporting = False
958+
self._last_value = None
959+
960+
# appliance might report the last value for consumption of previous cycle and it will report 0
961+
# only after a while, so it is necessary to force 0 until we see the 0 value coming from API, unless
962+
# we already saw a valid value in this cycle from cache
963+
elif (
964+
current_status in (StateStatus.IN_USE, StateStatus.PAUSE)
965+
and not self._is_reporting
966+
and last_value > 0
967+
):
968+
self._last_value = current_value
969+
self._is_reporting = True
970+
971+
elif (
972+
current_status in (StateStatus.IN_USE, StateStatus.PAUSE)
973+
and not self._is_reporting
974+
and current_value is not None
975+
and cast(int, current_value) > 0
976+
):
977+
self._last_value = 0
978+
979+
# keep value when program ends
980+
elif current_status == StateStatus.PROGRAM_ENDED:
981+
pass
982+
983+
else:
984+
self._last_value = current_value
985+
self._is_reporting = True

tests/components/miele/snapshots/test_sensor.ambr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3904,7 +3904,7 @@
39043904
'name': None,
39053905
'options': dict({
39063906
'sensor': dict({
3907-
'suggested_display_precision': 2,
3907+
'suggested_display_precision': 1,
39083908
}),
39093909
}),
39103910
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
@@ -3932,7 +3932,7 @@
39323932
'last_changed': <ANY>,
39333933
'last_reported': <ANY>,
39343934
'last_updated': <ANY>,
3935-
'state': '0.0',
3935+
'state': 'unknown',
39363936
})
39373937
# ---
39383938
# name: test_sensor_states[platforms0][sensor.washing_machine_energy_forecast-entry]
@@ -4501,7 +4501,7 @@
45014501
'name': None,
45024502
'options': dict({
45034503
'sensor': dict({
4504-
'suggested_display_precision': 2,
4504+
'suggested_display_precision': 0,
45054505
}),
45064506
}),
45074507
'original_device_class': <SensorDeviceClass.WATER: 'water'>,
@@ -4529,7 +4529,7 @@
45294529
'last_changed': <ANY>,
45304530
'last_reported': <ANY>,
45314531
'last_updated': <ANY>,
4532-
'state': '0.0',
4532+
'state': 'unknown',
45334533
})
45344534
# ---
45354535
# name: test_sensor_states[platforms0][sensor.washing_machine_water_forecast-entry]
@@ -6050,7 +6050,7 @@
60506050
'name': None,
60516051
'options': dict({
60526052
'sensor': dict({
6053-
'suggested_display_precision': 2,
6053+
'suggested_display_precision': 1,
60546054
}),
60556055
}),
60566056
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
@@ -6078,7 +6078,7 @@
60786078
'last_changed': <ANY>,
60796079
'last_reported': <ANY>,
60806080
'last_updated': <ANY>,
6081-
'state': '0.0',
6081+
'state': 'unknown',
60826082
})
60836083
# ---
60846084
# name: test_sensor_states_api_push[platforms0][sensor.washing_machine_energy_forecast-entry]
@@ -6647,7 +6647,7 @@
66476647
'name': None,
66486648
'options': dict({
66496649
'sensor': dict({
6650-
'suggested_display_precision': 2,
6650+
'suggested_display_precision': 0,
66516651
}),
66526652
}),
66536653
'original_device_class': <SensorDeviceClass.WATER: 'water'>,
@@ -6675,7 +6675,7 @@
66756675
'last_changed': <ANY>,
66766676
'last_reported': <ANY>,
66776677
'last_updated': <ANY>,
6678-
'state': '0.0',
6678+
'state': 'unknown',
66796679
})
66806680
# ---
66816681
# name: test_sensor_states_api_push[platforms0][sensor.washing_machine_water_forecast-entry]

tests/components/miele/test_sensor.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,13 @@ async def test_laundry_wash_scenario(
315315
check_sensor_state(hass, "sensor.washing_machine_remaining_time", "unknown", step)
316316
# OFF -> elapsed forced to unknown (some devices continue reporting last value of last cycle)
317317
check_sensor_state(hass, "sensor.washing_machine_elapsed_time", "unknown", step)
318+
# consumption sensors have to report "unknown" when the device is not working
319+
check_sensor_state(
320+
hass, "sensor.washing_machine_energy_consumption", "unknown", step
321+
)
322+
check_sensor_state(
323+
hass, "sensor.washing_machine_water_consumption", "unknown", step
324+
)
318325

319326
# Simulate program started
320327
device_fixture["DummyWasher"]["state"]["status"]["value_raw"] = 5
@@ -337,10 +344,41 @@ async def test_laundry_wash_scenario(
337344
device_fixture["DummyWasher"]["state"]["elapsedTime"][1] = 12
338345
device_fixture["DummyWasher"]["state"]["spinningSpeed"]["value_raw"] = 1200
339346
device_fixture["DummyWasher"]["state"]["spinningSpeed"]["value_localized"] = "1200"
347+
device_fixture["DummyWasher"]["state"]["ecoFeedback"] = {
348+
"currentEnergyConsumption": {
349+
"value": 0.9,
350+
"unit": "kWh",
351+
},
352+
"currentWaterConsumption": {
353+
"value": 52,
354+
"unit": "l",
355+
},
356+
}
340357

341358
freezer.tick(timedelta(seconds=130))
342359
async_fire_time_changed(hass)
343360
await hass.async_block_till_done()
361+
362+
# at this point, appliance is working, but it started reporting a value from last cycle, so it is forced to 0
363+
check_sensor_state(hass, "sensor.washing_machine_energy_consumption", "0", step)
364+
check_sensor_state(hass, "sensor.washing_machine_water_consumption", "0", step)
365+
366+
# intermediate step, only to report new consumption values
367+
device_fixture["DummyWasher"]["state"]["ecoFeedback"] = {
368+
"currentEnergyConsumption": {
369+
"value": 0.0,
370+
"unit": "kWh",
371+
},
372+
"currentWaterConsumption": {
373+
"value": 0,
374+
"unit": "l",
375+
},
376+
}
377+
378+
freezer.tick(timedelta(seconds=130))
379+
async_fire_time_changed(hass)
380+
await hass.async_block_till_done()
381+
344382
step += 1
345383

346384
check_sensor_state(hass, "sensor.washing_machine", "in_use", step)
@@ -351,6 +389,28 @@ async def test_laundry_wash_scenario(
351389
# IN_USE -> elapsed, remaining time from API (normal case)
352390
check_sensor_state(hass, "sensor.washing_machine_remaining_time", "105", step)
353391
check_sensor_state(hass, "sensor.washing_machine_elapsed_time", "12", step)
392+
check_sensor_state(hass, "sensor.washing_machine_energy_consumption", "0.0", step)
393+
check_sensor_state(hass, "sensor.washing_machine_water_consumption", "0", step)
394+
395+
# intermediate step, only to report new consumption values
396+
device_fixture["DummyWasher"]["state"]["ecoFeedback"] = {
397+
"currentEnergyConsumption": {
398+
"value": 0.1,
399+
"unit": "kWh",
400+
},
401+
"currentWaterConsumption": {
402+
"value": 7,
403+
"unit": "l",
404+
},
405+
}
406+
407+
freezer.tick(timedelta(seconds=130))
408+
async_fire_time_changed(hass)
409+
await hass.async_block_till_done()
410+
411+
# at this point, it starts reporting value from API
412+
check_sensor_state(hass, "sensor.washing_machine_energy_consumption", "0.1", step)
413+
check_sensor_state(hass, "sensor.washing_machine_water_consumption", "7", step)
354414

355415
# Simulate rinse hold phase
356416
device_fixture["DummyWasher"]["state"]["status"]["value_raw"] = 11
@@ -389,6 +449,7 @@ async def test_laundry_wash_scenario(
389449
device_fixture["DummyWasher"]["state"]["remainingTime"][1] = 0
390450
device_fixture["DummyWasher"]["state"]["elapsedTime"][0] = 0
391451
device_fixture["DummyWasher"]["state"]["elapsedTime"][1] = 0
452+
device_fixture["DummyWasher"]["state"]["ecoFeedback"] = None
392453

393454
freezer.tick(timedelta(seconds=130))
394455
async_fire_time_changed(hass)
@@ -406,6 +467,9 @@ async def test_laundry_wash_scenario(
406467
check_sensor_state(hass, "sensor.washing_machine_remaining_time", "0", step)
407468
# PROGRAM_ENDED -> elapsed time kept from last program (some devices immediately go to 0)
408469
check_sensor_state(hass, "sensor.washing_machine_elapsed_time", "109", step)
470+
# consumption values now are reporting last known value, API might start reporting null object
471+
check_sensor_state(hass, "sensor.washing_machine_energy_consumption", "0.1", step)
472+
check_sensor_state(hass, "sensor.washing_machine_water_consumption", "7", step)
409473

410474
# Simulate when door is opened after program ended
411475
device_fixture["DummyWasher"]["state"]["status"]["value_raw"] = 3

0 commit comments

Comments
 (0)