Remove YAML configuration support for IQVIA (#38141)

This commit is contained in:
Aaron Bach 2020-08-03 11:35:36 -06:00 committed by GitHub
parent 53e162c922
commit 6fd39f57ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 106 deletions

View File

@ -5,12 +5,10 @@ import logging
from pyiqvia import Client from pyiqvia import Client
from pyiqvia.errors import InvalidZipError, IQVIAError from pyiqvia.errors import InvalidZipError, IQVIAError
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.const import ATTR_ATTRIBUTION, CONF_MONITORED_CONDITIONS
from homeassistant.core import callback 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 ( from homeassistant.helpers.dispatcher import (
async_dispatcher_connect, async_dispatcher_connect,
async_dispatcher_send, async_dispatcher_send,
@ -18,13 +16,11 @@ 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 ( from .const import (
CONF_ZIP_CODE, CONF_ZIP_CODE,
DATA_CLIENT, DATA_CLIENT,
DATA_LISTENER, DATA_LISTENER,
DOMAIN, DOMAIN,
SENSORS,
TOPIC_DATA_UPDATE, TOPIC_DATA_UPDATE,
TYPE_ALLERGY_FORECAST, TYPE_ALLERGY_FORECAST,
TYPE_ALLERGY_INDEX, TYPE_ALLERGY_INDEX,
@ -56,23 +52,6 @@ DATA_CONFIG = "config"
DEFAULT_ATTRIBUTION = "Data provided by IQVIA™" DEFAULT_ATTRIBUTION = "Data provided by IQVIA™"
DEFAULT_SCAN_INTERVAL = timedelta(minutes=30) 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 @callback
def async_get_api_category(sensor_type): 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_CLIENT] = {}
hass.data[DOMAIN][DATA_LISTENER] = {} 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 return True
@ -107,6 +72,12 @@ async def async_setup_entry(hass, config_entry):
"""Set up IQVIA as config entry.""" """Set up IQVIA as config entry."""
websession = aiohttp_client.async_get_clientsession(hass) 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)) iqvia = IQVIAData(hass, Client(config_entry.data[CONF_ZIP_CODE], websession))
try: try:

View File

@ -1,28 +1,15 @@
"""Config flow to configure the IQVIA component.""" """Config flow to configure the IQVIA component."""
from collections import OrderedDict
from pyiqvia import Client from pyiqvia import Client
from pyiqvia.errors import InvalidZipError from pyiqvia.errors import InvalidZipError
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.helpers import aiohttp_client 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 class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
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):
"""Handle an IQVIA config flow.""" """Handle an IQVIA config flow."""
VERSION = 1 VERSION = 1
@ -30,34 +17,25 @@ class IQVIAFlowHandler(config_entries.ConfigFlow):
def __init__(self): def __init__(self):
"""Initialize the config flow.""" """Initialize the config flow."""
self.data_schema = OrderedDict() self.data_schema = vol.Schema({vol.Required(CONF_ZIP_CODE): str})
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)
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 self.async_show_form(step_id="user", data_schema=self.data_schema)
if user_input[CONF_ZIP_CODE] in configured_instances(self.hass): await self.async_set_unique_id(user_input[CONF_ZIP_CODE])
return await self._show_form({CONF_ZIP_CODE: "identifier_exists"}) self._abort_if_unique_id_configured()
websession = aiohttp_client.async_get_clientsession(self.hass) websession = aiohttp_client.async_get_clientsession(self.hass)
try: try:
Client(user_input[CONF_ZIP_CODE], websession) Client(user_input[CONF_ZIP_CODE], websession)
except InvalidZipError: 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) return self.async_create_entry(title=user_input[CONF_ZIP_CODE], data=user_input)

View File

@ -4,12 +4,16 @@
"user": { "user": {
"title": "IQVIA", "title": "IQVIA",
"description": "Fill out your U.S. or Canadian ZIP code.", "description": "Fill out your U.S. or Canadian ZIP code.",
"data": { "zip_code": "ZIP Code" } "data": {
"zip_code": "ZIP Code"
}
} }
}, },
"error": { "error": {
"identifier_exists": "ZIP code already registered",
"invalid_zip_code": "ZIP code is invalid" "invalid_zip_code": "ZIP code is invalid"
},
"abort": {
"already_configured": "This ZIP code has already been configured."
} }
} }
} }

View File

@ -1,7 +1,9 @@
{ {
"config": { "config": {
"abort": {
"already_configured": "This ZIP code has already been configured."
},
"error": { "error": {
"identifier_exists": "ZIP code already registered",
"invalid_zip_code": "ZIP code is invalid" "invalid_zip_code": "ZIP code is invalid"
}, },
"step": { "step": {

View File

@ -1,7 +1,9 @@
"""Define tests for the IQVIA config flow.""" """Define tests for the IQVIA config flow."""
from homeassistant import data_entry_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 from tests.common import MockConfigEntry
@ -9,57 +11,49 @@ 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_ZIP_CODE: "12345"} conf = {CONF_ZIP_CODE: "12345"}
MockConfigEntry(domain=DOMAIN, data=conf).add_to_hass(hass) MockConfigEntry(domain=DOMAIN, unique_id="12345", data=conf).add_to_hass(hass)
flow = config_flow.IQVIAFlowHandler()
flow.hass = hass
result = await flow.async_step_user(user_input=conf) result = await hass.config_entries.flow.async_init(
assert result["errors"] == {CONF_ZIP_CODE: "identifier_exists"} 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): async def test_invalid_zip_code(hass):
"""Test that an invalid ZIP code key throws an error.""" """Test that an invalid ZIP code key throws an error."""
conf = {CONF_ZIP_CODE: "abcde"} conf = {CONF_ZIP_CODE: "abcde"}
flow = config_flow.IQVIAFlowHandler() result = await hass.config_entries.flow.async_init(
flow.hass = hass 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"} assert result["errors"] == {CONF_ZIP_CODE: "invalid_zip_code"}
async def test_show_form(hass): 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.IQVIAFlowHandler() result = await hass.config_entries.flow.async_init(
flow.hass = hass DOMAIN, context={"source": SOURCE_USER}
)
result = await flow.async_step_user(user_input=None)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user" 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): 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"} conf = {CONF_ZIP_CODE: "12345"}
flow = config_flow.IQVIAFlowHandler() with patch(
flow.hass = hass "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["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "12345" assert result["title"] == "12345"
assert result["data"] == {CONF_ZIP_CODE: "12345"} assert result["data"] == {CONF_ZIP_CODE: "12345"}