|
| 1 | +"""Tests for the Tankerkoening integration.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from datetime import timedelta |
| 5 | +from unittest.mock import AsyncMock |
| 6 | + |
| 7 | +from aiotankerkoenig.exceptions import TankerkoenigRateLimitError |
| 8 | +import pytest |
| 9 | + |
| 10 | +from homeassistant.components.tankerkoenig.const import DEFAULT_SCAN_INTERVAL |
| 11 | +from homeassistant.config_entries import ConfigEntryState |
| 12 | +from homeassistant.const import STATE_UNAVAILABLE |
| 13 | +from homeassistant.core import HomeAssistant |
| 14 | +import homeassistant.util.dt as dt_util |
| 15 | + |
| 16 | +from tests.common import MockConfigEntry, async_fire_time_changed |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.usefixtures("setup_integration") |
| 20 | +async def test_rate_limit( |
| 21 | + hass: HomeAssistant, |
| 22 | + config_entry: MockConfigEntry, |
| 23 | + tankerkoenig: AsyncMock, |
| 24 | + caplog: pytest.LogCaptureFixture, |
| 25 | +) -> None: |
| 26 | + """Test detection of API rate limit.""" |
| 27 | + assert config_entry.state == ConfigEntryState.LOADED |
| 28 | + state = hass.states.get("binary_sensor.station_somewhere_street_1_status") |
| 29 | + assert state |
| 30 | + assert state.state == "on" |
| 31 | + |
| 32 | + tankerkoenig.prices.side_effect = TankerkoenigRateLimitError |
| 33 | + async_fire_time_changed( |
| 34 | + hass, dt_util.utcnow() + timedelta(minutes=DEFAULT_SCAN_INTERVAL) |
| 35 | + ) |
| 36 | + await hass.async_block_till_done() |
| 37 | + assert ( |
| 38 | + "API rate limit reached, consider to increase polling interval" in caplog.text |
| 39 | + ) |
| 40 | + state = hass.states.get("binary_sensor.station_somewhere_street_1_status") |
| 41 | + assert state |
| 42 | + assert state.state == STATE_UNAVAILABLE |
| 43 | + |
| 44 | + tankerkoenig.prices.side_effect = None |
| 45 | + async_fire_time_changed( |
| 46 | + hass, dt_util.utcnow() + timedelta(minutes=DEFAULT_SCAN_INTERVAL * 2) |
| 47 | + ) |
| 48 | + await hass.async_block_till_done() |
| 49 | + state = hass.states.get("binary_sensor.station_somewhere_street_1_status") |
| 50 | + assert state |
| 51 | + assert state.state == "on" |
0 commit comments