From 0643ff1cfe1057617cb6fda410084022f99d9252 Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Sun, 17 Mar 2024 13:44:32 +0100 Subject: [PATCH] Improve debug logging in Tankerkoenig (#113674) --- .../components/tankerkoenig/coordinator.py | 34 +++++++-- .../tankerkoenig/test_coordinator.py | 75 ++++++++++++++++++- 2 files changed, 100 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/tankerkoenig/coordinator.py b/homeassistant/components/tankerkoenig/coordinator.py index f6c021cb74e..447099d2dca 100644 --- a/homeassistant/components/tankerkoenig/coordinator.py +++ b/homeassistant/components/tankerkoenig/coordinator.py @@ -62,8 +62,18 @@ class TankerkoenigDataUpdateCoordinator(DataUpdateCoordinator): try: station = await self._tankerkoenig.station_details(station_id) except TankerkoenigInvalidKeyError as err: + _LOGGER.debug( + "invalid key error occur during setup of station %s %s", + station_id, + err, + ) raise ConfigEntryAuthFailed(err) from err except TankerkoenigConnectionError as err: + _LOGGER.debug( + "connection error occur during setup of station %s %s", + station_id, + err, + ) raise ConfigEntryNotReady(err) from err except TankerkoenigError as err: _LOGGER.error("Error when adding station %s %s", station_id, err) @@ -86,17 +96,27 @@ class TankerkoenigDataUpdateCoordinator(DataUpdateCoordinator): # The API seems to only return at most 10 results, so split the list in chunks of 10 # and merge it together. for index in range(ceil(len(station_ids) / 10)): + stations = station_ids[index * 10 : (index + 1) * 10] try: - data = await self._tankerkoenig.prices( - station_ids[index * 10 : (index + 1) * 10] - ) + data = await self._tankerkoenig.prices(stations) except TankerkoenigInvalidKeyError as err: + _LOGGER.debug( + "invalid key error occur during update of stations %s %s", + stations, + err, + ) raise ConfigEntryAuthFailed(err) from err + except TankerkoenigRateLimitError as err: + _LOGGER.warning( + "API rate limit reached, consider to increase polling interval" + ) + raise UpdateFailed(err) from err except (TankerkoenigError, TankerkoenigConnectionError) as err: - if isinstance(err, TankerkoenigRateLimitError): - _LOGGER.warning( - "API rate limit reached, consider to increase polling interval" - ) + _LOGGER.debug( + "error occur during update of stations %s %s", + stations, + err, + ) raise UpdateFailed(err) from err prices.update(data) diff --git a/tests/components/tankerkoenig/test_coordinator.py b/tests/components/tankerkoenig/test_coordinator.py index e1604c77819..5a33cb95dd9 100644 --- a/tests/components/tankerkoenig/test_coordinator.py +++ b/tests/components/tankerkoenig/test_coordinator.py @@ -5,13 +5,19 @@ from __future__ import annotations from datetime import timedelta from unittest.mock import AsyncMock -from aiotankerkoenig.exceptions import TankerkoenigRateLimitError +from aiotankerkoenig.exceptions import ( + TankerkoenigConnectionError, + TankerkoenigError, + TankerkoenigInvalidKeyError, + TankerkoenigRateLimitError, +) import pytest -from homeassistant.components.tankerkoenig.const import DEFAULT_SCAN_INTERVAL +from homeassistant.components.tankerkoenig.const import DEFAULT_SCAN_INTERVAL, DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.const import STATE_UNAVAILABLE from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util from tests.common import MockConfigEntry, async_fire_time_changed @@ -50,3 +56,68 @@ async def test_rate_limit( state = hass.states.get("binary_sensor.station_somewhere_street_1_status") assert state assert state.state == "on" + + +@pytest.mark.parametrize( + ("exception", "expected_log"), + [ + ( + TankerkoenigInvalidKeyError, + "invalid key error occur during update of stations", + ), + ( + TankerkoenigRateLimitError, + "API rate limit reached, consider to increase polling interval", + ), + (TankerkoenigConnectionError, "error occur during update of stations"), + (TankerkoenigError, "error occur during update of stations"), + ], +) +@pytest.mark.usefixtures("setup_integration") +async def test_update_exception_logging( + hass: HomeAssistant, + config_entry: MockConfigEntry, + tankerkoenig: AsyncMock, + caplog: pytest.LogCaptureFixture, + exception: None, + expected_log: str, +) -> None: + """Test log messages about exceptions during update.""" + tankerkoenig.prices.side_effect = exception + async_fire_time_changed( + hass, dt_util.utcnow() + timedelta(minutes=DEFAULT_SCAN_INTERVAL) + ) + await hass.async_block_till_done() + assert expected_log in caplog.text + state = hass.states.get("binary_sensor.station_somewhere_street_1_status") + assert state + assert state.state == STATE_UNAVAILABLE + + +@pytest.mark.parametrize( + ("exception", "expected_log"), + [ + ( + TankerkoenigInvalidKeyError, + "invalid key error occur during setup of station", + ), + (TankerkoenigConnectionError, "connection error occur during setup of station"), + (TankerkoenigError, "Error when adding station"), + ], +) +async def test_setup_exception_logging( + hass: HomeAssistant, + config_entry: MockConfigEntry, + tankerkoenig: AsyncMock, + caplog: pytest.LogCaptureFixture, + exception: None, + expected_log: str, +) -> None: + """Test log messages about exceptions during setup.""" + config_entry.add_to_hass(hass) + tankerkoenig.station_details.side_effect = exception + + assert await async_setup_component(hass, DOMAIN, {}) + await hass.async_block_till_done() + + assert expected_log in caplog.text