Skip to content

Commit

Permalink
Merge pull request #97 from golles/96-value-d2tmin-cant-be-converted-…
Browse files Browse the repository at this point in the history
…to-class-float

Improved log warning when a value is empty
  • Loading branch information
golles authored Oct 13, 2023
2 parents effcaba + a58c0a4 commit c56020d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions custom_components/knmi/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) == "" and key != "alarmtxt":
_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:
Expand Down
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
20 changes: 19 additions & 1 deletion tests/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class 'int'>" 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 <class 'int'>" in caplog.text

coordinator.get_value("d2tmax", int)
assert "Value d2tmax can't be converted to <class 'int'>" in caplog.text

assert await config_entry.async_unload(hass)
await hass.async_block_till_done()

0 comments on commit c56020d

Please sign in to comment.