Calculate _attr_native_value when no restore state and appliance is running in Whirlpool (#88559)

* Initialize _attr_native_value when running

* Fix return type on update_sensor_state()
move init at startup if _attr_native_value is None

* allow update _attr_native_value when running and none
This commit is contained in:
mkmer 2023-05-30 21:48:39 -04:00 committed by GitHub
parent fba826ae9e
commit bfec3d68dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View File

@ -290,12 +290,15 @@ class WasherDryerTimeClass(RestoreSensor):
if machine_state is MachineState.RunningMainCycle: if machine_state is MachineState.RunningMainCycle:
self._running = True self._running = True
new_timestamp = now + timedelta( new_timestamp = now + timedelta(
seconds=int(self._wd.get_attribute("Cavity_TimeStatusEstTimeRemaining")) seconds=int(self._wd.get_attribute("Cavity_TimeStatusEstTimeRemaining"))
) )
if isinstance(self._attr_native_value, datetime) and abs( if (
new_timestamp - self._attr_native_value isinstance(self._attr_native_value, datetime)
) > timedelta(seconds=60): and abs(new_timestamp - self._attr_native_value) > timedelta(seconds=60)
or self._attr_native_value is None
):
self._attr_native_value = new_timestamp self._attr_native_value = new_timestamp
self._async_write_ha_state() self._async_write_ha_state()

View File

@ -17,7 +17,7 @@ async def update_sensor_state(
hass: HomeAssistant, hass: HomeAssistant,
entity_id: str, entity_id: str,
mock_sensor_api_instance: MagicMock, mock_sensor_api_instance: MagicMock,
) -> None: ) -> State:
"""Simulate an update trigger from the API.""" """Simulate an update trigger from the API."""
for call in mock_sensor_api_instance.register_attr_callback.call_args_list: for call in mock_sensor_api_instance.register_attr_callback.call_args_list:
@ -300,6 +300,23 @@ async def test_restore_state(
assert state.state == thetimestamp.isoformat() assert state.state == thetimestamp.isoformat()
async def test_no_restore_state(
hass: HomeAssistant,
mock_sensor_api_instances: MagicMock,
mock_sensor1_api: MagicMock,
) -> None:
"""Test sensor restore state with no restore."""
# create and add entry
entity_id = "sensor.washer_end_time"
await init_integration(hass)
# restore from cache
state = hass.states.get(entity_id)
state.state = "unknown"
state = await update_sensor_state(hass, entity_id, mock_sensor1_api)
state.state = datetime.now().isoformat()
async def test_callback( async def test_callback(
hass: HomeAssistant, hass: HomeAssistant,
mock_sensor_api_instances: MagicMock, mock_sensor_api_instances: MagicMock,