mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
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
fdd9fca5b3
commit
de6ca56504
@ -15,6 +15,7 @@ from homeassistant.exceptions import HomeAssistantError
|
|||||||
from homeassistant.helpers.schema_config_entry_flow import (
|
from homeassistant.helpers.schema_config_entry_flow import (
|
||||||
SchemaCommonFlowHandler,
|
SchemaCommonFlowHandler,
|
||||||
SchemaConfigFlowHandler,
|
SchemaConfigFlowHandler,
|
||||||
|
SchemaFlowError,
|
||||||
SchemaFlowFormStep,
|
SchemaFlowFormStep,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.selector import (
|
from homeassistant.helpers.selector import (
|
||||||
@ -38,11 +39,13 @@ from .const import (
|
|||||||
from .sensor import MoldIndicator
|
from .sensor import MoldIndicator
|
||||||
|
|
||||||
|
|
||||||
async def validate_duplicate(
|
async def validate_input(
|
||||||
handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
|
handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Validate already existing entry."""
|
"""Validate already existing entry."""
|
||||||
handler.parent_handler._async_abort_entries_match({**handler.options, **user_input}) # noqa: SLF001
|
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
|
return user_input
|
||||||
|
|
||||||
|
|
||||||
@ -79,14 +82,14 @@ DATA_SCHEMA_CONFIG = vol.Schema(
|
|||||||
CONFIG_FLOW = {
|
CONFIG_FLOW = {
|
||||||
"user": SchemaFlowFormStep(
|
"user": SchemaFlowFormStep(
|
||||||
schema=DATA_SCHEMA_CONFIG,
|
schema=DATA_SCHEMA_CONFIG,
|
||||||
validate_user_input=validate_duplicate,
|
validate_user_input=validate_input,
|
||||||
preview="mold_indicator",
|
preview="mold_indicator",
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
OPTIONS_FLOW = {
|
OPTIONS_FLOW = {
|
||||||
"init": SchemaFlowFormStep(
|
"init": SchemaFlowFormStep(
|
||||||
DATA_SCHEMA_OPTIONS,
|
DATA_SCHEMA_OPTIONS,
|
||||||
validate_user_input=validate_duplicate,
|
validate_user_input=validate_input,
|
||||||
preview="mold_indicator",
|
preview="mold_indicator",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,9 @@
|
|||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||||
},
|
},
|
||||||
|
"error": {
|
||||||
|
"calibration_is_zero": "Calibration factor can't be zero."
|
||||||
|
},
|
||||||
"step": {
|
"step": {
|
||||||
"user": {
|
"user": {
|
||||||
"description": "Add Mold indicator helper",
|
"description": "Add Mold indicator helper",
|
||||||
@ -27,6 +30,9 @@
|
|||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||||
},
|
},
|
||||||
|
"error": {
|
||||||
|
"calibration_is_zero": "Calibration factor can't be zero."
|
||||||
|
},
|
||||||
"step": {
|
"step": {
|
||||||
"init": {
|
"init": {
|
||||||
"description": "Adjust the calibration factor as required",
|
"description": "Adjust the calibration factor as required",
|
||||||
|
@ -94,6 +94,52 @@ async def test_options_flow(hass: HomeAssistant, loaded_entry: MockConfigEntry)
|
|||||||
assert state is not None
|
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(
|
async def test_entry_already_exist(
|
||||||
hass: HomeAssistant, loaded_entry: MockConfigEntry
|
hass: HomeAssistant, loaded_entry: MockConfigEntry
|
||||||
) -> None:
|
) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user