mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Corrections for external statistics (#58469)
This commit is contained in:
parent
ac4496b985
commit
ac5e32d648
@ -182,8 +182,8 @@ def async_setup(hass: HomeAssistant) -> None:
|
|||||||
entity_id = event.data["entity_id"]
|
entity_id = event.data["entity_id"]
|
||||||
with session_scope(hass=hass) as session:
|
with session_scope(hass=hass) as session:
|
||||||
session.query(StatisticsMeta).filter(
|
session.query(StatisticsMeta).filter(
|
||||||
StatisticsMeta.statistic_id == old_entity_id
|
(StatisticsMeta.statistic_id == old_entity_id)
|
||||||
and StatisticsMeta.source == DOMAIN
|
& (StatisticsMeta.source == DOMAIN)
|
||||||
).update({StatisticsMeta.statistic_id: entity_id})
|
).update({StatisticsMeta.statistic_id: entity_id})
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -457,12 +457,12 @@ def _update_statistics(
|
|||||||
try:
|
try:
|
||||||
session.query(table).filter_by(id=stat_id).update(
|
session.query(table).filter_by(id=stat_id).update(
|
||||||
{
|
{
|
||||||
table.mean: statistic["mean"],
|
table.mean: statistic.get("mean"),
|
||||||
table.min: statistic["min"],
|
table.min: statistic.get("min"),
|
||||||
table.max: statistic["max"],
|
table.max: statistic.get("max"),
|
||||||
table.last_reset: statistic["last_reset"],
|
table.last_reset: statistic.get("last_reset"),
|
||||||
table.state: statistic["state"],
|
table.state: statistic.get("state"),
|
||||||
table.sum: statistic["sum"],
|
table.sum: statistic.get("sum"),
|
||||||
},
|
},
|
||||||
synchronize_session=False,
|
synchronize_session=False,
|
||||||
)
|
)
|
||||||
@ -992,7 +992,7 @@ def _statistics_exists(
|
|||||||
"""Return id if a statistics entry already exists."""
|
"""Return id if a statistics entry already exists."""
|
||||||
result = (
|
result = (
|
||||||
session.query(table.id)
|
session.query(table.id)
|
||||||
.filter(table.metadata_id == metadata_id and table.start == start)
|
.filter((table.metadata_id == metadata_id) & (table.start == start))
|
||||||
.first()
|
.first()
|
||||||
)
|
)
|
||||||
return result["id"] if result else None
|
return result["id"] if result else None
|
||||||
|
@ -314,13 +314,20 @@ def test_external_statistics(hass_recorder, caplog):
|
|||||||
|
|
||||||
zero = dt_util.utcnow()
|
zero = dt_util.utcnow()
|
||||||
period1 = zero.replace(minute=0, second=0, microsecond=0) + timedelta(hours=1)
|
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,
|
"start": period1,
|
||||||
"last_reset": None,
|
"last_reset": None,
|
||||||
"state": 0,
|
"state": 0,
|
||||||
"sum": 2,
|
"sum": 2,
|
||||||
}
|
}
|
||||||
|
external_statistics2 = {
|
||||||
|
"start": period2,
|
||||||
|
"last_reset": None,
|
||||||
|
"state": 1,
|
||||||
|
"sum": 3,
|
||||||
|
}
|
||||||
|
|
||||||
external_metadata = {
|
external_metadata = {
|
||||||
"has_mean": False,
|
"has_mean": False,
|
||||||
@ -331,7 +338,9 @@ def test_external_statistics(hass_recorder, caplog):
|
|||||||
"unit_of_measurement": "kWh",
|
"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)
|
wait_recording_done(hass)
|
||||||
stats = statistics_during_period(hass, zero, period="hour")
|
stats = statistics_during_period(hass, zero, period="hour")
|
||||||
assert stats == {
|
assert stats == {
|
||||||
@ -346,7 +355,18 @@ def test_external_statistics(hass_recorder, caplog):
|
|||||||
"last_reset": None,
|
"last_reset": None,
|
||||||
"state": approx(0.0),
|
"state": approx(0.0),
|
||||||
"sum": approx(2.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)
|
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
|
# Update the previously inserted statistics
|
||||||
external_statistics = {
|
external_statistics = {
|
||||||
"start": period1,
|
"start": period1,
|
||||||
@ -398,7 +455,18 @@ def test_external_statistics(hass_recorder, caplog):
|
|||||||
"last_reset": None,
|
"last_reset": None,
|
||||||
"state": approx(4.0),
|
"state": approx(4.0),
|
||||||
"sum": approx(5.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),
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user