Don't round in energy cost sensor (#56258)

This commit is contained in:
Erik Montnemery 2021-09-27 12:01:17 +02:00 committed by GitHub
parent 56b94d6809
commit efe467217a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -221,7 +221,6 @@ class EnergyCostSensor(SensorEntity):
self._attr_state_class = STATE_CLASS_TOTAL self._attr_state_class = STATE_CLASS_TOTAL
self._config = config self._config = config
self._last_energy_sensor_state: State | None = None self._last_energy_sensor_state: State | None = None
self._cur_value = 0.0
# add_finished is set when either of async_added_to_hass or add_to_platform_abort # add_finished is set when either of async_added_to_hass or add_to_platform_abort
# is called # is called
self.add_finished = asyncio.Event() self.add_finished = asyncio.Event()
@ -229,7 +228,6 @@ class EnergyCostSensor(SensorEntity):
def _reset(self, energy_state: State) -> None: def _reset(self, energy_state: State) -> None:
"""Reset the cost sensor.""" """Reset the cost sensor."""
self._attr_native_value = 0.0 self._attr_native_value = 0.0
self._cur_value = 0.0
self._attr_last_reset = dt_util.utcnow() self._attr_last_reset = dt_util.utcnow()
self._last_energy_sensor_state = energy_state self._last_energy_sensor_state = energy_state
self.async_write_ha_state() self.async_write_ha_state()
@ -339,8 +337,8 @@ class EnergyCostSensor(SensorEntity):
self._reset(energy_state_copy) self._reset(energy_state_copy)
# Update with newly incurred cost # Update with newly incurred cost
old_energy_value = float(self._last_energy_sensor_state.state) old_energy_value = float(self._last_energy_sensor_state.state)
self._cur_value += (energy - old_energy_value) * energy_price cur_value = cast(float, self._attr_native_value)
self._attr_native_value = round(self._cur_value, 2) self._attr_native_value = cur_value + (energy - old_energy_value) * energy_price
self._last_energy_sensor_state = energy_state self._last_energy_sensor_state = energy_state