Add config flow validation that calibration factor is not zero (#127136)
* Add config flow validation that calibration factor is not zero * Add test
This commit is contained in:
parent
60dfccb747
commit
6f5eac3143
3 changed files with 58 additions and 3 deletions
|
@ -12,6 +12,7 @@ from homeassistant.const import CONF_NAME, Platform
|
|||
from homeassistant.helpers.schema_config_entry_flow import (
|
||||
SchemaCommonFlowHandler,
|
||||
SchemaConfigFlowHandler,
|
||||
SchemaFlowError,
|
||||
SchemaFlowFormStep,
|
||||
)
|
||||
from homeassistant.helpers.selector import (
|
||||
|
@ -33,11 +34,13 @@ from .const import (
|
|||
)
|
||||
|
||||
|
||||
async def validate_duplicate(
|
||||
async def validate_input(
|
||||
handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
|
||||
) -> dict[str, Any]:
|
||||
"""Validate already existing entry."""
|
||||
handler.parent_handler._async_abort_entries_match({**handler.options, **user_input}) # noqa: SLF001
|
||||
if user_input[CONF_CALIBRATION_FACTOR] == 0.0:
|
||||
raise SchemaFlowError("calibration_is_zero")
|
||||
return user_input
|
||||
|
||||
|
||||
|
@ -74,13 +77,13 @@ DATA_SCHEMA_CONFIG = vol.Schema(
|
|||
CONFIG_FLOW = {
|
||||
"user": SchemaFlowFormStep(
|
||||
schema=DATA_SCHEMA_CONFIG,
|
||||
validate_user_input=validate_duplicate,
|
||||
validate_user_input=validate_input,
|
||||
),
|
||||
}
|
||||
OPTIONS_FLOW = {
|
||||
"init": SchemaFlowFormStep(
|
||||
DATA_SCHEMA_OPTIONS,
|
||||
validate_user_input=validate_duplicate,
|
||||
validate_user_input=validate_input,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||
},
|
||||
"error": {
|
||||
"calibration_is_zero": "Calibration factor can't be zero."
|
||||
},
|
||||
"step": {
|
||||
"user": {
|
||||
"description": "Add Mold indicator helper",
|
||||
|
@ -27,6 +30,9 @@
|
|||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||
},
|
||||
"error": {
|
||||
"calibration_is_zero": "Calibration factor can't be zero."
|
||||
},
|
||||
"step": {
|
||||
"init": {
|
||||
"description": "Adjust the calibration factor as required",
|
||||
|
|
|
@ -89,6 +89,52 @@ async def test_options_flow(hass: HomeAssistant, loaded_entry: MockConfigEntry)
|
|||
assert state is not None
|
||||
|
||||
|
||||
async def test_calibration_factor_not_zero(hass: HomeAssistant) -> None:
|
||||
"""Test calibration factor is not zero."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["step_id"] == "user"
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_INDOOR_TEMP: "sensor.indoor_temp",
|
||||
CONF_INDOOR_HUMIDITY: "sensor.indoor_humidity",
|
||||
CONF_OUTDOOR_TEMP: "sensor.outdoor_temp",
|
||||
CONF_CALIBRATION_FACTOR: 0.0,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "calibration_is_zero"}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_INDOOR_TEMP: "sensor.indoor_temp",
|
||||
CONF_INDOOR_HUMIDITY: "sensor.indoor_humidity",
|
||||
CONF_OUTDOOR_TEMP: "sensor.outdoor_temp",
|
||||
CONF_CALIBRATION_FACTOR: 1.0,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["options"] == {
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_INDOOR_TEMP: "sensor.indoor_temp",
|
||||
CONF_INDOOR_HUMIDITY: "sensor.indoor_humidity",
|
||||
CONF_OUTDOOR_TEMP: "sensor.outdoor_temp",
|
||||
CONF_CALIBRATION_FACTOR: 1.0,
|
||||
}
|
||||
|
||||
|
||||
async def test_entry_already_exist(
|
||||
hass: HomeAssistant, loaded_entry: MockConfigEntry
|
||||
) -> None:
|
||||
|
|
Loading…
Add table
Reference in a new issue