Add rate of change to statistics sensor (#15632)
* always export max_age/min_age * downgrade errors of missing data on start with empty recorder database these errors are logged multiple times: ERROR (MainThread) [homeassistant.components.sensor.statistics] mean requires at least one data point ERROR (MainThread) [homeassistant.components.sensor.statistics] variance requires at least two data points downgrade them to debug as they are not meaningful to end users * add change_rate attribute this calculates the average change rate of all data points * simplify count, reorder attribute calculation * reorder initialization * reorder attribute names * don't use min/max for min_age/max_age * add test case * style * style * sort constants * init variables with None * add precision config setting * round to precision * test round
This commit is contained in:
1 parent
50266e9b91
commit
8a2bc99f63
2 files changed
+112
-52
No files matched your search
@@ -28,8 +28,10 @@ class TestStatisticsSensor(unittest.TestCase):
|
||||
self.median = round(statistics.median(self.values), 2)
|
||||
self.deviation = round(statistics.stdev(self.values), 2)
|
||||
self.variance = round(statistics.variance(self.values), 2)
|
||||
self.change = self.values[-1] - self.values[0]
|
||||
self.average_change = self.change / (len(self.values) - 1)
|
||||
self.change = round(self.values[-1] - self.values[0], 2)
|
||||
self.average_change = round(self.change / (len(self.values) - 1), 2)
|
||||
self.change_rate = round(self.average_change / (60 * (self.count - 1)),
|
||||
2)
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
@@ -171,6 +173,42 @@ class TestStatisticsSensor(unittest.TestCase):
|
||||
self.assertEqual(6, state.attributes.get('min_value'))
|
||||
self.assertEqual(14, state.attributes.get('max_value'))
|
||||
|
||||
def test_change_rate(self):
|
||||
"""Test min_age/max_age and change_rate."""
|
||||
mock_data = {
|
||||
'return_time': datetime(2017, 8, 2, 12, 23, 42,
|
||||
tzinfo=dt_util.UTC),
|
||||
}
|
||||
|
||||
def mock_now():
|
||||
return mock_data['return_time']
|
||||
|
||||
with patch('homeassistant.components.sensor.statistics.dt_util.utcnow',
|
||||
new=mock_now):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'statistics',
|
||||
'name': 'test',
|
||||
'entity_id': 'sensor.test_monitored'
|
||||
}
|
||||
})
|
||||
|
||||
for value in self.values:
|
||||
self.hass.states.set('sensor.test_monitored', value,
|
||||
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
|
||||
self.hass.block_till_done()
|
||||
# insert the next value one minute later
|
||||
mock_data['return_time'] += timedelta(minutes=1)
|
||||
|
||||
state = self.hass.states.get('sensor.test_mean')
|
||||
|
||||
self.assertEqual(datetime(2017, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC),
|
||||
state.attributes.get('min_age'))
|
||||
self.assertEqual(datetime(2017, 8, 2, 12, 23 + self.count - 1, 42,
|
||||
tzinfo=dt_util.UTC),
|
||||
state.attributes.get('max_age'))
|
||||
self.assertEqual(self.change_rate, state.attributes.get('change_rate'))
|
||||
|
||||
def test_initialize_from_database(self):
|
||||
"""Test initializing the statistics from the database."""
|
||||
# enable the recorder
|
||||
|
||||
Reference in new issue
Block a user