Corrections for external statistics (#58469)

This commit is contained in:
Erik Montnemery 2021-10-26 14:05:45 +02:00 committed by GitHub
parent ac4496b985
commit ac5e32d648
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 13 deletions

View file

@ -182,8 +182,8 @@ def async_setup(hass: HomeAssistant) -> None:
entity_id = event.data["entity_id"]
with session_scope(hass=hass) as session:
session.query(StatisticsMeta).filter(
StatisticsMeta.statistic_id == old_entity_id
and StatisticsMeta.source == DOMAIN
(StatisticsMeta.statistic_id == old_entity_id)
& (StatisticsMeta.source == DOMAIN)
).update({StatisticsMeta.statistic_id: entity_id})
@callback
@ -457,12 +457,12 @@ def _update_statistics(
try:
session.query(table).filter_by(id=stat_id).update(
{
table.mean: statistic["mean"],
table.min: statistic["min"],
table.max: statistic["max"],
table.last_reset: statistic["last_reset"],
table.state: statistic["state"],
table.sum: statistic["sum"],
table.mean: statistic.get("mean"),
table.min: statistic.get("min"),
table.max: statistic.get("max"),
table.last_reset: statistic.get("last_reset"),
table.state: statistic.get("state"),
table.sum: statistic.get("sum"),
},
synchronize_session=False,
)
@ -992,7 +992,7 @@ def _statistics_exists(
"""Return id if a statistics entry already exists."""
result = (
session.query(table.id)
.filter(table.metadata_id == metadata_id and table.start == start)
.filter((table.metadata_id == metadata_id) & (table.start == start))
.first()
)
return result["id"] if result else None

View file

@ -314,13 +314,20 @@ def test_external_statistics(hass_recorder, caplog):
zero = dt_util.utcnow()
period1 = zero.replace(minute=0, second=0, microsecond=0) + timedelta(hours=1)
period2 = zero.replace(minute=0, second=0, microsecond=0) + timedelta(hours=2)
external_statistics = {
external_statistics1 = {
"start": period1,
"last_reset": None,
"state": 0,
"sum": 2,
}
external_statistics2 = {
"start": period2,
"last_reset": None,
"state": 1,
"sum": 3,
}
external_metadata = {
"has_mean": False,
@ -331,7 +338,9 @@ def test_external_statistics(hass_recorder, caplog):
"unit_of_measurement": "kWh",
}
async_add_external_statistics(hass, external_metadata, (external_statistics,))
async_add_external_statistics(
hass, external_metadata, (external_statistics1, external_statistics2)
)
wait_recording_done(hass)
stats = statistics_during_period(hass, zero, period="hour")
assert stats == {
@ -346,7 +355,18 @@ def test_external_statistics(hass_recorder, caplog):
"last_reset": None,
"state": approx(0.0),
"sum": approx(2.0),
}
},
{
"statistic_id": "test:total_energy_import",
"start": period2.isoformat(),
"end": (period2 + timedelta(hours=1)).isoformat(),
"max": None,
"mean": None,
"min": None,
"last_reset": None,
"state": approx(1.0),
"sum": approx(3.0),
},
]
}
statistic_ids = list_statistic_ids(hass)
@ -373,6 +393,43 @@ def test_external_statistics(hass_recorder, caplog):
)
}
# Update the previously inserted statistics
external_statistics = {
"start": period1,
"last_reset": None,
"state": 5,
"sum": 6,
}
async_add_external_statistics(hass, external_metadata, (external_statistics,))
wait_recording_done(hass)
stats = statistics_during_period(hass, zero, period="hour")
assert stats == {
"test:total_energy_import": [
{
"statistic_id": "test:total_energy_import",
"start": period1.isoformat(),
"end": (period1 + timedelta(hours=1)).isoformat(),
"max": None,
"mean": None,
"min": None,
"last_reset": None,
"state": approx(5.0),
"sum": approx(6.0),
},
{
"statistic_id": "test:total_energy_import",
"start": period2.isoformat(),
"end": (period2 + timedelta(hours=1)).isoformat(),
"max": None,
"mean": None,
"min": None,
"last_reset": None,
"state": approx(1.0),
"sum": approx(3.0),
},
]
}
# Update the previously inserted statistics
external_statistics = {
"start": period1,
@ -398,7 +455,18 @@ def test_external_statistics(hass_recorder, caplog):
"last_reset": None,
"state": approx(4.0),
"sum": approx(5.0),
}
},
{
"statistic_id": "test:total_energy_import",
"start": period2.isoformat(),
"end": (period2 + timedelta(hours=1)).isoformat(),
"max": None,
"mean": None,
"min": None,
"last_reset": None,
"state": approx(1.0),
"sum": approx(3.0),
},
]
}