Obihai config flow fixes (#88853)

* Commit split issue

* Clearer name

* Add yaml_failure test case

* Not sure why this is failing now

* Update homeassistant/components/obihai/strings.json

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* PR Feedback

* Update homeassistant/components/obihai/config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
pull/88977/head
Emory Penney 2023-03-01 07:33:32 -08:00 committed by GitHub
parent 79bcdf43f7
commit 137d2f0d73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 22 deletions

View File

@ -807,6 +807,7 @@ omit =
homeassistant/components/nuki/sensor.py homeassistant/components/nuki/sensor.py
homeassistant/components/nx584/alarm_control_panel.py homeassistant/components/nx584/alarm_control_panel.py
homeassistant/components/oasa_telematics/sensor.py homeassistant/components/oasa_telematics/sensor.py
homeassistant/components/obihai/__init__.py
homeassistant/components/obihai/connectivity.py homeassistant/components/obihai/connectivity.py
homeassistant/components/obihai/sensor.py homeassistant/components/obihai/sensor.py
homeassistant/components/octoprint/__init__.py homeassistant/components/octoprint/__init__.py

View File

@ -7,6 +7,7 @@ import voluptuous as vol
from homeassistant.config_entries import ConfigFlow from homeassistant.config_entries import ConfigFlow
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME 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 homeassistant.data_entry_flow import FlowResult
from .connectivity import validate_auth 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): class ObihaiFlowHandler(ConfigFlow, domain=DOMAIN):
"""Config flow for Obihai.""" """Config flow for Obihai."""
@ -40,12 +51,7 @@ class ObihaiFlowHandler(ConfigFlow, domain=DOMAIN):
if user_input is not None: if user_input is not None:
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]}) self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
if await self.hass.async_add_executor_job( if await async_validate_creds(self.hass, user_input):
validate_auth,
user_input[CONF_HOST],
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
):
return self.async_create_entry( return self.async_create_entry(
title=user_input[CONF_HOST], title=user_input[CONF_HOST],
data=user_input, data=user_input,
@ -63,11 +69,14 @@ class ObihaiFlowHandler(ConfigFlow, domain=DOMAIN):
async def async_step_import(self, config: dict[str, Any]) -> FlowResult: async def async_step_import(self, config: dict[str, Any]) -> FlowResult:
"""Handle a flow initialized by importing a config.""" """Handle a flow initialized by importing a config."""
self._async_abort_entries_match({CONF_HOST: config[CONF_HOST]}) self._async_abort_entries_match({CONF_HOST: config[CONF_HOST]})
return self.async_create_entry( if await async_validate_creds(self.hass, config):
title=config.get(CONF_NAME, config[CONF_HOST]), return self.async_create_entry(
data={ title=config.get(CONF_NAME, config[CONF_HOST]),
CONF_HOST: config[CONF_HOST], data={
CONF_PASSWORD: config[CONF_PASSWORD], CONF_HOST: config[CONF_HOST],
CONF_USERNAME: config[CONF_USERNAME], CONF_PASSWORD: config[CONF_PASSWORD],
}, CONF_USERNAME: config[CONF_USERNAME],
) },
)
return self.async_abort(reason="cannot_connect")

View File

@ -46,7 +46,7 @@ async def async_setup_platform(
"manual_migration", "manual_migration",
breaks_in_ha_version="2023.6.0", breaks_in_ha_version="2023.6.0",
is_fixable=False, is_fixable=False,
severity=ir.IssueSeverity.ERROR, severity=ir.IssueSeverity.WARNING,
translation_key="manual_migration", translation_key="manual_migration",
) )

View File

@ -18,7 +18,7 @@
}, },
"issues": { "issues": {
"manual_migration": { "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." "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."
} }
} }

View File

@ -10,6 +10,8 @@ from homeassistant.data_entry_flow import FlowResultType
from . import USER_INPUT from . import USER_INPUT
VALIDATE_AUTH_PATCH = "homeassistant.components.obihai.config_flow.validate_auth"
pytestmark = pytest.mark.usefixtures("mock_setup_entry") 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} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
with patch( with patch(VALIDATE_AUTH_PATCH, return_value=False):
"homeassistant.components.obihai.config_flow.validate_auth", return_value=False
):
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
USER_INPUT, USER_INPUT,
@ -59,9 +59,7 @@ async def test_auth_failure(hass: HomeAssistant) -> None:
async def test_yaml_import(hass: HomeAssistant) -> None: async def test_yaml_import(hass: HomeAssistant) -> None:
"""Test we get the YAML imported.""" """Test we get the YAML imported."""
with patch( with patch(VALIDATE_AUTH_PATCH, return_value=True):
"homeassistant.components.obihai.config_flow.validate_auth", return_value=True
):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_IMPORT}, 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 result["type"] == FlowResultType.CREATE_ENTRY
assert "errors" not in result 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