diff --git a/homeassistant/components/history_stats/sensor.py b/homeassistant/components/history_stats/sensor.py index 5b1242423c74b..958f46a5e04e2 100644 --- a/homeassistant/components/history_stats/sensor.py +++ b/homeassistant/components/history_stats/sensor.py @@ -163,6 +163,7 @@ def __init__( self._process_update() if self._type == CONF_TYPE_TIME: self._attr_device_class = SensorDeviceClass.DURATION + self._attr_suggested_display_precision = 2 @callback def _process_update(self) -> None: @@ -173,7 +174,7 @@ def _process_update(self) -> None: return if self._type == CONF_TYPE_TIME: - self._attr_native_value = round(state.seconds_matched / 3600, 2) + self._attr_native_value = state.seconds_matched / 3600 elif self._type == CONF_TYPE_RATIO: self._attr_native_value = pretty_ratio(state.seconds_matched, state.period) elif self._type == CONF_TYPE_COUNT: diff --git a/tests/components/history_stats/test_sensor.py b/tests/components/history_stats/test_sensor.py index 28e24b587aaa8..ddd11c0d7683d 100644 --- a/tests/components/history_stats/test_sensor.py +++ b/tests/components/history_stats/test_sensor.py @@ -334,7 +334,7 @@ def _fake_states(*args, **kwargs): await async_update_entity(hass, f"sensor.sensor{i}") await hass.async_block_till_done() - assert hass.states.get("sensor.sensor1").state == "0.5" + assert round(float(hass.states.get("sensor.sensor1").state), 3) == 0.5 assert hass.states.get("sensor.sensor2").state == "0.0" assert hass.states.get("sensor.sensor3").state == "2" assert hass.states.get("sensor.sensor4").state == "50.0" @@ -413,8 +413,8 @@ def _fake_states(*args, **kwargs): await async_update_entity(hass, f"sensor.sensor{i}") await hass.async_block_till_done() - assert hass.states.get("sensor.sensor1").state == "0.83" - assert hass.states.get("sensor.sensor2").state == "0.83" + assert hass.states.get("sensor.sensor1").state == "0.833333333333333" + assert hass.states.get("sensor.sensor2").state == "0.833333333333333" assert hass.states.get("sensor.sensor3").state == "2" assert hass.states.get("sensor.sensor4").state == "83.3" @@ -769,7 +769,7 @@ def _fake_states(*args, **kwargs): async_fire_time_changed(hass, next_update_time) await hass.async_block_till_done() - assert hass.states.get("sensor.sensor1").state == "1.53" + assert hass.states.get("sensor.sensor1").state == "1.53333333333333" end_time = start_time + timedelta(minutes=120) with freeze_time(end_time): @@ -1011,7 +1011,7 @@ def _fake_states(*args, **kwargs): async_fire_time_changed(hass, in_the_window) await hass.async_block_till_done() - assert hass.states.get("sensor.sensor1").state == "0.08" + assert hass.states.get("sensor.sensor1").state == "0.0833333333333333" past_the_window = start_time + timedelta(hours=25) with patch( @@ -1175,8 +1175,8 @@ def _fake_states(*args, **kwargs): await async_update_entity(hass, f"sensor.sensor{i}") await hass.async_block_till_done() - assert hass.states.get("sensor.sensor1").state == "0.83" - assert hass.states.get("sensor.sensor2").state == "0.83" + assert hass.states.get("sensor.sensor1").state == "0.833333333333333" + assert hass.states.get("sensor.sensor2").state == "0.833333333333333" assert hass.states.get("sensor.sensor3").state == "2" assert hass.states.get("sensor.sensor4").state == "41.7" @@ -1188,8 +1188,8 @@ def _fake_states(*args, **kwargs): async_fire_time_changed(hass, past_next_update) await hass.async_block_till_done() - assert hass.states.get("sensor.sensor1").state == "0.83" - assert hass.states.get("sensor.sensor2").state == "0.83" + assert hass.states.get("sensor.sensor1").state == "0.833333333333333" + assert hass.states.get("sensor.sensor2").state == "0.833333333333333" assert hass.states.get("sensor.sensor3").state == "2" assert hass.states.get("sensor.sensor4").state == "41.7" @@ -1269,8 +1269,8 @@ def _fake_states(*args, **kwargs): await async_update_entity(hass, f"sensor.sensor{i}") await hass.async_block_till_done() - assert hass.states.get("sensor.sensor1").state == "0.83" - assert hass.states.get("sensor.sensor2").state == "0.83" + assert hass.states.get("sensor.sensor1").state == "0.833333333333333" + assert hass.states.get("sensor.sensor2").state == "0.833333333333333" assert hass.states.get("sensor.sensor3").state == "2" assert hass.states.get("sensor.sensor4").state == "83.3" @@ -1282,8 +1282,8 @@ def _fake_states(*args, **kwargs): async_fire_time_changed(hass, past_next_update) await hass.async_block_till_done() - assert hass.states.get("sensor.sensor1").state == "0.83" - assert hass.states.get("sensor.sensor2").state == "0.83" + assert hass.states.get("sensor.sensor1").state == "0.833333333333333" + assert hass.states.get("sensor.sensor2").state == "0.833333333333333" assert hass.states.get("sensor.sensor3").state == "2" assert hass.states.get("sensor.sensor4").state == "83.3" @@ -1362,8 +1362,8 @@ def _fake_states(*args, **kwargs): await async_update_entity(hass, f"sensor.sensor{i}") await hass.async_block_till_done() - assert hass.states.get("sensor.sensor1").state == "0.83" - assert hass.states.get("sensor.sensor2").state == "0.83" + assert hass.states.get("sensor.sensor1").state == "0.833333333333333" + assert hass.states.get("sensor.sensor2").state == "0.833333333333333" assert hass.states.get("sensor.sensor3").state == "2" assert hass.states.get("sensor.sensor4").state == "83.3" @@ -1431,10 +1431,16 @@ def _fake_states(*args, **kwargs): await hass.async_block_till_done() await async_update_entity(hass, "sensor.heatpump_compressor_today") await hass.async_block_till_done() - assert hass.states.get("sensor.heatpump_compressor_today").state == "1.83" + assert ( + hass.states.get("sensor.heatpump_compressor_today").state + == "1.83333333333333" + ) async_fire_time_changed(hass, time_200) await hass.async_block_till_done() - assert hass.states.get("sensor.heatpump_compressor_today").state == "1.83" + assert ( + hass.states.get("sensor.heatpump_compressor_today").state + == "1.83333333333333" + ) hass.states.async_set("binary_sensor.heatpump_compressor_state", "off") await hass.async_block_till_done() @@ -1442,14 +1448,20 @@ def _fake_states(*args, **kwargs): with freeze_time(time_400): async_fire_time_changed(hass, time_400) await hass.async_block_till_done() - assert hass.states.get("sensor.heatpump_compressor_today").state == "1.83" + assert ( + hass.states.get("sensor.heatpump_compressor_today").state + == "1.83333333333333" + ) hass.states.async_set("binary_sensor.heatpump_compressor_state", "on") await async_wait_recording_done(hass) time_600 = start_of_today + timedelta(hours=6) with freeze_time(time_600): async_fire_time_changed(hass, time_600) await hass.async_block_till_done() - assert hass.states.get("sensor.heatpump_compressor_today").state == "3.83" + assert ( + hass.states.get("sensor.heatpump_compressor_today").state + == "3.83333333333333" + ) rolled_to_next_day = start_of_today + timedelta(days=1) assert rolled_to_next_day.hour == 0 @@ -1491,7 +1503,10 @@ def _fake_states(*args, **kwargs): with freeze_time(rolled_to_next_day_plus_18): async_fire_time_changed(hass, rolled_to_next_day_plus_18) await hass.async_block_till_done() - assert hass.states.get("sensor.heatpump_compressor_today").state == "16.0" + assert ( + hass.states.get("sensor.heatpump_compressor_today").state + == "16.0002388888929" + ) async def test_device_classes(recorder_mock: Recorder, hass: HomeAssistant) -> None: