Miele consumption sensors consistent behavior with RestoreSensor (#151098)

This commit is contained in:
Andrea Turri
2025-09-12 11:17:10 +02:00
committed by GitHub
parent 8263ea4a4a
commit baf4382724
3 changed files with 131 additions and 8 deletions
+59
View File
@@ -270,6 +270,7 @@ SENSOR_TYPES: Final[tuple[MieleSensorDefinition, ...]] = (
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
suggested_display_precision=1,
entity_category=EntityCategory.DIAGNOSTIC,
),
),
@@ -307,6 +308,7 @@ SENSOR_TYPES: Final[tuple[MieleSensorDefinition, ...]] = (
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfVolume.LITERS,
suggested_display_precision=0,
entity_category=EntityCategory.DIAGNOSTIC,
),
),
@@ -618,6 +620,8 @@ async def async_setup_entry(
"state_elapsed_time": MieleTimeSensor,
"state_remaining_time": MieleTimeSensor,
"state_start_time": MieleTimeSensor,
"current_energy_consumption": MieleConsumptionSensor,
"current_water_consumption": MieleConsumptionSensor,
}.get(definition.description.key, MieleSensor)
def _is_entity_registered(unique_id: str) -> bool:
@@ -924,3 +928,58 @@ class MieleTimeSensor(MieleRestorableSensor):
# otherwise, cache value and return it
else:
self._last_value = current_value
class MieleConsumptionSensor(MieleRestorableSensor):
"""Representation of consumption sensors keeping state from cache."""
_is_reporting: bool = False
def _update_last_value(self) -> None:
"""Update the last value of the sensor."""
current_value = self.entity_description.value_fn(self.device)
current_status = StateStatus(self.device.state_status)
last_value = (
float(cast(str, self._last_value))
if self._last_value is not None and self._last_value != STATE_UNKNOWN
else 0
)
# force unknown when appliance is not able to report consumption
if current_status in (
StateStatus.ON,
StateStatus.OFF,
StateStatus.PROGRAMMED,
StateStatus.WAITING_TO_START,
StateStatus.IDLE,
StateStatus.SERVICE,
):
self._is_reporting = False
self._last_value = None
# appliance might report the last value for consumption of previous cycle and it will report 0
# only after a while, so it is necessary to force 0 until we see the 0 value coming from API, unless
# we already saw a valid value in this cycle from cache
elif (
current_status in (StateStatus.IN_USE, StateStatus.PAUSE)
and not self._is_reporting
and last_value > 0
):
self._last_value = current_value
self._is_reporting = True
elif (
current_status in (StateStatus.IN_USE, StateStatus.PAUSE)
and not self._is_reporting
and current_value is not None
and cast(int, current_value) > 0
):
self._last_value = 0
# keep value when program ends
elif current_status == StateStatus.PROGRAM_ENDED:
pass
else:
self._last_value = current_value
self._is_reporting = True
@@ -3904,7 +3904,7 @@
'name': None,
'options': dict({
'sensor': dict({
'suggested_display_precision': 2,
'suggested_display_precision': 1,
}),
}),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
@@ -3932,7 +3932,7 @@
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '0.0',
'state': 'unknown',
})
# ---
# name: test_sensor_states[platforms0][sensor.washing_machine_energy_forecast-entry]
@@ -4501,7 +4501,7 @@
'name': None,
'options': dict({
'sensor': dict({
'suggested_display_precision': 2,
'suggested_display_precision': 0,
}),
}),
'original_device_class': <SensorDeviceClass.WATER: 'water'>,
@@ -4529,7 +4529,7 @@
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '0.0',
'state': 'unknown',
})
# ---
# name: test_sensor_states[platforms0][sensor.washing_machine_water_forecast-entry]
@@ -6050,7 +6050,7 @@
'name': None,
'options': dict({
'sensor': dict({
'suggested_display_precision': 2,
'suggested_display_precision': 1,
}),
}),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
@@ -6078,7 +6078,7 @@
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '0.0',
'state': 'unknown',
})
# ---
# name: test_sensor_states_api_push[platforms0][sensor.washing_machine_energy_forecast-entry]
@@ -6647,7 +6647,7 @@
'name': None,
'options': dict({
'sensor': dict({
'suggested_display_precision': 2,
'suggested_display_precision': 0,
}),
}),
'original_device_class': <SensorDeviceClass.WATER: 'water'>,
@@ -6675,7 +6675,7 @@
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '0.0',
'state': 'unknown',
})
# ---
# name: test_sensor_states_api_push[platforms0][sensor.washing_machine_water_forecast-entry]
+64
View File
@@ -315,6 +315,13 @@ async def test_laundry_wash_scenario(
check_sensor_state(hass, "sensor.washing_machine_remaining_time", "unknown", step)
# OFF -> elapsed forced to unknown (some devices continue reporting last value of last cycle)
check_sensor_state(hass, "sensor.washing_machine_elapsed_time", "unknown", step)
# consumption sensors have to report "unknown" when the device is not working
check_sensor_state(
hass, "sensor.washing_machine_energy_consumption", "unknown", step
)
check_sensor_state(
hass, "sensor.washing_machine_water_consumption", "unknown", step
)
# Simulate program started
device_fixture["DummyWasher"]["state"]["status"]["value_raw"] = 5
@@ -337,10 +344,41 @@ async def test_laundry_wash_scenario(
device_fixture["DummyWasher"]["state"]["elapsedTime"][1] = 12
device_fixture["DummyWasher"]["state"]["spinningSpeed"]["value_raw"] = 1200
device_fixture["DummyWasher"]["state"]["spinningSpeed"]["value_localized"] = "1200"
device_fixture["DummyWasher"]["state"]["ecoFeedback"] = {
"currentEnergyConsumption": {
"value": 0.9,
"unit": "kWh",
},
"currentWaterConsumption": {
"value": 52,
"unit": "l",
},
}
freezer.tick(timedelta(seconds=130))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# at this point, appliance is working, but it started reporting a value from last cycle, so it is forced to 0
check_sensor_state(hass, "sensor.washing_machine_energy_consumption", "0", step)
check_sensor_state(hass, "sensor.washing_machine_water_consumption", "0", step)
# intermediate step, only to report new consumption values
device_fixture["DummyWasher"]["state"]["ecoFeedback"] = {
"currentEnergyConsumption": {
"value": 0.0,
"unit": "kWh",
},
"currentWaterConsumption": {
"value": 0,
"unit": "l",
},
}
freezer.tick(timedelta(seconds=130))
async_fire_time_changed(hass)
await hass.async_block_till_done()
step += 1
check_sensor_state(hass, "sensor.washing_machine", "in_use", step)
@@ -351,6 +389,28 @@ async def test_laundry_wash_scenario(
# IN_USE -> elapsed, remaining time from API (normal case)
check_sensor_state(hass, "sensor.washing_machine_remaining_time", "105", step)
check_sensor_state(hass, "sensor.washing_machine_elapsed_time", "12", step)
check_sensor_state(hass, "sensor.washing_machine_energy_consumption", "0.0", step)
check_sensor_state(hass, "sensor.washing_machine_water_consumption", "0", step)
# intermediate step, only to report new consumption values
device_fixture["DummyWasher"]["state"]["ecoFeedback"] = {
"currentEnergyConsumption": {
"value": 0.1,
"unit": "kWh",
},
"currentWaterConsumption": {
"value": 7,
"unit": "l",
},
}
freezer.tick(timedelta(seconds=130))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# at this point, it starts reporting value from API
check_sensor_state(hass, "sensor.washing_machine_energy_consumption", "0.1", step)
check_sensor_state(hass, "sensor.washing_machine_water_consumption", "7", step)
# Simulate rinse hold phase
device_fixture["DummyWasher"]["state"]["status"]["value_raw"] = 11
@@ -389,6 +449,7 @@ async def test_laundry_wash_scenario(
device_fixture["DummyWasher"]["state"]["remainingTime"][1] = 0
device_fixture["DummyWasher"]["state"]["elapsedTime"][0] = 0
device_fixture["DummyWasher"]["state"]["elapsedTime"][1] = 0
device_fixture["DummyWasher"]["state"]["ecoFeedback"] = None
freezer.tick(timedelta(seconds=130))
async_fire_time_changed(hass)
@@ -406,6 +467,9 @@ async def test_laundry_wash_scenario(
check_sensor_state(hass, "sensor.washing_machine_remaining_time", "0", step)
# PROGRAM_ENDED -> elapsed time kept from last program (some devices immediately go to 0)
check_sensor_state(hass, "sensor.washing_machine_elapsed_time", "109", step)
# consumption values now are reporting last known value, API might start reporting null object
check_sensor_state(hass, "sensor.washing_machine_energy_consumption", "0.1", step)
check_sensor_state(hass, "sensor.washing_machine_water_consumption", "7", step)
# Simulate when door is opened after program ended
device_fixture["DummyWasher"]["state"]["status"]["value_raw"] = 3