From 11f5b4872426429a148ebbb81828e3d0a28a3485 Mon Sep 17 00:00:00 2001 From: Bertrand Roussel Date: Fri, 10 May 2024 01:16:09 -0700 Subject: [PATCH] Add standard deviation calculation to group (#112076) * Add standard deviation calculation to group * Add missing bits --------- Co-authored-by: G Johansson --- homeassistant/components/group/config_flow.py | 7 ++++--- homeassistant/components/group/sensor.py | 13 +++++++++++++ homeassistant/components/group/strings.json | 9 +++++---- tests/components/group/test_sensor.py | 2 ++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/group/config_flow.py b/homeassistant/components/group/config_flow.py index f3e2405d86a11..b7341aff59ab6 100644 --- a/homeassistant/components/group/config_flow.py +++ b/homeassistant/components/group/config_flow.py @@ -35,14 +35,15 @@ from .switch import async_create_preview_switch _STATISTIC_MEASURES = [ - "min", + "last", "max", "mean", "median", - "last", + "min", + "product", "range", + "stdev", "sum", - "product", ] diff --git a/homeassistant/components/group/sensor.py b/homeassistant/components/group/sensor.py index 5de668c7bb0a1..203b1b3fc8ea7 100644 --- a/homeassistant/components/group/sensor.py +++ b/homeassistant/components/group/sensor.py @@ -66,6 +66,7 @@ ATTR_LAST = "last" ATTR_LAST_ENTITY_ID = "last_entity_id" ATTR_RANGE = "range" +ATTR_STDEV = "stdev" ATTR_SUM = "sum" ATTR_PRODUCT = "product" SENSOR_TYPES = { @@ -75,6 +76,7 @@ ATTR_MEDIAN: "median", ATTR_LAST: "last", ATTR_RANGE: "range", + ATTR_STDEV: "stdev", ATTR_SUM: "sum", ATTR_PRODUCT: "product", } @@ -250,6 +252,16 @@ def calc_range( return {}, value +def calc_stdev( + sensor_values: list[tuple[str, float, State]], +) -> tuple[dict[str, str | None], float]: + """Calculate standard deviation value.""" + result = (sensor_value for _, sensor_value, _ in sensor_values) + + value: float = statistics.stdev(result) + return {}, value + + def calc_sum( sensor_values: list[tuple[str, float, State]], ) -> tuple[dict[str, str | None], float]: @@ -284,6 +296,7 @@ def calc_product( "median": calc_median, "last": calc_last, "range": calc_range, + "stdev": calc_stdev, "sum": calc_sum, "product": calc_product, } diff --git a/homeassistant/components/group/strings.json b/homeassistant/components/group/strings.json index f9039fb896e61..bff1f1e22ec6f 100644 --- a/homeassistant/components/group/strings.json +++ b/homeassistant/components/group/strings.json @@ -189,14 +189,15 @@ "selector": { "type": { "options": { - "min": "Minimum", + "last": "Most recently updated", "max": "Maximum", "mean": "Arithmetic mean", "median": "Median", - "last": "Most recently updated", + "min": "Minimum", + "product": "Product", "range": "Statistical range", - "sum": "Sum", - "product": "Product" + "stdev": "Standard deviation", + "sum": "Sum" } } }, diff --git a/tests/components/group/test_sensor.py b/tests/components/group/test_sensor.py index 4a8c434c7429e..c5331aa2f6051 100644 --- a/tests/components/group/test_sensor.py +++ b/tests/components/group/test_sensor.py @@ -48,6 +48,7 @@ MEAN = statistics.mean(VALUES) MEDIAN = statistics.median(VALUES) RANGE = max(VALUES) - min(VALUES) +STDEV = statistics.stdev(VALUES) SUM_VALUE = sum(VALUES) PRODUCT_VALUE = prod(VALUES) @@ -61,6 +62,7 @@ ("median", MEDIAN, {}), ("last", VALUES[2], {ATTR_LAST_ENTITY_ID: "sensor.test_3"}), ("range", RANGE, {}), + ("stdev", STDEV, {}), ("sum", SUM_VALUE, {}), ("product", PRODUCT_VALUE, {}), ],