2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Demo component."""
|
2022-08-25 15:39:51 +00:00
|
|
|
import datetime
|
2022-07-20 12:46:06 +00:00
|
|
|
from http import HTTPStatus
|
2015-09-13 15:08:46 +00:00
|
|
|
import json
|
2022-07-20 12:46:06 +00:00
|
|
|
from unittest.mock import ANY, patch
|
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
|
2022-03-21 22:49:18 +00:00
|
|
|
from homeassistant.components.recorder import get_instance
|
2022-08-25 15:39:51 +00:00
|
|
|
from homeassistant.components.recorder.statistics import (
|
|
|
|
async_add_external_statistics,
|
|
|
|
get_last_statistics,
|
|
|
|
list_statistic_ids,
|
|
|
|
)
|
2022-08-25 13:33:05 +00:00
|
|
|
from homeassistant.components.repairs import DOMAIN as REPAIRS_DOMAIN
|
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
|
2022-08-25 15:39:51 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2022-10-20 13:42:23 +00:00
|
|
|
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
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
|
|
|
|
2022-10-19 05:58:47 +00:00
|
|
|
@pytest.fixture
|
2018-02-26 08:28:25 +00:00
|
|
|
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)
|
2022-10-19 05:58:47 +00:00
|
|
|
def mock_device_tracker_update_config():
|
2022-07-02 18:20:40 +00:00
|
|
|
"""Prevent device tracker from creating known devices file."""
|
|
|
|
with patch("homeassistant.components.device_tracker.legacy.update_config"):
|
|
|
|
yield
|
2014-11-29 06:49:29 +00:00
|
|
|
|
|
|
|
|
2022-10-19 05:58:47 +00:00
|
|
|
async def test_setting_up_demo(mock_history, hass):
|
2019-04-14 23:59:06 +00:00
|
|
|
"""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-10-19 05:58:47 +00:00
|
|
|
async def test_demo_statistics(recorder_mock, mock_history, hass):
|
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-11-28 18:46:57 +00:00
|
|
|
"display_unit_of_measurement": "°C",
|
2022-03-24 09:12:01 +00:00
|
|
|
"has_mean": True,
|
|
|
|
"has_sum": False,
|
2022-08-24 09:09:23 +00:00
|
|
|
"name": "Outdoor temperature",
|
2021-12-02 17:55:46 +00:00
|
|
|
"source": "demo",
|
|
|
|
"statistic_id": "demo:temperature_outdoor",
|
2022-08-31 09:30:45 +00:00
|
|
|
"statistics_unit_of_measurement": "°C",
|
2022-09-20 21:43:57 +00:00
|
|
|
"unit_class": "temperature",
|
2021-12-02 17:55:46 +00:00
|
|
|
} in statistic_ids
|
|
|
|
assert {
|
2022-11-28 18:46:57 +00:00
|
|
|
"display_unit_of_measurement": "kWh",
|
2022-03-24 09:12:01 +00:00
|
|
|
"has_mean": False,
|
|
|
|
"has_sum": True,
|
2022-08-24 09:09:23 +00:00
|
|
|
"name": "Energy consumption 1",
|
2021-12-02 17:55:46 +00:00
|
|
|
"source": "demo",
|
2022-08-24 09:09:23 +00:00
|
|
|
"statistic_id": "demo:energy_consumption_kwh",
|
2022-08-31 09:30:45 +00:00
|
|
|
"statistics_unit_of_measurement": "kWh",
|
2022-09-20 21:43:57 +00:00
|
|
|
"unit_class": "energy",
|
2021-12-02 17:55:46 +00:00
|
|
|
} in statistic_ids
|
2022-07-20 12:46:06 +00:00
|
|
|
|
|
|
|
|
2022-10-19 05:58:47 +00:00
|
|
|
async def test_demo_statistics_growth(recorder_mock, mock_history, hass):
|
2022-08-25 15:39:51 +00:00
|
|
|
"""Test that the demo sum statistics adds to the previous state."""
|
2022-10-20 13:42:23 +00:00
|
|
|
hass.config.units = US_CUSTOMARY_SYSTEM
|
2022-09-01 18:03:37 +00:00
|
|
|
|
2022-08-25 15:39:51 +00:00
|
|
|
now = dt_util.now()
|
|
|
|
last_week = now - datetime.timedelta(days=7)
|
|
|
|
last_week_midnight = last_week.replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
|
|
|
|
|
statistic_id = f"{DOMAIN}:energy_consumption_kwh"
|
|
|
|
metadata = {
|
|
|
|
"source": DOMAIN,
|
|
|
|
"name": "Energy consumption 1",
|
|
|
|
"statistic_id": statistic_id,
|
2022-09-01 18:03:37 +00:00
|
|
|
"unit_of_measurement": "m³",
|
2022-08-25 15:39:51 +00:00
|
|
|
"has_mean": False,
|
|
|
|
"has_sum": True,
|
|
|
|
}
|
|
|
|
statistics = [
|
|
|
|
{
|
|
|
|
"start": last_week_midnight,
|
|
|
|
"sum": 2**20,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
async_add_external_statistics(hass, metadata, statistics)
|
|
|
|
await async_wait_recording_done(hass)
|
|
|
|
|
|
|
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_start()
|
|
|
|
await async_wait_recording_done(hass)
|
|
|
|
|
|
|
|
statistics = await get_instance(hass).async_add_executor_job(
|
2022-11-16 16:36:30 +00:00
|
|
|
get_last_statistics, hass, 1, statistic_id, False, {"sum"}
|
2022-08-25 15:39:51 +00:00
|
|
|
)
|
|
|
|
assert statistics[statistic_id][0]["sum"] > 2**20
|
2022-09-01 18:03:37 +00:00
|
|
|
assert statistics[statistic_id][0]["sum"] <= (2**20 + 24)
|
2022-08-25 15:39:51 +00:00
|
|
|
|
|
|
|
|
2022-10-19 05:58:47 +00:00
|
|
|
async def test_issues_created(mock_history, hass, hass_client, hass_ws_client):
|
2022-07-20 12:46:06 +00:00
|
|
|
"""Test issues are created and can be fixed."""
|
2022-08-25 13:33:05 +00:00
|
|
|
assert await async_setup_component(hass, REPAIRS_DOMAIN, {REPAIRS_DOMAIN: {}})
|
2022-07-20 12:46:06 +00:00
|
|
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_start()
|
|
|
|
|
|
|
|
ws_client = await hass_ws_client(hass)
|
|
|
|
client = await hass_client()
|
|
|
|
|
|
|
|
await ws_client.send_json({"id": 1, "type": "repairs/list_issues"})
|
|
|
|
msg = await ws_client.receive_json()
|
|
|
|
|
|
|
|
assert msg["success"]
|
|
|
|
assert msg["result"] == {
|
|
|
|
"issues": [
|
|
|
|
{
|
|
|
|
"breaks_in_ha_version": "2023.1.1",
|
|
|
|
"created": ANY,
|
|
|
|
"dismissed_version": None,
|
|
|
|
"domain": "demo",
|
|
|
|
"ignored": False,
|
|
|
|
"is_fixable": False,
|
|
|
|
"issue_id": "transmogrifier_deprecated",
|
2022-07-27 20:53:51 +00:00
|
|
|
"issue_domain": None,
|
2022-07-20 12:46:06 +00:00
|
|
|
"learn_more_url": "https://en.wiktionary.org/wiki/transmogrifier",
|
|
|
|
"severity": "warning",
|
|
|
|
"translation_key": "transmogrifier_deprecated",
|
|
|
|
"translation_placeholders": None,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"breaks_in_ha_version": "2023.1.1",
|
|
|
|
"created": ANY,
|
|
|
|
"dismissed_version": None,
|
|
|
|
"domain": "demo",
|
|
|
|
"ignored": False,
|
|
|
|
"is_fixable": True,
|
|
|
|
"issue_id": "out_of_blinker_fluid",
|
2022-07-27 20:53:51 +00:00
|
|
|
"issue_domain": None,
|
2022-07-20 12:46:06 +00:00
|
|
|
"learn_more_url": "https://www.youtube.com/watch?v=b9rntRxLlbU",
|
|
|
|
"severity": "critical",
|
|
|
|
"translation_key": "out_of_blinker_fluid",
|
|
|
|
"translation_placeholders": None,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"breaks_in_ha_version": None,
|
|
|
|
"created": ANY,
|
|
|
|
"dismissed_version": None,
|
|
|
|
"domain": "demo",
|
|
|
|
"ignored": False,
|
|
|
|
"is_fixable": False,
|
|
|
|
"issue_id": "unfixable_problem",
|
2022-07-27 20:53:51 +00:00
|
|
|
"issue_domain": None,
|
2022-07-20 12:46:06 +00:00
|
|
|
"learn_more_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
|
|
|
"severity": "warning",
|
|
|
|
"translation_key": "unfixable_problem",
|
|
|
|
"translation_placeholders": None,
|
|
|
|
},
|
2022-08-05 10:07:51 +00:00
|
|
|
{
|
|
|
|
"breaks_in_ha_version": None,
|
|
|
|
"created": ANY,
|
|
|
|
"dismissed_version": None,
|
|
|
|
"domain": "demo",
|
|
|
|
"ignored": False,
|
|
|
|
"is_fixable": True,
|
|
|
|
"issue_domain": None,
|
|
|
|
"issue_id": "bad_psu",
|
|
|
|
"learn_more_url": "https://www.youtube.com/watch?v=b9rntRxLlbU",
|
|
|
|
"severity": "critical",
|
|
|
|
"translation_key": "bad_psu",
|
|
|
|
"translation_placeholders": None,
|
|
|
|
},
|
2022-11-24 13:54:41 +00:00
|
|
|
{
|
|
|
|
"breaks_in_ha_version": None,
|
|
|
|
"created": ANY,
|
|
|
|
"dismissed_version": None,
|
|
|
|
"domain": "demo",
|
|
|
|
"is_fixable": True,
|
|
|
|
"issue_domain": None,
|
|
|
|
"issue_id": "cold_tea",
|
|
|
|
"learn_more_url": None,
|
|
|
|
"severity": "warning",
|
|
|
|
"translation_key": "cold_tea",
|
|
|
|
"translation_placeholders": None,
|
|
|
|
"ignored": False,
|
|
|
|
},
|
2022-07-20 12:46:06 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
url = "/api/repairs/issues/fix"
|
|
|
|
resp = await client.post(
|
|
|
|
url, json={"handler": "demo", "issue_id": "out_of_blinker_fluid"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert resp.status == HTTPStatus.OK
|
|
|
|
data = await resp.json()
|
|
|
|
|
|
|
|
flow_id = data["flow_id"]
|
|
|
|
assert data == {
|
|
|
|
"data_schema": [],
|
|
|
|
"description_placeholders": None,
|
|
|
|
"errors": None,
|
|
|
|
"flow_id": ANY,
|
|
|
|
"handler": "demo",
|
|
|
|
"last_step": None,
|
|
|
|
"step_id": "confirm",
|
|
|
|
"type": "form",
|
|
|
|
}
|
|
|
|
|
|
|
|
url = f"/api/repairs/issues/fix/{flow_id}"
|
|
|
|
resp = await client.post(url)
|
|
|
|
|
|
|
|
assert resp.status == HTTPStatus.OK
|
|
|
|
data = await resp.json()
|
|
|
|
|
|
|
|
flow_id = data["flow_id"]
|
|
|
|
assert data == {
|
|
|
|
"description": None,
|
|
|
|
"description_placeholders": None,
|
|
|
|
"flow_id": flow_id,
|
|
|
|
"handler": "demo",
|
2022-08-05 10:07:51 +00:00
|
|
|
"title": "",
|
2022-07-20 12:46:06 +00:00
|
|
|
"type": "create_entry",
|
|
|
|
"version": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
await ws_client.send_json({"id": 4, "type": "repairs/list_issues"})
|
|
|
|
msg = await ws_client.receive_json()
|
|
|
|
|
|
|
|
assert msg["success"]
|
|
|
|
assert msg["result"] == {
|
|
|
|
"issues": [
|
|
|
|
{
|
|
|
|
"breaks_in_ha_version": "2023.1.1",
|
|
|
|
"created": ANY,
|
|
|
|
"dismissed_version": None,
|
|
|
|
"domain": "demo",
|
|
|
|
"ignored": False,
|
|
|
|
"is_fixable": False,
|
|
|
|
"issue_id": "transmogrifier_deprecated",
|
2022-07-27 20:53:51 +00:00
|
|
|
"issue_domain": None,
|
2022-07-20 12:46:06 +00:00
|
|
|
"learn_more_url": "https://en.wiktionary.org/wiki/transmogrifier",
|
|
|
|
"severity": "warning",
|
|
|
|
"translation_key": "transmogrifier_deprecated",
|
|
|
|
"translation_placeholders": None,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"breaks_in_ha_version": None,
|
|
|
|
"created": ANY,
|
|
|
|
"dismissed_version": None,
|
|
|
|
"domain": "demo",
|
|
|
|
"ignored": False,
|
|
|
|
"is_fixable": False,
|
|
|
|
"issue_id": "unfixable_problem",
|
2022-07-27 20:53:51 +00:00
|
|
|
"issue_domain": None,
|
2022-07-20 12:46:06 +00:00
|
|
|
"learn_more_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
|
|
|
"severity": "warning",
|
|
|
|
"translation_key": "unfixable_problem",
|
|
|
|
"translation_placeholders": None,
|
|
|
|
},
|
2022-08-05 10:07:51 +00:00
|
|
|
{
|
|
|
|
"breaks_in_ha_version": None,
|
|
|
|
"created": ANY,
|
|
|
|
"dismissed_version": None,
|
|
|
|
"domain": "demo",
|
|
|
|
"ignored": False,
|
|
|
|
"is_fixable": True,
|
|
|
|
"issue_domain": None,
|
|
|
|
"issue_id": "bad_psu",
|
|
|
|
"learn_more_url": "https://www.youtube.com/watch?v=b9rntRxLlbU",
|
|
|
|
"severity": "critical",
|
|
|
|
"translation_key": "bad_psu",
|
|
|
|
"translation_placeholders": None,
|
|
|
|
},
|
2022-11-24 13:54:41 +00:00
|
|
|
{
|
|
|
|
"breaks_in_ha_version": None,
|
|
|
|
"created": ANY,
|
|
|
|
"dismissed_version": None,
|
|
|
|
"domain": "demo",
|
|
|
|
"is_fixable": True,
|
|
|
|
"issue_domain": None,
|
|
|
|
"issue_id": "cold_tea",
|
|
|
|
"learn_more_url": None,
|
|
|
|
"severity": "warning",
|
|
|
|
"translation_key": "cold_tea",
|
|
|
|
"translation_placeholders": None,
|
|
|
|
"ignored": False,
|
|
|
|
},
|
2022-07-20 12:46:06 +00:00
|
|
|
]
|
|
|
|
}
|