mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 23:07:09 +00:00
Fix surepetcare token update (#126385)
Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
parent
e8a5a75e96
commit
ccec85f047
@ -10,9 +10,8 @@ import surepy
|
|||||||
from surepy.exceptions import SurePetcareAuthenticationError, SurePetcareError
|
from surepy.exceptions import SurePetcareAuthenticationError, SurePetcareError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import DOMAIN, SURE_API_TIMEOUT
|
from .const import DOMAIN, SURE_API_TIMEOUT
|
||||||
@ -27,41 +26,28 @@ USER_DATA_SCHEMA = vol.Schema(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
"""Validate the user input allows us to connect."""
|
|
||||||
surepy_client = surepy.Surepy(
|
|
||||||
data[CONF_USERNAME],
|
|
||||||
data[CONF_PASSWORD],
|
|
||||||
auth_token=None,
|
|
||||||
api_timeout=SURE_API_TIMEOUT,
|
|
||||||
session=async_get_clientsession(hass),
|
|
||||||
)
|
|
||||||
|
|
||||||
token = await surepy_client.sac.get_token()
|
|
||||||
|
|
||||||
return {CONF_TOKEN: token}
|
|
||||||
|
|
||||||
|
|
||||||
class SurePetCareConfigFlow(ConfigFlow, domain=DOMAIN):
|
class SurePetCareConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow for Sure Petcare."""
|
"""Handle a config flow for Sure Petcare."""
|
||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
def __init__(self) -> None:
|
reauth_entry: ConfigEntry | None = None
|
||||||
"""Initialize."""
|
|
||||||
self._username: str | None = None
|
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
if user_input is None:
|
|
||||||
return self.async_show_form(step_id="user", data_schema=USER_DATA_SCHEMA)
|
|
||||||
|
|
||||||
errors = {}
|
errors = {}
|
||||||
|
if user_input is not None:
|
||||||
|
client = surepy.Surepy(
|
||||||
|
user_input[CONF_USERNAME],
|
||||||
|
user_input[CONF_PASSWORD],
|
||||||
|
auth_token=None,
|
||||||
|
api_timeout=SURE_API_TIMEOUT,
|
||||||
|
session=async_get_clientsession(self.hass),
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
info = await validate_input(self.hass, user_input)
|
token = await client.sac.get_token()
|
||||||
except SurePetcareAuthenticationError:
|
except SurePetcareAuthenticationError:
|
||||||
errors["base"] = "invalid_auth"
|
errors["base"] = "invalid_auth"
|
||||||
except SurePetcareError:
|
except SurePetcareError:
|
||||||
@ -73,10 +59,9 @@ class SurePetCareConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
await self.async_set_unique_id(user_input[CONF_USERNAME].lower())
|
await self.async_set_unique_id(user_input[CONF_USERNAME].lower())
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
user_input[CONF_TOKEN] = info[CONF_TOKEN]
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title="Sure Petcare",
|
title="Sure Petcare",
|
||||||
data=user_input,
|
data={**user_input, CONF_TOKEN: token},
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
@ -87,18 +72,27 @@ class SurePetCareConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
self, entry_data: Mapping[str, Any]
|
self, entry_data: Mapping[str, Any]
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle configuration by re-auth."""
|
"""Handle configuration by re-auth."""
|
||||||
self._username = entry_data[CONF_USERNAME]
|
self.reauth_entry = self.hass.config_entries.async_get_entry(
|
||||||
|
self.context["entry_id"]
|
||||||
|
)
|
||||||
return await self.async_step_reauth_confirm()
|
return await self.async_step_reauth_confirm()
|
||||||
|
|
||||||
async def async_step_reauth_confirm(
|
async def async_step_reauth_confirm(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Dialog that informs the user that reauth is required."""
|
"""Dialog that informs the user that reauth is required."""
|
||||||
|
assert self.reauth_entry
|
||||||
errors = {}
|
errors = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
user_input[CONF_USERNAME] = self._username
|
client = surepy.Surepy(
|
||||||
|
self.reauth_entry.data[CONF_USERNAME],
|
||||||
|
user_input[CONF_PASSWORD],
|
||||||
|
auth_token=None,
|
||||||
|
api_timeout=SURE_API_TIMEOUT,
|
||||||
|
session=async_get_clientsession(self.hass),
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
await validate_input(self.hass, user_input)
|
token = await client.sac.get_token()
|
||||||
except SurePetcareAuthenticationError:
|
except SurePetcareAuthenticationError:
|
||||||
errors["base"] = "invalid_auth"
|
errors["base"] = "invalid_auth"
|
||||||
except SurePetcareError:
|
except SurePetcareError:
|
||||||
@ -107,16 +101,20 @@ class SurePetCareConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
_LOGGER.exception("Unexpected exception")
|
_LOGGER.exception("Unexpected exception")
|
||||||
errors["base"] = "unknown"
|
errors["base"] = "unknown"
|
||||||
else:
|
else:
|
||||||
existing_entry = await self.async_set_unique_id(
|
return self.async_update_reload_and_abort(
|
||||||
user_input[CONF_USERNAME].lower()
|
self.reauth_entry,
|
||||||
|
data={
|
||||||
|
**self.reauth_entry.data,
|
||||||
|
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
||||||
|
CONF_TOKEN: token,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if existing_entry:
|
|
||||||
await self.hass.config_entries.async_reload(existing_entry.entry_id)
|
|
||||||
return self.async_abort(reason="reauth_successful")
|
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="reauth_confirm",
|
step_id="reauth_confirm",
|
||||||
description_placeholders={"username": self._username},
|
description_placeholders={
|
||||||
|
"username": self.reauth_entry.data[CONF_USERNAME]
|
||||||
|
},
|
||||||
data_schema=vol.Schema({vol.Required(CONF_PASSWORD): str}),
|
data_schema=vol.Schema({vol.Required(CONF_PASSWORD): str}),
|
||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
@ -6,6 +6,7 @@ from surepy.exceptions import SurePetcareAuthenticationError, SurePetcareError
|
|||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components.surepetcare.const import DOMAIN
|
from homeassistant.components.surepetcare.const import DOMAIN
|
||||||
|
from homeassistant.const import CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
@ -24,7 +25,7 @@ async def test_form(hass: HomeAssistant, surepetcare: NonCallableMagicMock) -> N
|
|||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["errors"] is None
|
assert not result["errors"]
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.surepetcare.async_setup_entry",
|
"homeassistant.components.surepetcare.async_setup_entry",
|
||||||
@ -146,11 +147,17 @@ async def test_flow_entry_already_exists(
|
|||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
async def test_reauthentication(hass: HomeAssistant) -> None:
|
async def test_reauthentication(
|
||||||
|
hass: HomeAssistant, surepetcare: NonCallableMagicMock
|
||||||
|
) -> None:
|
||||||
"""Test surepetcare reauthentication."""
|
"""Test surepetcare reauthentication."""
|
||||||
old_entry = MockConfigEntry(
|
old_entry = MockConfigEntry(
|
||||||
domain="surepetcare",
|
domain="surepetcare",
|
||||||
data=INPUT_DATA,
|
data={
|
||||||
|
CONF_USERNAME: "test-username",
|
||||||
|
CONF_PASSWORD: "test-password",
|
||||||
|
CONF_TOKEN: "token",
|
||||||
|
},
|
||||||
unique_id="test-username",
|
unique_id="test-username",
|
||||||
)
|
)
|
||||||
old_entry.add_to_hass(hass)
|
old_entry.add_to_hass(hass)
|
||||||
@ -161,19 +168,23 @@ async def test_reauthentication(hass: HomeAssistant) -> None:
|
|||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
assert result["step_id"] == "reauth_confirm"
|
assert result["step_id"] == "reauth_confirm"
|
||||||
|
|
||||||
with patch(
|
surepetcare.get_token.return_value = "token2"
|
||||||
"homeassistant.components.surepetcare.config_flow.surepy.client.SureAPIClient.get_token",
|
|
||||||
return_value={"token": "token"},
|
|
||||||
):
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{"password": "test-password"},
|
{"password": "test-password2"},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result2["type"] is FlowResultType.ABORT
|
assert result2["type"] is FlowResultType.ABORT
|
||||||
assert result2["reason"] == "reauth_successful"
|
assert result2["reason"] == "reauth_successful"
|
||||||
|
|
||||||
|
assert old_entry.data == {
|
||||||
|
CONF_USERNAME: "test-username",
|
||||||
|
CONF_PASSWORD: "test-password2",
|
||||||
|
CONF_TOKEN: "token2",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_reauthentication_failure(hass: HomeAssistant) -> None:
|
async def test_reauthentication_failure(hass: HomeAssistant) -> None:
|
||||||
"""Test surepetcare reauthentication failure."""
|
"""Test surepetcare reauthentication failure."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user