Modernize Notion config flow (#32167)
* Modernize Notion config flow * Linting
This commit is contained in:
parent
0e6a48c688
commit
c97b1c60b0
6 changed files with 36 additions and 30 deletions
|
@ -480,6 +480,7 @@ omit =
|
||||||
homeassistant/components/nissan_leaf/*
|
homeassistant/components/nissan_leaf/*
|
||||||
homeassistant/components/nmap_tracker/device_tracker.py
|
homeassistant/components/nmap_tracker/device_tracker.py
|
||||||
homeassistant/components/nmbs/sensor.py
|
homeassistant/components/nmbs/sensor.py
|
||||||
|
homeassistant/components/notion/__init__.py
|
||||||
homeassistant/components/notion/binary_sensor.py
|
homeassistant/components/notion/binary_sensor.py
|
||||||
homeassistant/components/notion/sensor.py
|
homeassistant/components/notion/sensor.py
|
||||||
homeassistant/components/noaa_tides/sensor.py
|
homeassistant/components/noaa_tides/sensor.py
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
|
"abort": {
|
||||||
|
"already_configured": "This username is already in use."
|
||||||
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"identifier_exists": "Username already registered",
|
|
||||||
"invalid_credentials": "Invalid username or password",
|
"invalid_credentials": "Invalid username or password",
|
||||||
"no_devices": "No devices found in account"
|
"no_devices": "No devices found in account"
|
||||||
},
|
},
|
||||||
|
|
|
@ -22,7 +22,6 @@ from homeassistant.helpers.dispatcher import (
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.event import async_track_time_interval
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
|
|
||||||
from .config_flow import configured_instances
|
|
||||||
from .const import DATA_CLIENT, DEFAULT_SCAN_INTERVAL, DOMAIN, TOPIC_DATA_UPDATE
|
from .const import DATA_CLIENT, DEFAULT_SCAN_INTERVAL, DOMAIN, TOPIC_DATA_UPDATE
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -84,9 +83,6 @@ async def async_setup(hass, config):
|
||||||
|
|
||||||
conf = config[DOMAIN]
|
conf = config[DOMAIN]
|
||||||
|
|
||||||
if conf[CONF_USERNAME] in configured_instances(hass):
|
|
||||||
return True
|
|
||||||
|
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
hass.config_entries.flow.async_init(
|
hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
@ -103,6 +99,11 @@ async def async_setup(hass, config):
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry):
|
async def async_setup_entry(hass, config_entry):
|
||||||
"""Set up Notion as a config entry."""
|
"""Set up Notion as a config entry."""
|
||||||
|
if not config_entry.unique_id:
|
||||||
|
hass.config_entries.async_update_entry(
|
||||||
|
config_entry, unique_id=config_entry.data[CONF_USERNAME]
|
||||||
|
)
|
||||||
|
|
||||||
session = aiohttp_client.async_get_clientsession(hass)
|
session = aiohttp_client.async_get_clientsession(hass)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -5,35 +5,27 @@ import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.core import callback
|
|
||||||
from homeassistant.helpers import aiohttp_client
|
from homeassistant.helpers import aiohttp_client
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN # pylint: disable=unused-import
|
||||||
|
|
||||||
|
|
||||||
@callback
|
class NotionFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
def configured_instances(hass):
|
|
||||||
"""Return a set of configured Notion instances."""
|
|
||||||
return set(
|
|
||||||
entry.data[CONF_USERNAME] for entry in hass.config_entries.async_entries(DOMAIN)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@config_entries.HANDLERS.register(DOMAIN)
|
|
||||||
class NotionFlowHandler(config_entries.ConfigFlow):
|
|
||||||
"""Handle a Notion config flow."""
|
"""Handle a Notion config flow."""
|
||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
||||||
|
|
||||||
async def _show_form(self, errors=None):
|
def __init__(self):
|
||||||
"""Show the form to the user."""
|
"""Initialize the config flow."""
|
||||||
data_schema = vol.Schema(
|
self.data_schema = vol.Schema(
|
||||||
{vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
|
{vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def _show_form(self, errors=None):
|
||||||
|
"""Show the form to the user."""
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user", data_schema=data_schema, errors=errors or {}
|
step_id="user", data_schema=self.data_schema, errors=errors or {}
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_import(self, import_config):
|
async def async_step_import(self, import_config):
|
||||||
|
@ -42,12 +34,11 @@ class NotionFlowHandler(config_entries.ConfigFlow):
|
||||||
|
|
||||||
async def async_step_user(self, user_input=None):
|
async def async_step_user(self, user_input=None):
|
||||||
"""Handle the start of the config flow."""
|
"""Handle the start of the config flow."""
|
||||||
|
|
||||||
if not user_input:
|
if not user_input:
|
||||||
return await self._show_form()
|
return await self._show_form()
|
||||||
|
|
||||||
if user_input[CONF_USERNAME] in configured_instances(self.hass):
|
await self.async_set_unique_id(user_input[CONF_USERNAME])
|
||||||
return await self._show_form({CONF_USERNAME: "identifier_exists"})
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
session = aiohttp_client.async_get_clientsession(self.hass)
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,11 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"identifier_exists": "Username already registered",
|
|
||||||
"invalid_credentials": "Invalid username or password",
|
"invalid_credentials": "Invalid username or password",
|
||||||
"no_devices": "No devices found in account"
|
"no_devices": "No devices found in account"
|
||||||
|
},
|
||||||
|
"abort": {
|
||||||
|
"already_configured": "This username is already in use."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import pytest
|
||||||
|
|
||||||
from homeassistant import data_entry_flow
|
from homeassistant import data_entry_flow
|
||||||
from homeassistant.components.notion import DOMAIN, config_flow
|
from homeassistant.components.notion import DOMAIN, config_flow
|
||||||
|
from homeassistant.config_entries import SOURCE_USER
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, mock_coro
|
from tests.common import MockConfigEntry, mock_coro
|
||||||
|
@ -29,12 +30,16 @@ async def test_duplicate_error(hass):
|
||||||
"""Test that errors are shown when duplicates are added."""
|
"""Test that errors are shown when duplicates are added."""
|
||||||
conf = {CONF_USERNAME: "user@host.com", CONF_PASSWORD: "password123"}
|
conf = {CONF_USERNAME: "user@host.com", CONF_PASSWORD: "password123"}
|
||||||
|
|
||||||
MockConfigEntry(domain=DOMAIN, data=conf).add_to_hass(hass)
|
MockConfigEntry(domain=DOMAIN, unique_id="user@host.com", data=conf).add_to_hass(
|
||||||
flow = config_flow.NotionFlowHandler()
|
hass
|
||||||
flow.hass = hass
|
)
|
||||||
|
|
||||||
result = await flow.async_step_user(user_input=conf)
|
result = await hass.config_entries.flow.async_init(
|
||||||
assert result["errors"] == {CONF_USERNAME: "identifier_exists"}
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
@ -46,6 +51,7 @@ async def test_invalid_credentials(hass, mock_aionotion):
|
||||||
|
|
||||||
flow = config_flow.NotionFlowHandler()
|
flow = config_flow.NotionFlowHandler()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
|
flow.context = {"source": SOURCE_USER}
|
||||||
|
|
||||||
result = await flow.async_step_user(user_input=conf)
|
result = await flow.async_step_user(user_input=conf)
|
||||||
assert result["errors"] == {"base": "invalid_credentials"}
|
assert result["errors"] == {"base": "invalid_credentials"}
|
||||||
|
@ -55,6 +61,7 @@ async def test_show_form(hass):
|
||||||
"""Test that the form is served with no input."""
|
"""Test that the form is served with no input."""
|
||||||
flow = config_flow.NotionFlowHandler()
|
flow = config_flow.NotionFlowHandler()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
|
flow.context = {"source": SOURCE_USER}
|
||||||
|
|
||||||
result = await flow.async_step_user(user_input=None)
|
result = await flow.async_step_user(user_input=None)
|
||||||
|
|
||||||
|
@ -68,6 +75,7 @@ async def test_step_import(hass, mock_aionotion):
|
||||||
|
|
||||||
flow = config_flow.NotionFlowHandler()
|
flow = config_flow.NotionFlowHandler()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
|
flow.context = {"source": SOURCE_USER}
|
||||||
|
|
||||||
result = await flow.async_step_import(import_config=conf)
|
result = await flow.async_step_import(import_config=conf)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
|
@ -84,6 +92,7 @@ async def test_step_user(hass, mock_aionotion):
|
||||||
|
|
||||||
flow = config_flow.NotionFlowHandler()
|
flow = config_flow.NotionFlowHandler()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
|
flow.context = {"source": SOURCE_USER}
|
||||||
|
|
||||||
result = await flow.async_step_user(user_input=conf)
|
result = await flow.async_step_user(user_input=conf)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue