Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions homeassistant/components/notion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def async_save_refresh_token(refresh_token: str) -> None:
# Create a callback to save the refresh token when it changes:
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)

async def async_update() -> NotionData:
Expand Down
7 changes: 4 additions & 3 deletions tests/components/notion/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from aionotion.user.models import UserPreferences
import pytest

from homeassistant.components.notion import DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.components.notion import CONF_REFRESH_TOKEN, CONF_USER_UUID, DOMAIN
from homeassistant.const import CONF_USERNAME
from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry, load_fixture
Expand Down Expand Up @@ -82,7 +82,8 @@ def config_fixture():
"""Define a config entry data fixture."""
return {
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
CONF_USER_UUID: TEST_USER_UUID,
CONF_REFRESH_TOKEN: TEST_REFRESH_TOKEN,
}


Expand Down
16 changes: 12 additions & 4 deletions tests/components/notion/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
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")

Expand All @@ -27,7 +27,6 @@
async def test_create_entry(
hass: HomeAssistant,
client,
config,
errors,
get_client_with_exception,
mock_aionotion,
Expand All @@ -45,13 +44,22 @@ async def test_create_entry(
get_client_with_exception,
):
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["errors"] == errors

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["title"] == TEST_USERNAME
Expand Down