Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 10 commits into from
May 31, 2023
9 changes: 6 additions & 3 deletions homeassistant/components/whirlpool/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,15 @@ def update_from_latest_data(self) -> None:

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more efficient to start with the None check as that is faster than isinstance and Python conditions will short circuit.

):
self._attr_native_value = new_timestamp
self._async_write_ha_state()
19 changes: 18 additions & 1 deletion tests/components/whirlpool/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be an assertion?


state = await update_sensor_state(hass, entity_id, mock_sensor1_api)
state.state = datetime.now().isoformat()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too?



async def test_callback(
hass: HomeAssistant,
mock_sensor_api_instances: MagicMock,
Expand Down