Make sure Notion saves new refresh token upon startup (#112676)
* Make sure Notion saves new refresh token upon startup * Code review * Typing * Smoother syntax * Fix tests * Fix tests for real
This commit is contained in:
parent
5b2a24b1bb
commit
3405bda835
3 changed files with 20 additions and 7 deletions
|
@ -181,6 +181,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
# Create a callback to save the refresh token when it changes:
|
# Create a callback to save the refresh token when it changes:
|
||||||
entry.async_on_unload(client.add_refresh_token_callback(async_save_refresh_token))
|
entry.async_on_unload(client.add_refresh_token_callback(async_save_refresh_token))
|
||||||
|
|
||||||
|
# Save the client's refresh token if it's different than what we already have:
|
||||||
|
if (token := client.refresh_token) and token != entry.data[CONF_REFRESH_TOKEN]:
|
||||||
|
async_save_refresh_token(token)
|
||||||
|
|
||||||
hass.config_entries.async_update_entry(entry, **entry_updates)
|
hass.config_entries.async_update_entry(entry, **entry_updates)
|
||||||
|
|
||||||
async def async_update() -> NotionData:
|
async def async_update() -> NotionData:
|
||||||
|
|
|
@ -10,8 +10,8 @@ from aionotion.sensor.models import Sensor
|
||||||
from aionotion.user.models import UserPreferences
|
from aionotion.user.models import UserPreferences
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.notion import DOMAIN
|
from homeassistant.components.notion import CONF_REFRESH_TOKEN, CONF_USER_UUID, DOMAIN
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, load_fixture
|
from tests.common import MockConfigEntry, load_fixture
|
||||||
|
@ -82,7 +82,8 @@ def config_fixture():
|
||||||
"""Define a config entry data fixture."""
|
"""Define a config entry data fixture."""
|
||||||
return {
|
return {
|
||||||
CONF_USERNAME: TEST_USERNAME,
|
CONF_USERNAME: TEST_USERNAME,
|
||||||
CONF_PASSWORD: TEST_PASSWORD,
|
CONF_USER_UUID: TEST_USER_UUID,
|
||||||
|
CONF_REFRESH_TOKEN: TEST_REFRESH_TOKEN,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .conftest import TEST_REFRESH_TOKEN, TEST_USER_UUID, TEST_USERNAME
|
from .conftest import TEST_PASSWORD, TEST_REFRESH_TOKEN, TEST_USER_UUID, TEST_USERNAME
|
||||||
|
|
||||||
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
||||||
|
|
||||||
|
@ -27,7 +27,6 @@ pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
||||||
async def test_create_entry(
|
async def test_create_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
client,
|
client,
|
||||||
config,
|
|
||||||
errors,
|
errors,
|
||||||
get_client_with_exception,
|
get_client_with_exception,
|
||||||
mock_aionotion,
|
mock_aionotion,
|
||||||
|
@ -45,13 +44,22 @@ async def test_create_entry(
|
||||||
get_client_with_exception,
|
get_client_with_exception,
|
||||||
):
|
):
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
DOMAIN,
|
||||||
|
context={"source": SOURCE_USER},
|
||||||
|
data={
|
||||||
|
CONF_USERNAME: TEST_USERNAME,
|
||||||
|
CONF_PASSWORD: TEST_PASSWORD,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||||
assert result["errors"] == errors
|
assert result["errors"] == errors
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input=config
|
result["flow_id"],
|
||||||
|
user_input={
|
||||||
|
CONF_USERNAME: TEST_USERNAME,
|
||||||
|
CONF_PASSWORD: TEST_PASSWORD,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == TEST_USERNAME
|
assert result["title"] == TEST_USERNAME
|
||||||
|
|
Loading…
Add table
Reference in a new issue