From 6fd39f57eed13e06ed2407363b3b80b1bce07dc6 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Mon, 3 Aug 2020 11:35:36 -0600 Subject: [PATCH] Remove YAML configuration support for IQVIA (#38141) --- homeassistant/components/iqvia/__init__.py | 45 +++----------- homeassistant/components/iqvia/config_flow.py | 44 ++++---------- homeassistant/components/iqvia/strings.json | 8 ++- .../components/iqvia/translations/en.json | 4 +- tests/components/iqvia/test_config_flow.py | 60 +++++++++---------- 5 files changed, 55 insertions(+), 106 deletions(-) diff --git a/homeassistant/components/iqvia/__init__.py b/homeassistant/components/iqvia/__init__.py index cd75e88bb44..3e67e2639e2 100644 --- a/homeassistant/components/iqvia/__init__.py +++ b/homeassistant/components/iqvia/__init__.py @@ -5,12 +5,10 @@ import logging from pyiqvia import Client from pyiqvia.errors import InvalidZipError, IQVIAError -import voluptuous as vol -from homeassistant.config_entries import SOURCE_IMPORT -from homeassistant.const import ATTR_ATTRIBUTION, CONF_MONITORED_CONDITIONS +from homeassistant.const import ATTR_ATTRIBUTION from homeassistant.core import callback -from homeassistant.helpers import aiohttp_client, config_validation as cv +from homeassistant.helpers import aiohttp_client from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, async_dispatcher_send, @@ -18,13 +16,11 @@ from homeassistant.helpers.dispatcher import ( from homeassistant.helpers.entity import Entity from homeassistant.helpers.event import async_track_time_interval -from .config_flow import configured_instances from .const import ( CONF_ZIP_CODE, DATA_CLIENT, DATA_LISTENER, DOMAIN, - SENSORS, TOPIC_DATA_UPDATE, TYPE_ALLERGY_FORECAST, TYPE_ALLERGY_INDEX, @@ -56,23 +52,6 @@ DATA_CONFIG = "config" DEFAULT_ATTRIBUTION = "Data provided by IQVIA™" DEFAULT_SCAN_INTERVAL = timedelta(minutes=30) -CONFIG_SCHEMA = vol.Schema( - { - DOMAIN: vol.All( - cv.deprecated(CONF_MONITORED_CONDITIONS, invalidation_version="0.114.0"), - vol.Schema( - { - vol.Required(CONF_ZIP_CODE): str, - vol.Optional( - CONF_MONITORED_CONDITIONS, default=list(SENSORS) - ): vol.All(cv.ensure_list, [vol.In(SENSORS)]), - } - ), - ) - }, - extra=vol.ALLOW_EXTRA, -) - @callback def async_get_api_category(sensor_type): @@ -86,20 +65,6 @@ async def async_setup(hass, config): hass.data[DOMAIN][DATA_CLIENT] = {} hass.data[DOMAIN][DATA_LISTENER] = {} - if DOMAIN not in config: - return True - - conf = config[DOMAIN] - - if conf[CONF_ZIP_CODE] in configured_instances(hass): - return True - - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_IMPORT}, data=conf - ) - ) - return True @@ -107,6 +72,12 @@ async def async_setup_entry(hass, config_entry): """Set up IQVIA as config entry.""" websession = aiohttp_client.async_get_clientsession(hass) + if not config_entry.unique_id: + # If the config entry doesn't already have a unique ID, set one: + hass.config_entries.async_update_entry( + config_entry, **{"unique_id": config_entry.data[CONF_ZIP_CODE]} + ) + iqvia = IQVIAData(hass, Client(config_entry.data[CONF_ZIP_CODE], websession)) try: diff --git a/homeassistant/components/iqvia/config_flow.py b/homeassistant/components/iqvia/config_flow.py index 6a57f0f24d4..e43c61985d6 100644 --- a/homeassistant/components/iqvia/config_flow.py +++ b/homeassistant/components/iqvia/config_flow.py @@ -1,28 +1,15 @@ """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 @@ -30,34 +17,25 @@ class IQVIAFlowHandler(config_entries.ConfigFlow): 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) diff --git a/homeassistant/components/iqvia/strings.json b/homeassistant/components/iqvia/strings.json index efc9582e20a..b0d82430ef7 100644 --- a/homeassistant/components/iqvia/strings.json +++ b/homeassistant/components/iqvia/strings.json @@ -4,12 +4,16 @@ "user": { "title": "IQVIA", "description": "Fill out your U.S. or Canadian ZIP code.", - "data": { "zip_code": "ZIP Code" } + "data": { + "zip_code": "ZIP Code" + } } }, "error": { - "identifier_exists": "ZIP code already registered", "invalid_zip_code": "ZIP code is invalid" + }, + "abort": { + "already_configured": "This ZIP code has already been configured." } } } diff --git a/homeassistant/components/iqvia/translations/en.json b/homeassistant/components/iqvia/translations/en.json index 63ffc145594..c9b3526102b 100644 --- a/homeassistant/components/iqvia/translations/en.json +++ b/homeassistant/components/iqvia/translations/en.json @@ -1,7 +1,9 @@ { "config": { + "abort": { + "already_configured": "This ZIP code has already been configured." + }, "error": { - "identifier_exists": "ZIP code already registered", "invalid_zip_code": "ZIP code is invalid" }, "step": { diff --git a/tests/components/iqvia/test_config_flow.py b/tests/components/iqvia/test_config_flow.py index 4cc30958b23..6b6872d5f67 100644 --- a/tests/components/iqvia/test_config_flow.py +++ b/tests/components/iqvia/test_config_flow.py @@ -1,7 +1,9 @@ """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 @@ -9,57 +11,49 @@ 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"}