Skip to content

Commit

Permalink
Add standard deviation calculation to group (#112076)
Browse files Browse the repository at this point in the history
* Add standard deviation calculation to group

* Add missing bits

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
  • Loading branch information
CoRfr and gjohansson-ST committed May 10, 2024
1 parent 8c54587 commit 11f5b48
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
7 changes: 4 additions & 3 deletions homeassistant/components/group/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]


Expand Down
13 changes: 13 additions & 0 deletions homeassistant/components/group/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -75,6 +76,7 @@
ATTR_MEDIAN: "median",
ATTR_LAST: "last",
ATTR_RANGE: "range",
ATTR_STDEV: "stdev",
ATTR_SUM: "sum",
ATTR_PRODUCT: "product",
}
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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,
}
Expand Down
9 changes: 5 additions & 4 deletions homeassistant/components/group/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions tests/components/group/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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, {}),
],
Expand Down

0 comments on commit 11f5b48

Please sign in to comment.