Integration Sensor Initial State (#55875)

* initial state is UNAVAILABLE

* update tests
This commit is contained in:
Diogo Gomes 2021-09-07 07:12:54 +01:00 committed by GitHub
parent 789f21c427
commit 1ca9deb520
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View file

@ -106,7 +106,7 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
"""Initialize the integration sensor."""
self._sensor_source_id = source_entity
self._round_digits = round_digits
self._state = 0
self._state = STATE_UNAVAILABLE
self._method = integration_method
self._name = name if name is not None else f"{source_entity} integral"
@ -187,7 +187,10 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
except AssertionError as err:
_LOGGER.error("Could not calculate integral: %s", err)
else:
self._state += integral
if isinstance(self._state, Decimal):
self._state += integral
else:
self._state = integral
self.async_write_ha_state()
async_track_state_change_event(
@ -202,7 +205,9 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
@property
def native_value(self):
"""Return the state of the sensor."""
return round(self._state, self._round_digits)
if isinstance(self._state, Decimal):
return round(self._state, self._round_digits)
return self._state
@property
def native_unit_of_measurement(self):