Improve debug logging in Tankerkoenig (#113674)

pull/113677/head^2
Michael 2024-03-17 13:44:32 +01:00 committed by GitHub
parent 6113b99ddd
commit 0643ff1cfe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 100 additions and 9 deletions

View File

@ -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)

View File

@ -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