Generate external statistics in demo component (#58470)

pull/60855/head
Erik Montnemery 2021-12-02 18:55:46 +01:00 committed by GitHub
parent 8e0ef52cc8
commit 411b0f0b15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 2 deletions

View File

@ -1,13 +1,20 @@
"""Set up the demo environment that mimics interaction with devices.""" """Set up the demo environment that mimics interaction with devices."""
import asyncio import asyncio
import datetime
from random import random
from homeassistant import bootstrap, config_entries from homeassistant import bootstrap, config_entries
from homeassistant.components.recorder.statistics import (
async_add_external_statistics,
get_last_statistics,
)
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_START,
SOUND_PRESSURE_DB, SOUND_PRESSURE_DB,
) )
import homeassistant.core as ha import homeassistant.core as ha
import homeassistant.util.dt as dt_util
DOMAIN = "demo" DOMAIN = "demo"
@ -150,6 +157,82 @@ async def async_setup(hass, config):
return True return True
def _generate_mean_statistics(start, end, init_value, max_diff):
statistics = []
mean = init_value
now = start
while now < end:
mean = mean + random() * max_diff - max_diff / 2
statistics.append(
{
"start": now,
"mean": mean,
"min": mean - random() * max_diff,
"max": mean + random() * max_diff,
}
)
now = now + datetime.timedelta(hours=1)
return statistics
def _generate_sum_statistics(start, end, init_value, max_diff):
statistics = []
now = start
sum_ = init_value
while now < end:
sum_ = sum_ + random() * max_diff
statistics.append(
{
"start": now,
"sum": sum_,
}
)
now = now + datetime.timedelta(hours=1)
return statistics
async def _insert_statistics(hass):
"""Insert some fake statistics."""
now = dt_util.now()
yesterday = now - datetime.timedelta(days=1)
yesterday_midnight = yesterday.replace(hour=0, minute=0, second=0, microsecond=0)
# Fake yesterday's temperatures
metadata = {
"source": DOMAIN,
"statistic_id": f"{DOMAIN}:temperature_outdoor",
"unit_of_measurement": "°C",
"has_mean": True,
"has_sum": False,
}
statistics = _generate_mean_statistics(
yesterday_midnight, yesterday_midnight + datetime.timedelta(days=1), 15, 1
)
async_add_external_statistics(hass, metadata, statistics)
# Fake yesterday's energy consumption
metadata = {
"source": DOMAIN,
"statistic_id": f"{DOMAIN}:energy_consumption",
"unit_of_measurement": "kWh",
"has_mean": False,
"has_sum": True,
}
statistic_id = f"{DOMAIN}:energy_consumption"
sum_ = 0
last_stats = await hass.async_add_executor_job(
get_last_statistics, hass, 1, statistic_id, True
)
if "domain:energy_consumption" in last_stats:
sum_ = last_stats["domain.electricity_total"]["sum"] or 0
statistics = _generate_sum_statistics(
yesterday_midnight, yesterday_midnight + datetime.timedelta(days=1), sum_, 1
)
async_add_external_statistics(hass, metadata, statistics)
async def async_setup_entry(hass, config_entry): async def async_setup_entry(hass, config_entry):
"""Set the config entry up.""" """Set the config entry up."""
# Set up demo platforms with config entry # Set up demo platforms with config entry
@ -157,6 +240,8 @@ async def async_setup_entry(hass, config_entry):
hass.async_create_task( hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, platform) hass.config_entries.async_forward_entry_setup(config_entry, platform)
) )
if "recorder" in hass.config.components:
await _insert_statistics(hass)
return True return True

View File

@ -2,7 +2,8 @@
"domain": "demo", "domain": "demo",
"name": "Demo", "name": "Demo",
"documentation": "https://www.home-assistant.io/integrations/demo", "documentation": "https://www.home-assistant.io/integrations/demo",
"dependencies": ["conversation", "zone", "group"], "after_dependencies": ["recorder"],
"dependencies": ["conversation", "group", "zone"],
"codeowners": ["@home-assistant/core"], "codeowners": ["@home-assistant/core"],
"quality_scale": "internal", "quality_scale": "internal",
"iot_class": "calculated" "iot_class": "calculated"

View File

@ -7,8 +7,11 @@ import pytest
from homeassistant.components.demo import DOMAIN from homeassistant.components.demo import DOMAIN
from homeassistant.components.device_tracker.legacy import YAML_DEVICES from homeassistant.components.device_tracker.legacy import YAML_DEVICES
from homeassistant.components.recorder.statistics import list_statistic_ids
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, setup_component
from tests.components.recorder.common import wait_recording_done
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
@ -40,3 +43,27 @@ async def test_setting_up_demo(hass):
"Unable to convert all demo entities to JSON. " "Unable to convert all demo entities to JSON. "
"Wrong data in state machine!" "Wrong data in state machine!"
) )
def test_demo_statistics(hass_recorder):
"""Test that the demo components makes some statistics available."""
hass = hass_recorder()
assert setup_component(hass, DOMAIN, {DOMAIN: {}})
hass.block_till_done()
hass.start()
wait_recording_done(hass)
statistic_ids = list_statistic_ids(hass)
assert {
"name": None,
"source": "demo",
"statistic_id": "demo:temperature_outdoor",
"unit_of_measurement": "°C",
} in statistic_ids
assert {
"name": None,
"source": "demo",
"statistic_id": "demo:energy_consumption",
"unit_of_measurement": "kWh",
} in statistic_ids