From 65e7818aeb53669af2855d61d84c94cad123b47f Mon Sep 17 00:00:00 2001 From: Sander Date: Sat, 30 Sep 2023 13:58:35 +0000 Subject: [PATCH 1/2] Improved log warning when a value is empty --- custom_components/knmi/coordinator.py | 4 ++++ tests/conftest.py | 17 +++++++++++++++++ tests/test_coordinator.py | 20 +++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/custom_components/knmi/coordinator.py b/custom_components/knmi/coordinator.py index 612df5d..42f8959 100644 --- a/custom_components/knmi/coordinator.py +++ b/custom_components/knmi/coordinator.py @@ -42,6 +42,10 @@ def get_value( ) -> float | int | str | None: """Get a value from the retrieved data and convert to given type""" if self.data and key in self.data: + if self.data.get(key, None) == "": + _LOGGER.warning("Value %s is empty in API response", key) + return "" # Leave empty, eg. warning attribute can be an empty string. + try: return convert_to(self.data.get(key, None)) except ValueError: diff --git a/tests/conftest.py b/tests/conftest.py index 9b6bc24..efa4f2c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -98,6 +98,23 @@ def mocked_data_empty_values_fixture(): yield +# This fixture, when used, will have the mocked values from response.json loaded in the integration. +# As an addition, d2tmin and d2tmax have a wrong type (string instead of a number). +@pytest.fixture(name="mocked_data_wrong_values_fixture") +def mocked_data_wrong_values_fixture(): + """Skip calls to get data from API.""" + data = json.loads(load_fixture(response_json)) + + data["d2tmin"] = "koud" + data["d2tmax"] = "warm" + + with patch( + async_get_data, + return_value=data, + ): + yield + + # In this fixture, we raise an KnmiApiClientCommunicationError in async_get_data. @pytest.fixture(name="error_on_get_data") def error_get_data_fixture(): diff --git a/tests/test_coordinator.py b/tests/test_coordinator.py index b6f7bf7..bf72897 100644 --- a/tests/test_coordinator.py +++ b/tests/test_coordinator.py @@ -44,7 +44,25 @@ async def test_get_value_empty(hass: HomeAssistant, mocked_data_empty_values, ca assert "Value plaats is missing in API response" in caplog.text coordinator.get_value("temp", int) - assert "Value temp can't be converted to " in caplog.text + assert "Value temp is empty in API response" in caplog.text + + assert await config_entry.async_unload(hass) + await hass.async_block_till_done() + + +async def test_get_value_wrong_type( + hass: HomeAssistant, mocked_data_wrong_values_fixture, caplog +): + """Test get_value function with empty values.""" + config_entry = await setup_component(hass) + + coordinator: KnmiDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] + + coordinator.get_value("d2tmin", int) + assert "Value d2tmin can't be converted to " in caplog.text + + coordinator.get_value("d2tmax", int) + assert "Value d2tmax can't be converted to " in caplog.text assert await config_entry.async_unload(hass) await hass.async_block_till_done() From a58c0a4d9424879ad8cbb9d3c0db26f0696b246c Mon Sep 17 00:00:00 2001 From: Sander Date: Sat, 30 Sep 2023 14:23:34 +0000 Subject: [PATCH 2/2] Don't log empty value for `alarmtxt` --- custom_components/knmi/coordinator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/knmi/coordinator.py b/custom_components/knmi/coordinator.py index 42f8959..ec06cd6 100644 --- a/custom_components/knmi/coordinator.py +++ b/custom_components/knmi/coordinator.py @@ -42,7 +42,7 @@ def get_value( ) -> float | int | str | None: """Get a value from the retrieved data and convert to given type""" if self.data and key in self.data: - if self.data.get(key, None) == "": + if self.data.get(key, None) == "" and key != "alarmtxt": _LOGGER.warning("Value %s is empty in API response", key) return "" # Leave empty, eg. warning attribute can be an empty string.