2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Demo component."""
|
2021-03-23 13:36:43 +00:00
|
|
|
from contextlib import suppress
|
2015-09-13 15:08:46 +00:00
|
|
|
import json
|
2016-02-14 23:08:23 +00:00
|
|
|
import os
|
2014-11-29 06:49:29 +00:00
|
|
|
|
2017-07-22 04:38:53 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-04-09 22:40:51 +00:00
|
|
|
from homeassistant.components.demo import DOMAIN
|
2019-05-15 21:43:45 +00:00
|
|
|
from homeassistant.components.device_tracker.legacy import YAML_DEVICES
|
2022-03-21 22:49:18 +00:00
|
|
|
from homeassistant.components.recorder import get_instance
|
2021-12-02 17:55:46 +00:00
|
|
|
from homeassistant.components.recorder.statistics import list_statistic_ids
|
2018-08-21 13:49:58 +00:00
|
|
|
from homeassistant.helpers.json import JSONEncoder
|
2022-03-21 22:49:18 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2021-12-02 17:55:46 +00:00
|
|
|
|
2022-04-25 10:04:47 +00:00
|
|
|
from tests.components.recorder.common import async_wait_recording_done
|
2014-11-29 06:49:29 +00:00
|
|
|
|
2017-07-22 04:38:53 +00:00
|
|
|
|
2018-02-26 08:28:25 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_history(hass):
|
|
|
|
"""Mock history component loaded."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.config.components.add("history")
|
2018-02-26 08:28:25 +00:00
|
|
|
|
|
|
|
|
2017-07-22 04:38:53 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def demo_cleanup(hass):
|
|
|
|
"""Clean up device tracker demo file."""
|
|
|
|
yield
|
2021-03-23 13:36:43 +00:00
|
|
|
with suppress(FileNotFoundError):
|
2019-05-15 21:43:45 +00:00
|
|
|
os.remove(hass.config.path(YAML_DEVICES))
|
2014-11-29 06:49:29 +00:00
|
|
|
|
|
|
|
|
2019-04-14 23:59:06 +00:00
|
|
|
async def test_setting_up_demo(hass):
|
|
|
|
"""Test if we can set up the demo and dump it to JSON."""
|
2020-04-09 22:40:51 +00:00
|
|
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
|
2020-04-12 21:10:05 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-04-14 23:59:06 +00:00
|
|
|
await hass.async_start()
|
2015-09-13 15:08:46 +00:00
|
|
|
|
2019-04-14 23:59:06 +00:00
|
|
|
# This is done to make sure entity components don't accidentally store
|
|
|
|
# non-JSON-serializable data in the state machine.
|
2017-07-22 04:38:53 +00:00
|
|
|
try:
|
|
|
|
json.dumps(hass.states.async_all(), cls=JSONEncoder)
|
2020-04-09 22:40:51 +00:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2019-07-31 19:25:30 +00:00
|
|
|
pytest.fail(
|
|
|
|
"Unable to convert all demo entities to JSON. "
|
|
|
|
"Wrong data in state machine!"
|
|
|
|
)
|
2021-12-02 17:55:46 +00:00
|
|
|
|
|
|
|
|
2022-03-21 22:49:18 +00:00
|
|
|
async def test_demo_statistics(hass, recorder_mock):
|
2021-12-02 17:55:46 +00:00
|
|
|
"""Test that the demo components makes some statistics available."""
|
2022-03-21 22:49:18 +00:00
|
|
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_start()
|
2022-04-25 10:04:47 +00:00
|
|
|
await async_wait_recording_done(hass)
|
2021-12-02 17:55:46 +00:00
|
|
|
|
2022-03-21 22:49:18 +00:00
|
|
|
statistic_ids = await get_instance(hass).async_add_executor_job(
|
|
|
|
list_statistic_ids, hass
|
|
|
|
)
|
2021-12-02 17:55:46 +00:00
|
|
|
assert {
|
2022-03-24 09:12:01 +00:00
|
|
|
"has_mean": True,
|
|
|
|
"has_sum": False,
|
2021-12-02 17:55:46 +00:00
|
|
|
"name": None,
|
|
|
|
"source": "demo",
|
|
|
|
"statistic_id": "demo:temperature_outdoor",
|
|
|
|
"unit_of_measurement": "°C",
|
|
|
|
} in statistic_ids
|
|
|
|
assert {
|
2022-03-24 09:12:01 +00:00
|
|
|
"has_mean": False,
|
|
|
|
"has_sum": True,
|
2021-12-02 17:55:46 +00:00
|
|
|
"name": None,
|
|
|
|
"source": "demo",
|
|
|
|
"statistic_id": "demo:energy_consumption",
|
|
|
|
"unit_of_measurement": "kWh",
|
|
|
|
} in statistic_ids
|