hass-core/tests/components/trend/test_config_flow.py
Jan-Philipp Benecke e04fda3fad
Add config flow to trend (#99761)
* Add config flow to trend

* Remove device_class from options flow

* Add min_samples and import step to config flow

* Fix import

* Fixing tests and some cleanup

* remove unneeded usefixtures

* Apply code review suggestions

* Re-add YAML support

* Re-add reload service

* Fix import

* Apply code review suggestions

* Add test coverage for yaml setup

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
2023-12-27 14:46:57 +01:00

80 lines
2.4 KiB
Python

"""Test the Trend config flow."""
from __future__ import annotations
from unittest.mock import patch
from homeassistant import config_entries
from homeassistant.components.trend import async_setup_entry
from homeassistant.components.trend.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
async def test_form(hass: HomeAssistant) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["step_id"] == "user"
assert result["type"] == FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"name": "CPU Temperature rising", "entity_id": "sensor.cpu_temp"},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
# test step 2 of config flow: settings of trend sensor
with patch(
"homeassistant.components.trend.async_setup_entry", wraps=async_setup_entry
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"invert": False,
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "CPU Temperature rising"
assert result["data"] == {}
assert result["options"] == {
"entity_id": "sensor.cpu_temp",
"invert": False,
"name": "CPU Temperature rising",
}
async def test_options(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
"""Test options flow."""
config_entry.add_to_hass(hass)
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
{
"min_samples": 30,
"max_samples": 50,
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["data"] == {
"min_samples": 30,
"max_samples": 50,
"entity_id": "sensor.cpu_temp",
"invert": False,
"min_gradient": 0.0,
"name": "My trend",
"sample_duration": 0.0,
}