diff --git a/homeassistant/components/statistics/sensor.py b/homeassistant/components/statistics/sensor.py index 5dc11b92729..e32ce804cb2 100644 --- a/homeassistant/components/statistics/sensor.py +++ b/homeassistant/components/statistics/sensor.py @@ -177,9 +177,9 @@ class StatisticsSensor(SensorEntity): self.states = deque(maxlen=self._samples_max_buffer_size) self.ages = deque(maxlen=self._samples_max_buffer_size) self.attributes = { - STAT_AGE_COVERAGE_RATIO: STATE_UNKNOWN, - STAT_BUFFER_USAGE_RATIO: STATE_UNKNOWN, - STAT_SOURCE_VALUE_VALID: STATE_UNKNOWN, + STAT_AGE_COVERAGE_RATIO: None, + STAT_BUFFER_USAGE_RATIO: None, + STAT_SOURCE_VALUE_VALID: None, } self._state_characteristic_fn = getattr( self, f"_stat_{self._state_characteristic}" @@ -319,15 +319,8 @@ class StatisticsSensor(SensorEntity): @property def extra_state_attributes(self): """Return the state attributes of the sensor.""" - extra_attr = {} - if self._samples_max_age is not None: - extra_attr = { - STAT_AGE_COVERAGE_RATIO: self.attributes[STAT_AGE_COVERAGE_RATIO] - } return { - **extra_attr, - STAT_BUFFER_USAGE_RATIO: self.attributes[STAT_BUFFER_USAGE_RATIO], - STAT_SOURCE_VALUE_VALID: self.attributes[STAT_SOURCE_VALUE_VALID], + key: value for key, value in self.attributes.items() if value is not None } @property @@ -449,7 +442,7 @@ class StatisticsSensor(SensorEntity): 2, ) else: - self.attributes[STAT_AGE_COVERAGE_RATIO] = STATE_UNKNOWN + self.attributes[STAT_AGE_COVERAGE_RATIO] = None def _update_value(self): """Front to call the right statistical characteristics functions. diff --git a/tests/components/statistics/test_sensor.py b/tests/components/statistics/test_sensor.py index 91e5f07497b..f1f895b2b1f 100644 --- a/tests/components/statistics/test_sensor.py +++ b/tests/components/statistics/test_sensor.py @@ -328,7 +328,7 @@ class TestStatisticsSensor(unittest.TestCase): state = self.hass.states.get("sensor.test") assert state.state == STATE_UNKNOWN assert state.attributes.get("buffer_usage_ratio") == round(0 / 20, 2) - assert state.attributes.get("age_coverage_ratio") == STATE_UNKNOWN + assert state.attributes.get("age_coverage_ratio") is None def test_precision_0(self): """Test correct result with precision=0 as integer."""