mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Fix demo external energy statistics (#77665)
This commit is contained in:
parent
d0d1b303fd
commit
e326dd2847
@ -16,8 +16,13 @@ from homeassistant.components.recorder.statistics import (
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
|
ENERGY_KILO_WATT_HOUR,
|
||||||
|
ENERGY_MEGA_WATT_HOUR,
|
||||||
EVENT_HOMEASSISTANT_START,
|
EVENT_HOMEASSISTANT_START,
|
||||||
SOUND_PRESSURE_DB,
|
SOUND_PRESSURE_DB,
|
||||||
|
TEMP_CELSIUS,
|
||||||
|
VOLUME_CUBIC_FEET,
|
||||||
|
VOLUME_CUBIC_METERS,
|
||||||
Platform,
|
Platform,
|
||||||
)
|
)
|
||||||
import homeassistant.core as ha
|
import homeassistant.core as ha
|
||||||
@ -262,7 +267,7 @@ async def _insert_sum_statistics(
|
|||||||
statistic_id = metadata["statistic_id"]
|
statistic_id = metadata["statistic_id"]
|
||||||
|
|
||||||
last_stats = await get_instance(hass).async_add_executor_job(
|
last_stats = await get_instance(hass).async_add_executor_job(
|
||||||
get_last_statistics, hass, 1, statistic_id, True
|
get_last_statistics, hass, 1, statistic_id, False
|
||||||
)
|
)
|
||||||
if statistic_id in last_stats:
|
if statistic_id in last_stats:
|
||||||
sum_ = last_stats[statistic_id][0]["sum"] or 0
|
sum_ = last_stats[statistic_id][0]["sum"] or 0
|
||||||
@ -291,7 +296,7 @@ async def _insert_statistics(hass: HomeAssistant) -> None:
|
|||||||
"source": DOMAIN,
|
"source": DOMAIN,
|
||||||
"name": "Outdoor temperature",
|
"name": "Outdoor temperature",
|
||||||
"statistic_id": f"{DOMAIN}:temperature_outdoor",
|
"statistic_id": f"{DOMAIN}:temperature_outdoor",
|
||||||
"unit_of_measurement": "°C",
|
"unit_of_measurement": TEMP_CELSIUS,
|
||||||
"has_mean": True,
|
"has_mean": True,
|
||||||
"has_sum": False,
|
"has_sum": False,
|
||||||
}
|
}
|
||||||
@ -304,11 +309,11 @@ async def _insert_statistics(hass: HomeAssistant) -> None:
|
|||||||
"source": DOMAIN,
|
"source": DOMAIN,
|
||||||
"name": "Energy consumption 1",
|
"name": "Energy consumption 1",
|
||||||
"statistic_id": f"{DOMAIN}:energy_consumption_kwh",
|
"statistic_id": f"{DOMAIN}:energy_consumption_kwh",
|
||||||
"unit_of_measurement": "kWh",
|
"unit_of_measurement": ENERGY_KILO_WATT_HOUR,
|
||||||
"has_mean": False,
|
"has_mean": False,
|
||||||
"has_sum": True,
|
"has_sum": True,
|
||||||
}
|
}
|
||||||
await _insert_sum_statistics(hass, metadata, yesterday_midnight, today_midnight, 2)
|
await _insert_sum_statistics(hass, metadata, yesterday_midnight, today_midnight, 1)
|
||||||
|
|
||||||
# Add external energy consumption in MWh, ~ 12 kWh / day
|
# Add external energy consumption in MWh, ~ 12 kWh / day
|
||||||
# This should not be possible to pick for the energy dashboard
|
# This should not be possible to pick for the energy dashboard
|
||||||
@ -316,12 +321,12 @@ async def _insert_statistics(hass: HomeAssistant) -> None:
|
|||||||
"source": DOMAIN,
|
"source": DOMAIN,
|
||||||
"name": "Energy consumption 2",
|
"name": "Energy consumption 2",
|
||||||
"statistic_id": f"{DOMAIN}:energy_consumption_mwh",
|
"statistic_id": f"{DOMAIN}:energy_consumption_mwh",
|
||||||
"unit_of_measurement": "MWh",
|
"unit_of_measurement": ENERGY_MEGA_WATT_HOUR,
|
||||||
"has_mean": False,
|
"has_mean": False,
|
||||||
"has_sum": True,
|
"has_sum": True,
|
||||||
}
|
}
|
||||||
await _insert_sum_statistics(
|
await _insert_sum_statistics(
|
||||||
hass, metadata, yesterday_midnight, today_midnight, 0.002
|
hass, metadata, yesterday_midnight, today_midnight, 0.001
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add external gas consumption in m³, ~6 m3/day
|
# Add external gas consumption in m³, ~6 m3/day
|
||||||
@ -330,11 +335,13 @@ async def _insert_statistics(hass: HomeAssistant) -> None:
|
|||||||
"source": DOMAIN,
|
"source": DOMAIN,
|
||||||
"name": "Gas consumption 1",
|
"name": "Gas consumption 1",
|
||||||
"statistic_id": f"{DOMAIN}:gas_consumption_m3",
|
"statistic_id": f"{DOMAIN}:gas_consumption_m3",
|
||||||
"unit_of_measurement": "m³",
|
"unit_of_measurement": VOLUME_CUBIC_METERS,
|
||||||
"has_mean": False,
|
"has_mean": False,
|
||||||
"has_sum": True,
|
"has_sum": True,
|
||||||
}
|
}
|
||||||
await _insert_sum_statistics(hass, metadata, yesterday_midnight, today_midnight, 1)
|
await _insert_sum_statistics(
|
||||||
|
hass, metadata, yesterday_midnight, today_midnight, 0.5
|
||||||
|
)
|
||||||
|
|
||||||
# Add external gas consumption in ft³, ~180 ft3/day
|
# Add external gas consumption in ft³, ~180 ft3/day
|
||||||
# This should not be possible to pick for the energy dashboard
|
# This should not be possible to pick for the energy dashboard
|
||||||
@ -342,11 +349,11 @@ async def _insert_statistics(hass: HomeAssistant) -> None:
|
|||||||
"source": DOMAIN,
|
"source": DOMAIN,
|
||||||
"name": "Gas consumption 2",
|
"name": "Gas consumption 2",
|
||||||
"statistic_id": f"{DOMAIN}:gas_consumption_ft3",
|
"statistic_id": f"{DOMAIN}:gas_consumption_ft3",
|
||||||
"unit_of_measurement": "ft³",
|
"unit_of_measurement": VOLUME_CUBIC_FEET,
|
||||||
"has_mean": False,
|
"has_mean": False,
|
||||||
"has_sum": True,
|
"has_sum": True,
|
||||||
}
|
}
|
||||||
await _insert_sum_statistics(hass, metadata, yesterday_midnight, today_midnight, 30)
|
await _insert_sum_statistics(hass, metadata, yesterday_midnight, today_midnight, 15)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||||
|
@ -17,6 +17,7 @@ from homeassistant.components.repairs import DOMAIN as REPAIRS_DOMAIN
|
|||||||
from homeassistant.helpers.json import JSONEncoder
|
from homeassistant.helpers.json import JSONEncoder
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
from homeassistant.util.unit_system import IMPERIAL_SYSTEM
|
||||||
|
|
||||||
from tests.components.recorder.common import async_wait_recording_done
|
from tests.components.recorder.common import async_wait_recording_done
|
||||||
|
|
||||||
@ -83,6 +84,8 @@ async def test_demo_statistics(hass, recorder_mock):
|
|||||||
|
|
||||||
async def test_demo_statistics_growth(hass, recorder_mock):
|
async def test_demo_statistics_growth(hass, recorder_mock):
|
||||||
"""Test that the demo sum statistics adds to the previous state."""
|
"""Test that the demo sum statistics adds to the previous state."""
|
||||||
|
hass.config.units = IMPERIAL_SYSTEM
|
||||||
|
|
||||||
now = dt_util.now()
|
now = dt_util.now()
|
||||||
last_week = now - datetime.timedelta(days=7)
|
last_week = now - datetime.timedelta(days=7)
|
||||||
last_week_midnight = last_week.replace(hour=0, minute=0, second=0, microsecond=0)
|
last_week_midnight = last_week.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
@ -92,7 +95,7 @@ async def test_demo_statistics_growth(hass, recorder_mock):
|
|||||||
"source": DOMAIN,
|
"source": DOMAIN,
|
||||||
"name": "Energy consumption 1",
|
"name": "Energy consumption 1",
|
||||||
"statistic_id": statistic_id,
|
"statistic_id": statistic_id,
|
||||||
"unit_of_measurement": "kWh",
|
"unit_of_measurement": "m³",
|
||||||
"has_mean": False,
|
"has_mean": False,
|
||||||
"has_sum": True,
|
"has_sum": True,
|
||||||
}
|
}
|
||||||
@ -114,6 +117,7 @@ async def test_demo_statistics_growth(hass, recorder_mock):
|
|||||||
get_last_statistics, hass, 1, statistic_id, False
|
get_last_statistics, hass, 1, statistic_id, False
|
||||||
)
|
)
|
||||||
assert statistics[statistic_id][0]["sum"] > 2**20
|
assert statistics[statistic_id][0]["sum"] > 2**20
|
||||||
|
assert statistics[statistic_id][0]["sum"] <= (2**20 + 24)
|
||||||
|
|
||||||
|
|
||||||
async def test_issues_created(hass, hass_client, hass_ws_client):
|
async def test_issues_created(hass, hass_client, hass_ws_client):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user