Fix Melcloud import issue (#97673)

* Fix Melcloud import issue

* remove old issue

* Simplify

* Update homeassistant/components/melcloud/strings.json

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/melcloud/strings.json

* Update homeassistant/components/melcloud/strings.json

* Apply suggestions from code review

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
G Johansson 2023-08-05 17:00:33 +02:00 committed by GitHub
parent 2e263560ec
commit a22aa285d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 174 additions and 25 deletions

View file

@ -11,11 +11,47 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
from homeassistant.data_entry_flow import AbortFlow, FlowResultType
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from .const import DOMAIN
async def async_create_import_issue(
hass: HomeAssistant, source: str, issue: str, success: bool = False
) -> None:
"""Create issue from import."""
if source != config_entries.SOURCE_IMPORT:
return
if not success:
async_create_issue(
hass,
DOMAIN,
f"deprecated_yaml_import_issue_{issue}",
breaks_in_ha_version="2024.2.0",
is_fixable=False,
severity=IssueSeverity.ERROR,
translation_key=f"deprecated_yaml_import_issue_{issue}",
)
return
async_create_issue(
hass,
HOMEASSISTANT_DOMAIN,
f"deprecated_yaml_{DOMAIN}",
breaks_in_ha_version="2024.2.0",
is_fixable=False,
issue_domain=DOMAIN,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
translation_placeholders={
"domain": DOMAIN,
"integration_title": "MELCloud",
},
)
class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow."""
@ -24,7 +60,11 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async def _create_entry(self, username: str, token: str):
"""Register new entry."""
await self.async_set_unique_id(username)
self._abort_if_unique_id_configured({CONF_TOKEN: token})
try:
self._abort_if_unique_id_configured({CONF_TOKEN: token})
except AbortFlow:
await async_create_import_issue(self.hass, self.context["source"], "", True)
raise
return self.async_create_entry(
title=username, data={CONF_USERNAME: username, CONF_TOKEN: token}
)
@ -37,11 +77,6 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
token: str | None = None,
):
"""Create client."""
if password is None and token is None:
raise ValueError(
"Invalid internal state. Called without either password or token"
)
try:
async with timeout(10):
if (acquired_token := token) is None:
@ -56,9 +91,18 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
)
except ClientResponseError as err:
if err.status in (HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN):
await async_create_import_issue(
self.hass, self.context["source"], "invalid_auth"
)
return self.async_abort(reason="invalid_auth")
await async_create_import_issue(
self.hass, self.context["source"], "cannot_connect"
)
return self.async_abort(reason="cannot_connect")
except (asyncio.TimeoutError, ClientError):
await async_create_import_issue(
self.hass, self.context["source"], "cannot_connect"
)
return self.async_abort(reason="cannot_connect")
return await self._create_entry(username, acquired_token)
@ -77,6 +121,9 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_import(self, user_input):
"""Import a config entry."""
return await self._create_client(
result = await self._create_client(
user_input[CONF_USERNAME], token=user_input[CONF_TOKEN]
)
if result["type"] == FlowResultType.CREATE_ENTRY:
await async_create_import_issue(self.hass, self.context["source"], "", True)
return result