Skip to content

Commit

Permalink
Stop rounding history_stats sensor (home-assistant#97195)
Browse files Browse the repository at this point in the history
  • Loading branch information
amosyuen authored Jul 26, 2023
1 parent 70b1083 commit d0512d5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
3 changes: 2 additions & 1 deletion homeassistant/components/history_stats/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
55 changes: 35 additions & 20 deletions tests/components/history_stats/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"

Expand All @@ -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"

Expand Down Expand Up @@ -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"

Expand All @@ -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"

Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -1431,25 +1431,37 @@ 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()

time_400 = start_of_today + timedelta(hours=4)
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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d0512d5

Please sign in to comment.