Skip to content

Commit

Permalink
Remove YAML configuration support for IQVIA
Browse files Browse the repository at this point in the history
  • Loading branch information
bachya committed Jul 24, 2020
1 parent a5b7a2c commit f1b49d0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 106 deletions.
39 changes: 2 additions & 37 deletions homeassistant/components/iqvia/__init__.py
Expand Up @@ -5,26 +5,22 @@

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,
)
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,
Expand Down Expand Up @@ -56,23 +52,6 @@
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):
Expand All @@ -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


Expand Down
44 changes: 11 additions & 33 deletions homeassistant/components/iqvia/config_flow.py
@@ -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)
8 changes: 6 additions & 2 deletions homeassistant/components/iqvia/strings.json
Expand Up @@ -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."
}
}
}
4 changes: 3 additions & 1 deletion 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": {
Expand Down
60 changes: 27 additions & 33 deletions tests/components/iqvia/test_config_flow.py
@@ -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"}

0 comments on commit f1b49d0

Please sign in to comment.