diff --git a/homeassistant/components/mold_indicator/config_flow.py b/homeassistant/components/mold_indicator/config_flow.py index ac85d7cc100..96ccbe2f8ee 100644 --- a/homeassistant/components/mold_indicator/config_flow.py +++ b/homeassistant/components/mold_indicator/config_flow.py @@ -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, ) } diff --git a/homeassistant/components/mold_indicator/strings.json b/homeassistant/components/mold_indicator/strings.json index 2e34bcc1ba1..03c6a05546f 100644 --- a/homeassistant/components/mold_indicator/strings.json +++ b/homeassistant/components/mold_indicator/strings.json @@ -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", diff --git a/tests/components/mold_indicator/test_config_flow.py b/tests/components/mold_indicator/test_config_flow.py index 7a766be11f5..339cb3a02e7 100644 --- a/tests/components/mold_indicator/test_config_flow.py +++ b/tests/components/mold_indicator/test_config_flow.py @@ -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: