diff --git a/homeassistant/components/whirlpool/sensor.py b/homeassistant/components/whirlpool/sensor.py index 9c26a0319f7..5b83c35cca4 100644 --- a/homeassistant/components/whirlpool/sensor.py +++ b/homeassistant/components/whirlpool/sensor.py @@ -290,12 +290,15 @@ class WasherDryerTimeClass(RestoreSensor): if machine_state is MachineState.RunningMainCycle: self._running = True + new_timestamp = now + timedelta( seconds=int(self._wd.get_attribute("Cavity_TimeStatusEstTimeRemaining")) ) - if isinstance(self._attr_native_value, datetime) and abs( - new_timestamp - self._attr_native_value - ) > timedelta(seconds=60): + if ( + isinstance(self._attr_native_value, datetime) + 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._async_write_ha_state() diff --git a/tests/components/whirlpool/test_sensor.py b/tests/components/whirlpool/test_sensor.py index 429e8895ad8..063ebd49c4c 100644 --- a/tests/components/whirlpool/test_sensor.py +++ b/tests/components/whirlpool/test_sensor.py @@ -17,7 +17,7 @@ async def update_sensor_state( hass: HomeAssistant, entity_id: str, mock_sensor_api_instance: MagicMock, -) -> None: +) -> State: """Simulate an update trigger from the API.""" 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() +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( hass: HomeAssistant, mock_sensor_api_instances: MagicMock,