diff --git a/.coveragerc b/.coveragerc index 47376833679..3c9a1c378a8 100644 --- a/.coveragerc +++ b/.coveragerc @@ -807,6 +807,7 @@ omit = homeassistant/components/nuki/sensor.py homeassistant/components/nx584/alarm_control_panel.py homeassistant/components/oasa_telematics/sensor.py + homeassistant/components/obihai/__init__.py homeassistant/components/obihai/connectivity.py homeassistant/components/obihai/sensor.py homeassistant/components/octoprint/__init__.py diff --git a/homeassistant/components/obihai/config_flow.py b/homeassistant/components/obihai/config_flow.py index dd2aa0db06d..2f8dd0075b8 100644 --- a/homeassistant/components/obihai/config_flow.py +++ b/homeassistant/components/obihai/config_flow.py @@ -7,6 +7,7 @@ import voluptuous as vol from homeassistant.config_entries import ConfigFlow from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME +from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResult from .connectivity import validate_auth @@ -27,6 +28,16 @@ DATA_SCHEMA = vol.Schema( ) +async def async_validate_creds(hass: HomeAssistant, user_input: dict[str, Any]) -> bool: + """Manage Obihai options.""" + return await hass.async_add_executor_job( + validate_auth, + user_input[CONF_HOST], + user_input[CONF_USERNAME], + user_input[CONF_PASSWORD], + ) + + class ObihaiFlowHandler(ConfigFlow, domain=DOMAIN): """Config flow for Obihai.""" @@ -40,12 +51,7 @@ class ObihaiFlowHandler(ConfigFlow, domain=DOMAIN): if user_input is not None: self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]}) - if await self.hass.async_add_executor_job( - validate_auth, - user_input[CONF_HOST], - user_input[CONF_USERNAME], - user_input[CONF_PASSWORD], - ): + if await async_validate_creds(self.hass, user_input): return self.async_create_entry( title=user_input[CONF_HOST], data=user_input, @@ -63,11 +69,14 @@ class ObihaiFlowHandler(ConfigFlow, domain=DOMAIN): async def async_step_import(self, config: dict[str, Any]) -> FlowResult: """Handle a flow initialized by importing a config.""" self._async_abort_entries_match({CONF_HOST: config[CONF_HOST]}) - return self.async_create_entry( - title=config.get(CONF_NAME, config[CONF_HOST]), - data={ - CONF_HOST: config[CONF_HOST], - CONF_PASSWORD: config[CONF_PASSWORD], - CONF_USERNAME: config[CONF_USERNAME], - }, - ) + if await async_validate_creds(self.hass, config): + return self.async_create_entry( + title=config.get(CONF_NAME, config[CONF_HOST]), + data={ + CONF_HOST: config[CONF_HOST], + CONF_PASSWORD: config[CONF_PASSWORD], + CONF_USERNAME: config[CONF_USERNAME], + }, + ) + + return self.async_abort(reason="cannot_connect") diff --git a/homeassistant/components/obihai/sensor.py b/homeassistant/components/obihai/sensor.py index 4f7b6195e4e..7524fbc7d47 100644 --- a/homeassistant/components/obihai/sensor.py +++ b/homeassistant/components/obihai/sensor.py @@ -46,7 +46,7 @@ async def async_setup_platform( "manual_migration", breaks_in_ha_version="2023.6.0", is_fixable=False, - severity=ir.IssueSeverity.ERROR, + severity=ir.IssueSeverity.WARNING, translation_key="manual_migration", ) diff --git a/homeassistant/components/obihai/strings.json b/homeassistant/components/obihai/strings.json index 053343b4501..fb673675ad7 100644 --- a/homeassistant/components/obihai/strings.json +++ b/homeassistant/components/obihai/strings.json @@ -18,7 +18,7 @@ }, "issues": { "manual_migration": { - "title": "Manual migration required for Obihai", + "title": "Obihai YAML configuration is being removed", "description": "Configuration of the Obihai platform in YAML is deprecated and will be removed in Home Assistant 2023.6; Your existing configuration has been imported into the UI automatically and can be safely removed from your configuration.yaml file." } } diff --git a/tests/components/obihai/test_config_flow.py b/tests/components/obihai/test_config_flow.py index 07d00f15775..234f1d59967 100644 --- a/tests/components/obihai/test_config_flow.py +++ b/tests/components/obihai/test_config_flow.py @@ -10,6 +10,8 @@ from homeassistant.data_entry_flow import FlowResultType from . import USER_INPUT +VALIDATE_AUTH_PATCH = "homeassistant.components.obihai.config_flow.validate_auth" + pytestmark = pytest.mark.usefixtures("mock_setup_entry") @@ -43,9 +45,7 @@ async def test_auth_failure(hass: HomeAssistant) -> None: DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with patch( - "homeassistant.components.obihai.config_flow.validate_auth", return_value=False - ): + with patch(VALIDATE_AUTH_PATCH, return_value=False): result = await hass.config_entries.flow.async_configure( result["flow_id"], USER_INPUT, @@ -59,9 +59,7 @@ async def test_auth_failure(hass: HomeAssistant) -> None: async def test_yaml_import(hass: HomeAssistant) -> None: """Test we get the YAML imported.""" - with patch( - "homeassistant.components.obihai.config_flow.validate_auth", return_value=True - ): + with patch(VALIDATE_AUTH_PATCH, return_value=True): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, @@ -71,3 +69,18 @@ async def test_yaml_import(hass: HomeAssistant) -> None: assert result["type"] == FlowResultType.CREATE_ENTRY assert "errors" not in result + + +async def test_yaml_import_fail(hass: HomeAssistant) -> None: + """Test the YAML import fails.""" + with patch(VALIDATE_AUTH_PATCH, return_value=False): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_IMPORT}, + data=USER_INPUT, + ) + await hass.async_block_till_done() + + assert result["type"] == FlowResultType.ABORT + assert result["reason"] == "cannot_connect" + assert "errors" not in result