Add product calculation to Group sensor (#87373)

* Group product

* config flow
This commit is contained in:
G Johansson 2023-03-28 09:35:09 +02:00 committed by GitHub
parent 8b7594ae08
commit 706e8d5612
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 0 deletions

View file

@ -31,6 +31,7 @@ _STATISTIC_MEASURES = [
selector.SelectOptionDict(value="last", label="Most recently updated"), selector.SelectOptionDict(value="last", label="Most recently updated"),
selector.SelectOptionDict(value="range", label="Statistical range"), selector.SelectOptionDict(value="range", label="Statistical range"),
selector.SelectOptionDict(value="sum", label="Sum"), selector.SelectOptionDict(value="sum", label="Sum"),
selector.SelectOptionDict(value="product", label="Product"),
] ]

View file

@ -54,6 +54,7 @@ ATTR_LAST = "last"
ATTR_LAST_ENTITY_ID = "last_entity_id" ATTR_LAST_ENTITY_ID = "last_entity_id"
ATTR_RANGE = "range" ATTR_RANGE = "range"
ATTR_SUM = "sum" ATTR_SUM = "sum"
ATTR_PRODUCT = "product"
SENSOR_TYPES = { SENSOR_TYPES = {
ATTR_MIN_VALUE: "min", ATTR_MIN_VALUE: "min",
ATTR_MAX_VALUE: "max", ATTR_MAX_VALUE: "max",
@ -62,6 +63,7 @@ SENSOR_TYPES = {
ATTR_LAST: "last", ATTR_LAST: "last",
ATTR_RANGE: "range", ATTR_RANGE: "range",
ATTR_SUM: "sum", ATTR_SUM: "sum",
ATTR_PRODUCT: "product",
} }
SENSOR_TYPE_TO_ATTR = {v: k for k, v in SENSOR_TYPES.items()} SENSOR_TYPE_TO_ATTR = {v: k for k, v in SENSOR_TYPES.items()}
@ -226,6 +228,17 @@ def calc_sum(
return {}, result return {}, result
def calc_product(
sensor_values: list[tuple[str, float, State]]
) -> tuple[dict[str, str | None], float]:
"""Calculate a product of values."""
result = 1.0
for _, sensor_value, _ in sensor_values:
result *= sensor_value
return {}, result
CALC_TYPES: dict[ CALC_TYPES: dict[
str, str,
Callable[ Callable[
@ -239,6 +252,7 @@ CALC_TYPES: dict[
"last": calc_last, "last": calc_last,
"range": calc_range, "range": calc_range,
"sum": calc_sum, "sum": calc_sum,
"product": calc_product,
} }

View file

@ -1,6 +1,7 @@
"""The tests for the Group Sensor platform.""" """The tests for the Group Sensor platform."""
from __future__ import annotations from __future__ import annotations
from math import prod
import statistics import statistics
from typing import Any from typing import Any
from unittest.mock import patch from unittest.mock import patch
@ -45,6 +46,7 @@ MEAN = statistics.mean(VALUES)
MEDIAN = statistics.median(VALUES) MEDIAN = statistics.median(VALUES)
RANGE = max(VALUES) - min(VALUES) RANGE = max(VALUES) - min(VALUES)
SUM_VALUE = sum(VALUES) SUM_VALUE = sum(VALUES)
PRODUCT_VALUE = prod(VALUES)
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -57,6 +59,7 @@ SUM_VALUE = sum(VALUES)
("last", VALUES[2], {ATTR_LAST_ENTITY_ID: "sensor.test_3"}), ("last", VALUES[2], {ATTR_LAST_ENTITY_ID: "sensor.test_3"}),
("range", RANGE, {}), ("range", RANGE, {}),
("sum", SUM_VALUE, {}), ("sum", SUM_VALUE, {}),
("product", PRODUCT_VALUE, {}),
], ],
) )
async def test_sensors( async def test_sensors(