forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect reached API rate limit in Tankerkoenig (home-assistant#110432)
- Loading branch information
Showing
5 changed files
with
69 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""Tests for the Tankerkoening integration.""" | ||
from __future__ import annotations | ||
|
||
from datetime import timedelta | ||
from unittest.mock import AsyncMock | ||
|
||
from aiotankerkoenig.exceptions import TankerkoenigRateLimitError | ||
import pytest | ||
|
||
from homeassistant.components.tankerkoenig.const import DEFAULT_SCAN_INTERVAL | ||
from homeassistant.config_entries import ConfigEntryState | ||
from homeassistant.const import STATE_UNAVAILABLE | ||
from homeassistant.core import HomeAssistant | ||
import homeassistant.util.dt as dt_util | ||
|
||
from tests.common import MockConfigEntry, async_fire_time_changed | ||
|
||
|
||
@pytest.mark.usefixtures("setup_integration") | ||
async def test_rate_limit( | ||
hass: HomeAssistant, | ||
config_entry: MockConfigEntry, | ||
tankerkoenig: AsyncMock, | ||
caplog: pytest.LogCaptureFixture, | ||
) -> None: | ||
"""Test detection of API rate limit.""" | ||
assert config_entry.state == ConfigEntryState.LOADED | ||
state = hass.states.get("binary_sensor.station_somewhere_street_1_status") | ||
assert state | ||
assert state.state == "on" | ||
|
||
tankerkoenig.prices.side_effect = TankerkoenigRateLimitError | ||
async_fire_time_changed( | ||
hass, dt_util.utcnow() + timedelta(minutes=DEFAULT_SCAN_INTERVAL) | ||
) | ||
await hass.async_block_till_done() | ||
assert ( | ||
"API rate limit reached, consider to increase polling interval" in caplog.text | ||
) | ||
state = hass.states.get("binary_sensor.station_somewhere_street_1_status") | ||
assert state | ||
assert state.state == STATE_UNAVAILABLE | ||
|
||
tankerkoenig.prices.side_effect = None | ||
async_fire_time_changed( | ||
hass, dt_util.utcnow() + timedelta(minutes=DEFAULT_SCAN_INTERVAL * 2) | ||
) | ||
await hass.async_block_till_done() | ||
state = hass.states.get("binary_sensor.station_somewhere_street_1_status") | ||
assert state | ||
assert state.state == "on" |