Skip to content

Commit b003429

Browse files
authored
Expose statistics selector, use for recorder.get_statistics (home-assistant#147056)
* Expose statistics selector, use for `recorder.get_statistics` * code review * syntax formatting * rerun ci
1 parent 4aff032 commit b003429

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

homeassistant/components/recorder/services.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ get_statistics:
6969
- sensor.energy_consumption
7070
- sensor.temperature
7171
selector:
72-
entity:
72+
statistic:
7373
multiple: true
7474

7575
period:

homeassistant/helpers/selector.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,39 @@ def __call__(self, data: Any) -> Any:
12161216
return [parent_schema(vol.Schema(str)(val)) for val in data]
12171217

12181218

1219+
class StatisticSelectorConfig(BaseSelectorConfig, total=False):
1220+
"""Class to represent a statistic selector config."""
1221+
1222+
multiple: bool
1223+
1224+
1225+
@SELECTORS.register("statistic")
1226+
class StatisticSelector(Selector[StatisticSelectorConfig]):
1227+
"""Selector of a single or list of statistics."""
1228+
1229+
selector_type = "statistic"
1230+
1231+
CONFIG_SCHEMA = BASE_SELECTOR_CONFIG_SCHEMA.extend(
1232+
{
1233+
vol.Optional("multiple", default=False): cv.boolean,
1234+
}
1235+
)
1236+
1237+
def __init__(self, config: StatisticSelectorConfig | None = None) -> None:
1238+
"""Instantiate a selector."""
1239+
super().__init__(config)
1240+
1241+
def __call__(self, data: Any) -> str | list[str]:
1242+
"""Validate the passed selection."""
1243+
1244+
if not self.config["multiple"]:
1245+
stat: str = vol.Schema(str)(data)
1246+
return stat
1247+
if not isinstance(data, list):
1248+
raise vol.Invalid("Value should be a list")
1249+
return [vol.Schema(str)(val) for val in data]
1250+
1251+
12191252
class TargetSelectorConfig(BaseSelectorConfig, total=False):
12201253
"""Class to represent a target selector config."""
12211254

tests/helpers/test_selector.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,3 +1262,30 @@ def test_label_selector_schema(schema, valid_selections, invalid_selections) ->
12621262
def test_floor_selector_schema(schema, valid_selections, invalid_selections) -> None:
12631263
"""Test floor selector."""
12641264
_test_selector("floor", schema, valid_selections, invalid_selections)
1265+
1266+
1267+
@pytest.mark.parametrize(
1268+
("schema", "valid_selections", "invalid_selections"),
1269+
[
1270+
(
1271+
{},
1272+
("sensor.temperature",),
1273+
(None, ["sensor.temperature"]),
1274+
),
1275+
(
1276+
{"multiple": True},
1277+
(["sensor.temperature", "sensor:external_temperature"], []),
1278+
("sensor.temperature",),
1279+
),
1280+
(
1281+
{"multiple": False},
1282+
("sensor.temperature",),
1283+
(None, ["sensor.temperature"]),
1284+
),
1285+
],
1286+
)
1287+
def test_statistic_selector_schema(
1288+
schema, valid_selections, invalid_selections
1289+
) -> None:
1290+
"""Test statistic selector."""
1291+
_test_selector("statistic", schema, valid_selections, invalid_selections)

0 commit comments

Comments
 (0)