Adds median to min_max component (#36686)

This commit is contained in:
Thorjan Knudsvik authored and GitHub committed 2020-07-18 16:18:31 -07:00
1 parent 6fa04aa3e3
commit e6ff8d6839
2 files changed
+68 -11

No files matched your search

+37
View File
@@ -1,4 +1,5 @@
"""The test for the min/max sensor platform."""
import statistics
import unittest
from homeassistant.const import (
@@ -27,6 +28,7 @@ class TestMinMaxSensor(unittest.TestCase):
self.mean = round(sum(self.values) / self.count, 2)
self.mean_1_digit = round(sum(self.values) / self.count, 1)
self.mean_4_digits = round(sum(self.values) / self.count, 4)
self.median = round(statistics.median(self.values), 2)
def teardown_method(self, method):
"""Stop everything that was started."""
@@ -58,6 +60,7 @@ class TestMinMaxSensor(unittest.TestCase):
assert self.max == state.attributes.get("max_value")
assert entity_ids[1] == state.attributes.get("max_entity_id")
assert self.mean == state.attributes.get("mean")
assert self.median == state.attributes.get("median")
def test_max_sensor(self):
"""Test the max sensor."""
@@ -85,6 +88,7 @@ class TestMinMaxSensor(unittest.TestCase):
assert self.min == state.attributes.get("min_value")
assert entity_ids[1] == state.attributes.get("max_entity_id")
assert self.mean == state.attributes.get("mean")
assert self.median == state.attributes.get("median")
def test_mean_sensor(self):
"""Test the mean sensor."""
@@ -112,6 +116,7 @@ class TestMinMaxSensor(unittest.TestCase):
assert entity_ids[2] == state.attributes.get("min_entity_id")
assert self.max == state.attributes.get("max_value")
assert entity_ids[1] == state.attributes.get("max_entity_id")
assert self.median == state.attributes.get("median")
def test_mean_1_digit_sensor(self):
"""Test the mean with 1-digit precision sensor."""
@@ -140,6 +145,7 @@ class TestMinMaxSensor(unittest.TestCase):
assert entity_ids[2] == state.attributes.get("min_entity_id")
assert self.max == state.attributes.get("max_value")
assert entity_ids[1] == state.attributes.get("max_entity_id")
assert self.median == state.attributes.get("median")
def test_mean_4_digit_sensor(self):
"""Test the mean with 1-digit precision sensor."""
@@ -168,6 +174,35 @@ class TestMinMaxSensor(unittest.TestCase):
assert entity_ids[2] == state.attributes.get("min_entity_id")
assert self.max == state.attributes.get("max_value")
assert entity_ids[1] == state.attributes.get("max_entity_id")
assert self.median == state.attributes.get("median")
def test_median_sensor(self):
"""Test the median sensor."""
config = {
"sensor": {
"platform": "min_max",
"name": "test_median",
"type": "median",
"entity_ids": ["sensor.test_1", "sensor.test_2", "sensor.test_3"],
}
}
assert setup_component(self.hass, "sensor", config)
entity_ids = config["sensor"]["entity_ids"]
for entity_id, value in dict(zip(entity_ids, self.values)).items():
self.hass.states.set(entity_id, value)
self.hass.block_till_done()
state = self.hass.states.get("sensor.test_median")
assert str(float(self.median)) == state.state
assert self.min == state.attributes.get("min_value")
assert entity_ids[2] == state.attributes.get("min_entity_id")
assert self.max == state.attributes.get("max_value")
assert entity_ids[1] == state.attributes.get("max_entity_id")
assert self.mean == state.attributes.get("mean")
def test_not_enough_sensor_value(self):
"""Test that there is nothing done if not enough values available."""
@@ -193,6 +228,7 @@ class TestMinMaxSensor(unittest.TestCase):
assert state.attributes.get("min_value") is None
assert state.attributes.get("max_entity_id") is None
assert state.attributes.get("max_value") is None
assert state.attributes.get("median") is None
self.hass.states.set(entity_ids[1], self.values[1])
self.hass.block_till_done()
@@ -295,3 +331,4 @@ class TestMinMaxSensor(unittest.TestCase):
assert self.min == state.attributes.get("min_value")
assert self.max == state.attributes.get("max_value")
assert self.mean == state.attributes.get("mean")
assert self.median == state.attributes.get("median")