From 297d24c5b04a60abceacf0c6f16039f696cbbd65 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Thu, 16 May 2019 15:19:53 -0600 Subject: [PATCH] Fix bug when IQVIA API fails to return data (#23916) * Fix bug when IQVIA API fails to return data * Updated requirements * Fixed tests * Linting * Removed impossible case * Removed extraneous comment --- homeassistant/components/iqvia/__init__.py | 14 +++----------- homeassistant/components/iqvia/config_flow.py | 7 +++---- homeassistant/components/iqvia/manifest.json | 2 +- homeassistant/components/iqvia/sensor.py | 4 ++-- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/iqvia/test_config_flow.py | 15 ++------------- 7 files changed, 13 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/iqvia/__init__.py b/homeassistant/components/iqvia/__init__.py index cf8f92c1bd2..c58a7508e81 100644 --- a/homeassistant/components/iqvia/__init__.py +++ b/homeassistant/components/iqvia/__init__.py @@ -82,8 +82,9 @@ async def async_setup_entry(hass, config_entry): Client(config_entry.data[CONF_ZIP_CODE], websession), config_entry.data.get(CONF_MONITORED_CONDITIONS, list(SENSORS))) await iqvia.async_update() - except IQVIAError as err: - _LOGGER.error('Unable to set up IQVIA: %s', err) + except InvalidZipError: + _LOGGER.error( + 'Invalid ZIP code provided: %s', config_entry.data[CONF_ZIP_CODE]) return False hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = iqvia @@ -157,16 +158,7 @@ class IQVIAData: results = await asyncio.gather(*tasks.values(), return_exceptions=True) - # IQVIA sites require a bit more complicated error handling, given that - # they sometimes have parts (but not the whole thing) go down: - # 1. If `InvalidZipError` is thrown, quit everything immediately. - # 2. If a single request throws any other error, try the others. for key, result in zip(tasks, results): - if isinstance(result, InvalidZipError): - _LOGGER.error("No data for ZIP: %s", self._client.zip_code) - self.data = {} - return - if isinstance(result, IQVIAError): _LOGGER.error('Unable to get %s data: %s', key, result) self.data[key] = {} diff --git a/homeassistant/components/iqvia/config_flow.py b/homeassistant/components/iqvia/config_flow.py index d9a8c693670..fadecc8f3a7 100644 --- a/homeassistant/components/iqvia/config_flow.py +++ b/homeassistant/components/iqvia/config_flow.py @@ -4,7 +4,7 @@ from collections import OrderedDict import voluptuous as vol from pyiqvia import Client -from pyiqvia.errors import IQVIAError +from pyiqvia.errors import InvalidZipError from homeassistant import config_entries from homeassistant.core import callback @@ -54,11 +54,10 @@ class IQVIAFlowHandler(config_entries.ConfigFlow): return await self._show_form({CONF_ZIP_CODE: 'identifier_exists'}) websession = aiohttp_client.async_get_clientsession(self.hass) - client = Client(user_input[CONF_ZIP_CODE], websession) try: - await client.allergens.current() - except IQVIAError: + Client(user_input[CONF_ZIP_CODE], websession) + except InvalidZipError: return await self._show_form({CONF_ZIP_CODE: 'invalid_zip_code'}) return self.async_create_entry( diff --git a/homeassistant/components/iqvia/manifest.json b/homeassistant/components/iqvia/manifest.json index a59caa1654c..381165847ef 100644 --- a/homeassistant/components/iqvia/manifest.json +++ b/homeassistant/components/iqvia/manifest.json @@ -5,7 +5,7 @@ "documentation": "https://www.home-assistant.io/components/iqvia", "requirements": [ "numpy==1.16.3", - "pyiqvia==0.2.0" + "pyiqvia==0.2.1" ], "dependencies": [], "codeowners": [ diff --git a/homeassistant/components/iqvia/sensor.py b/homeassistant/components/iqvia/sensor.py index 5128b997b35..acd612d658b 100644 --- a/homeassistant/components/iqvia/sensor.py +++ b/homeassistant/components/iqvia/sensor.py @@ -106,8 +106,8 @@ class ForecastSensor(IQVIAEntity): if not self._iqvia.data: return - data = self._iqvia.data[self._type].get('Location') - if not data: + data = self._iqvia.data[self._type]['Location'] + if not data.get('periods'): return indices = [p['Index'] for p in data['periods']] diff --git a/requirements_all.txt b/requirements_all.txt index 90c66d4b7dc..9dc37a821cc 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1130,7 +1130,7 @@ pyicloud==0.9.1 pyipma==1.2.1 # homeassistant.components.iqvia -pyiqvia==0.2.0 +pyiqvia==0.2.1 # homeassistant.components.irish_rail_transport pyirishrail==0.0.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 99c2e9e09dc..c932096825b 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -235,7 +235,7 @@ pyheos==0.5.2 pyhomematic==0.1.58 # homeassistant.components.iqvia -pyiqvia==0.2.0 +pyiqvia==0.2.1 # homeassistant.components.litejet pylitejet==0.1 diff --git a/tests/components/iqvia/test_config_flow.py b/tests/components/iqvia/test_config_flow.py index 97ab4014291..48edc36629e 100644 --- a/tests/components/iqvia/test_config_flow.py +++ b/tests/components/iqvia/test_config_flow.py @@ -1,25 +1,16 @@ """Define tests for the IQVIA config flow.""" -from pyiqvia.errors import IQVIAError import pytest from homeassistant import data_entry_flow from homeassistant.components.iqvia import CONF_ZIP_CODE, DOMAIN, config_flow -from tests.common import MockConfigEntry, MockDependency, mock_coro +from tests.common import MockConfigEntry, MockDependency @pytest.fixture -def allergens_current_response(): - """Define a fixture for a successful allergens.current response.""" - return mock_coro() - - -@pytest.fixture -def mock_pyiqvia(allergens_current_response): +def mock_pyiqvia(): """Mock the pyiqvia library.""" with MockDependency('pyiqvia') as mock_pyiqvia_: - mock_pyiqvia_.Client().allergens.current.return_value = ( - allergens_current_response) yield mock_pyiqvia_ @@ -37,8 +28,6 @@ async def test_duplicate_error(hass): assert result['errors'] == {CONF_ZIP_CODE: 'identifier_exists'} -@pytest.mark.parametrize( - 'allergens_current_response', [mock_coro(exception=IQVIAError)]) async def test_invalid_zip_code(hass, mock_pyiqvia): """Test that an invalid ZIP code key throws an error.""" conf = {