-
-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove YAML configuration support for IQVIA
- Loading branch information
Showing
5 changed files
with
49 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,41 @@ | ||
"""Config flow to configure the IQVIA component.""" | ||
|
||
from collections import OrderedDict | ||
|
||
from pyiqvia import Client | ||
from pyiqvia.errors import InvalidZipError | ||
import voluptuous as vol | ||
|
||
from homeassistant import config_entries | ||
from homeassistant.core import callback | ||
from homeassistant.helpers import aiohttp_client | ||
|
||
from .const import CONF_ZIP_CODE, DOMAIN | ||
|
||
from .const import CONF_ZIP_CODE, DOMAIN # pylint:disable=unused-import | ||
|
||
@callback | ||
def configured_instances(hass): | ||
"""Return a set of configured IQVIA instances.""" | ||
return { | ||
entry.data[CONF_ZIP_CODE] for entry in hass.config_entries.async_entries(DOMAIN) | ||
} | ||
|
||
|
||
@config_entries.HANDLERS.register(DOMAIN) | ||
class IQVIAFlowHandler(config_entries.ConfigFlow): | ||
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): | ||
"""Handle an IQVIA config flow.""" | ||
|
||
VERSION = 1 | ||
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL | ||
|
||
def __init__(self): | ||
"""Initialize the config flow.""" | ||
self.data_schema = OrderedDict() | ||
self.data_schema[vol.Required(CONF_ZIP_CODE)] = str | ||
|
||
async def _show_form(self, errors=None): | ||
"""Show the form to the user.""" | ||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=vol.Schema(self.data_schema), | ||
errors=errors if errors else {}, | ||
) | ||
|
||
async def async_step_import(self, import_config): | ||
"""Import a config entry from configuration.yaml.""" | ||
return await self.async_step_user(import_config) | ||
self.data_schema = vol.Schema({vol.Required(CONF_ZIP_CODE): str}) | ||
|
||
async def async_step_user(self, user_input=None): | ||
"""Handle the start of the config flow.""" | ||
if not user_input: | ||
return await self._show_form() | ||
return self.async_show_form(step_id="user", data_schema=self.data_schema) | ||
|
||
if user_input[CONF_ZIP_CODE] in configured_instances(self.hass): | ||
return await self._show_form({CONF_ZIP_CODE: "identifier_exists"}) | ||
await self.async_set_unique_id(user_input[CONF_ZIP_CODE]) | ||
self._abort_if_unique_id_configured() | ||
|
||
websession = aiohttp_client.async_get_clientsession(self.hass) | ||
|
||
try: | ||
Client(user_input[CONF_ZIP_CODE], websession) | ||
except InvalidZipError: | ||
return await self._show_form({CONF_ZIP_CODE: "invalid_zip_code"}) | ||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=self.data_schema, | ||
errors={CONF_ZIP_CODE: "invalid_zip_code"}, | ||
) | ||
|
||
return self.async_create_entry(title=user_input[CONF_ZIP_CODE], data=user_input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,59 @@ | ||
"""Define tests for the IQVIA config flow.""" | ||
from homeassistant import data_entry_flow | ||
from homeassistant.components.iqvia import CONF_ZIP_CODE, DOMAIN, config_flow | ||
from homeassistant.components.iqvia import CONF_ZIP_CODE, DOMAIN | ||
from homeassistant.config_entries import SOURCE_USER | ||
|
||
from tests.async_mock import patch | ||
from tests.common import MockConfigEntry | ||
|
||
|
||
async def test_duplicate_error(hass): | ||
"""Test that errors are shown when duplicates are added.""" | ||
conf = {CONF_ZIP_CODE: "12345"} | ||
|
||
MockConfigEntry(domain=DOMAIN, data=conf).add_to_hass(hass) | ||
flow = config_flow.IQVIAFlowHandler() | ||
flow.hass = hass | ||
MockConfigEntry(domain=DOMAIN, unique_id="12345", data=conf).add_to_hass(hass) | ||
|
||
result = await flow.async_step_user(user_input=conf) | ||
assert result["errors"] == {CONF_ZIP_CODE: "identifier_exists"} | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": SOURCE_USER}, data=conf | ||
) | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT | ||
assert result["reason"] == "already_configured" | ||
|
||
|
||
async def test_invalid_zip_code(hass): | ||
"""Test that an invalid ZIP code key throws an error.""" | ||
conf = {CONF_ZIP_CODE: "abcde"} | ||
|
||
flow = config_flow.IQVIAFlowHandler() | ||
flow.hass = hass | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": SOURCE_USER}, data=conf | ||
) | ||
|
||
result = await flow.async_step_user(user_input=conf) | ||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["errors"] == {CONF_ZIP_CODE: "invalid_zip_code"} | ||
|
||
|
||
async def test_show_form(hass): | ||
"""Test that the form is served with no input.""" | ||
flow = config_flow.IQVIAFlowHandler() | ||
flow.hass = hass | ||
|
||
result = await flow.async_step_user(user_input=None) | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": SOURCE_USER} | ||
) | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["step_id"] == "user" | ||
|
||
|
||
async def test_step_import(hass): | ||
"""Test that the import step works.""" | ||
conf = {CONF_ZIP_CODE: "12345"} | ||
|
||
flow = config_flow.IQVIAFlowHandler() | ||
flow.hass = hass | ||
|
||
result = await flow.async_step_import(import_config=conf) | ||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY | ||
assert result["title"] == "12345" | ||
assert result["data"] == {CONF_ZIP_CODE: "12345"} | ||
|
||
|
||
async def test_step_user(hass): | ||
"""Test that the user step works.""" | ||
"""Test that the user step works (without MFA).""" | ||
conf = {CONF_ZIP_CODE: "12345"} | ||
|
||
flow = config_flow.IQVIAFlowHandler() | ||
flow.hass = hass | ||
with patch( | ||
"homeassistant.components.simplisafe.async_setup_entry", return_value=True | ||
): | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": SOURCE_USER}, data=conf | ||
) | ||
|
||
result = await flow.async_step_user(user_input=conf) | ||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY | ||
assert result["title"] == "12345" | ||
assert result["data"] == {CONF_ZIP_CODE: "12345"} | ||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY | ||
assert result["title"] == "12345" | ||
assert result["data"] == {CONF_ZIP_CODE: "12345"} |