mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Improve debug logging in Tankerkoenig (#113674)
This commit is contained in:
parent
6113b99ddd
commit
0643ff1cfe
@ -62,8 +62,18 @@ class TankerkoenigDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
try:
|
try:
|
||||||
station = await self._tankerkoenig.station_details(station_id)
|
station = await self._tankerkoenig.station_details(station_id)
|
||||||
except TankerkoenigInvalidKeyError as err:
|
except TankerkoenigInvalidKeyError as err:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"invalid key error occur during setup of station %s %s",
|
||||||
|
station_id,
|
||||||
|
err,
|
||||||
|
)
|
||||||
raise ConfigEntryAuthFailed(err) from err
|
raise ConfigEntryAuthFailed(err) from err
|
||||||
except TankerkoenigConnectionError as err:
|
except TankerkoenigConnectionError as err:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"connection error occur during setup of station %s %s",
|
||||||
|
station_id,
|
||||||
|
err,
|
||||||
|
)
|
||||||
raise ConfigEntryNotReady(err) from err
|
raise ConfigEntryNotReady(err) from err
|
||||||
except TankerkoenigError as err:
|
except TankerkoenigError as err:
|
||||||
_LOGGER.error("Error when adding station %s %s", station_id, 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
|
# The API seems to only return at most 10 results, so split the list in chunks of 10
|
||||||
# and merge it together.
|
# and merge it together.
|
||||||
for index in range(ceil(len(station_ids) / 10)):
|
for index in range(ceil(len(station_ids) / 10)):
|
||||||
|
stations = station_ids[index * 10 : (index + 1) * 10]
|
||||||
try:
|
try:
|
||||||
data = await self._tankerkoenig.prices(
|
data = await self._tankerkoenig.prices(stations)
|
||||||
station_ids[index * 10 : (index + 1) * 10]
|
|
||||||
)
|
|
||||||
except TankerkoenigInvalidKeyError as err:
|
except TankerkoenigInvalidKeyError as err:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"invalid key error occur during update of stations %s %s",
|
||||||
|
stations,
|
||||||
|
err,
|
||||||
|
)
|
||||||
raise ConfigEntryAuthFailed(err) from 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:
|
except (TankerkoenigError, TankerkoenigConnectionError) as err:
|
||||||
if isinstance(err, TankerkoenigRateLimitError):
|
_LOGGER.debug(
|
||||||
_LOGGER.warning(
|
"error occur during update of stations %s %s",
|
||||||
"API rate limit reached, consider to increase polling interval"
|
stations,
|
||||||
)
|
err,
|
||||||
|
)
|
||||||
raise UpdateFailed(err) from err
|
raise UpdateFailed(err) from err
|
||||||
|
|
||||||
prices.update(data)
|
prices.update(data)
|
||||||
|
@ -5,13 +5,19 @@ from __future__ import annotations
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
from aiotankerkoenig.exceptions import TankerkoenigRateLimitError
|
from aiotankerkoenig.exceptions import (
|
||||||
|
TankerkoenigConnectionError,
|
||||||
|
TankerkoenigError,
|
||||||
|
TankerkoenigInvalidKeyError,
|
||||||
|
TankerkoenigRateLimitError,
|
||||||
|
)
|
||||||
import pytest
|
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.config_entries import ConfigEntryState
|
||||||
from homeassistant.const import STATE_UNAVAILABLE
|
from homeassistant.const import STATE_UNAVAILABLE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
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")
|
state = hass.states.get("binary_sensor.station_somewhere_street_1_status")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == "on"
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user