Reduce code duplication in Suez config flow (#105558)
parent
a91dfc7954
commit
06f81251fb
|
@ -10,16 +10,13 @@ import voluptuous as vol
|
|||
|
||||
from homeassistant.config_entries import ConfigFlow
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN
|
||||
from homeassistant.data_entry_flow import AbortFlow, FlowResult
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||
|
||||
from .const import CONF_COUNTER_ID, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ISSUE_PLACEHOLDER = {"url": "/config/integrations/dashboard/add?domain=suez_water"}
|
||||
STEP_USER_DATA_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_USERNAME): str,
|
||||
|
@ -81,80 +78,16 @@ class SuezWaterConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||
async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
|
||||
"""Import the yaml config."""
|
||||
await self.async_set_unique_id(user_input[CONF_USERNAME])
|
||||
try:
|
||||
self._abort_if_unique_id_configured()
|
||||
except AbortFlow as err:
|
||||
async_create_issue(
|
||||
self.hass,
|
||||
HOMEASSISTANT_DOMAIN,
|
||||
f"deprecated_yaml_{DOMAIN}",
|
||||
breaks_in_ha_version="2024.7.0",
|
||||
is_fixable=False,
|
||||
issue_domain=DOMAIN,
|
||||
severity=IssueSeverity.WARNING,
|
||||
translation_key="deprecated_yaml",
|
||||
translation_placeholders={
|
||||
"domain": DOMAIN,
|
||||
"integration_title": "Suez Water",
|
||||
},
|
||||
)
|
||||
raise err
|
||||
self._abort_if_unique_id_configured()
|
||||
try:
|
||||
await self.hass.async_add_executor_job(validate_input, user_input)
|
||||
except CannotConnect:
|
||||
async_create_issue(
|
||||
self.hass,
|
||||
DOMAIN,
|
||||
"deprecated_yaml_import_issue_cannot_connect",
|
||||
breaks_in_ha_version="2024.7.0",
|
||||
is_fixable=False,
|
||||
issue_domain=DOMAIN,
|
||||
severity=IssueSeverity.WARNING,
|
||||
translation_key="deprecated_yaml_import_issue_cannot_connect",
|
||||
translation_placeholders=ISSUE_PLACEHOLDER,
|
||||
)
|
||||
return self.async_abort(reason="cannot_connect")
|
||||
except InvalidAuth:
|
||||
async_create_issue(
|
||||
self.hass,
|
||||
DOMAIN,
|
||||
"deprecated_yaml_import_issue_invalid_auth",
|
||||
breaks_in_ha_version="2024.7.0",
|
||||
is_fixable=False,
|
||||
issue_domain=DOMAIN,
|
||||
severity=IssueSeverity.WARNING,
|
||||
translation_key="deprecated_yaml_import_issue_invalid_auth",
|
||||
translation_placeholders=ISSUE_PLACEHOLDER,
|
||||
)
|
||||
return self.async_abort(reason="invalid_auth")
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
async_create_issue(
|
||||
self.hass,
|
||||
DOMAIN,
|
||||
"deprecated_yaml_import_issue_unknown",
|
||||
breaks_in_ha_version="2024.7.0",
|
||||
is_fixable=False,
|
||||
issue_domain=DOMAIN,
|
||||
severity=IssueSeverity.WARNING,
|
||||
translation_key="deprecated_yaml_import_issue_unknown",
|
||||
translation_placeholders=ISSUE_PLACEHOLDER,
|
||||
)
|
||||
return self.async_abort(reason="unknown")
|
||||
async_create_issue(
|
||||
self.hass,
|
||||
HOMEASSISTANT_DOMAIN,
|
||||
f"deprecated_yaml_{DOMAIN}",
|
||||
breaks_in_ha_version="2024.7.0",
|
||||
is_fixable=False,
|
||||
issue_domain=DOMAIN,
|
||||
severity=IssueSeverity.WARNING,
|
||||
translation_key="deprecated_yaml",
|
||||
translation_placeholders={
|
||||
"domain": DOMAIN,
|
||||
"integration_title": "Suez Water",
|
||||
},
|
||||
)
|
||||
return self.async_create_entry(title=user_input[CONF_USERNAME], data=user_input)
|
||||
|
||||
|
||||
|
|
|
@ -15,14 +15,17 @@ from homeassistant.components.sensor import (
|
|||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, UnitOfVolume
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import CONF_COUNTER_ID, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
ISSUE_PLACEHOLDER = {"url": "/config/integrations/dashboard/add?domain=suez_water"}
|
||||
|
||||
SCAN_INTERVAL = timedelta(hours=12)
|
||||
|
||||
|
@ -35,20 +38,48 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
)
|
||||
|
||||
|
||||
def setup_platform(
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
add_entities: AddEntitiesCallback,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the sensor platform."""
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=config,
|
||||
)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=config,
|
||||
)
|
||||
if (
|
||||
result["type"] == FlowResultType.CREATE_ENTRY
|
||||
or result["reason"] == "already_configured"
|
||||
):
|
||||
async_create_issue(
|
||||
hass,
|
||||
HOMEASSISTANT_DOMAIN,
|
||||
f"deprecated_yaml_{DOMAIN}",
|
||||
breaks_in_ha_version="2024.7.0",
|
||||
is_fixable=False,
|
||||
issue_domain=DOMAIN,
|
||||
severity=IssueSeverity.WARNING,
|
||||
translation_key="deprecated_yaml",
|
||||
translation_placeholders={
|
||||
"domain": DOMAIN,
|
||||
"integration_title": "Suez Water",
|
||||
},
|
||||
)
|
||||
else:
|
||||
async_create_issue(
|
||||
hass,
|
||||
DOMAIN,
|
||||
f"deprecated_yaml_import_issue_${result['reason']}",
|
||||
breaks_in_ha_version="2024.7.0",
|
||||
is_fixable=False,
|
||||
issue_domain=DOMAIN,
|
||||
severity=IssueSeverity.WARNING,
|
||||
translation_key=f"deprecated_yaml_import_issue_${result['reason']}",
|
||||
translation_placeholders=ISSUE_PLACEHOLDER,
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
|
|
|
@ -8,7 +8,6 @@ from homeassistant import config_entries
|
|||
from homeassistant.components.suez_water.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.helpers import issue_registry as ir
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
@ -138,9 +137,7 @@ async def test_form_error(
|
|||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock, issue_registry: ir.IssueRegistry
|
||||
) -> None:
|
||||
async def test_import(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
||||
"""Test import flow."""
|
||||
with patch("homeassistant.components.suez_water.config_flow.SuezClient"):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
|
@ -153,7 +150,6 @@ async def test_import(
|
|||
assert result["result"].unique_id == "test-username"
|
||||
assert result["data"] == MOCK_DATA
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
assert len(issue_registry.issues) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
@ -164,7 +160,6 @@ async def test_import_error(
|
|||
mock_setup_entry: AsyncMock,
|
||||
exception: Exception,
|
||||
reason: str,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
) -> None:
|
||||
"""Test we handle errors while importing."""
|
||||
|
||||
|
@ -178,12 +173,9 @@ async def test_import_error(
|
|||
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == reason
|
||||
assert len(issue_registry.issues) == 1
|
||||
|
||||
|
||||
async def test_importing_invalid_auth(
|
||||
hass: HomeAssistant, issue_registry: ir.IssueRegistry
|
||||
) -> None:
|
||||
async def test_importing_invalid_auth(hass: HomeAssistant) -> None:
|
||||
"""Test we handle invalid auth when importing."""
|
||||
|
||||
with patch(
|
||||
|
@ -199,12 +191,9 @@ async def test_importing_invalid_auth(
|
|||
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "invalid_auth"
|
||||
assert len(issue_registry.issues) == 1
|
||||
|
||||
|
||||
async def test_import_already_configured(
|
||||
hass: HomeAssistant, issue_registry: ir.IssueRegistry
|
||||
) -> None:
|
||||
async def test_import_already_configured(hass: HomeAssistant) -> None:
|
||||
"""Test we abort import when entry is already configured."""
|
||||
|
||||
entry = MockConfigEntry(
|
||||
|
@ -220,4 +209,3 @@ async def test_import_already_configured(
|
|||
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
assert len(issue_registry.issues) == 1
|
||||
|
|
Loading…
Reference in New Issue