mirror of
https://github.com/home-assistant/core
synced 2024-09-06 10:29:55 +02:00
Fix kraken I/O and sleep in tests (#51599)
This commit is contained in:
parent
330f713e43
commit
6c2e452e3d
@ -25,6 +25,8 @@ from .const import (
|
||||
)
|
||||
from .utils import get_tradable_asset_pairs
|
||||
|
||||
CALL_RATE_LIMIT_SLEEP = 1
|
||||
|
||||
PLATFORMS = ["sensor"]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -125,7 +127,8 @@ class KrakenData:
|
||||
self._config_entry, options=options
|
||||
)
|
||||
await self._async_refresh_tradable_asset_pairs()
|
||||
await asyncio.sleep(1) # Wait 1 second to avoid triggering the CallRateLimiter
|
||||
# Wait 1 second to avoid triggering the CallRateLimiter
|
||||
await asyncio.sleep(CALL_RATE_LIMIT_SLEEP)
|
||||
self.coordinator = DataUpdateCoordinator(
|
||||
self._hass,
|
||||
_LOGGER,
|
||||
|
@ -38,7 +38,7 @@ class KrakenConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Handle the initial step."""
|
||||
if DOMAIN in self.hass.data:
|
||||
if self._async_current_entries():
|
||||
return self.async_abort(reason="already_configured")
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(title=DOMAIN, data=user_input)
|
||||
|
11
tests/components/kraken/conftest.py
Normal file
11
tests/components/kraken/conftest.py
Normal file
@ -0,0 +1,11 @@
|
||||
"""Provide common pytest fixtures for kraken tests."""
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_call_rate_limit_sleep():
|
||||
"""Patch the call rate limit sleep time."""
|
||||
with patch("homeassistant.components.kraken.CALL_RATE_LIMIT_SLEEP", new=0):
|
||||
yield
|
@ -12,74 +12,61 @@ from tests.common import MockConfigEntry
|
||||
async def test_config_flow(hass):
|
||||
"""Test we can finish a config flow."""
|
||||
with patch(
|
||||
"pykrakenapi.KrakenAPI.get_tradable_asset_pairs",
|
||||
return_value=TRADEABLE_ASSET_PAIR_RESPONSE,
|
||||
), patch(
|
||||
"pykrakenapi.KrakenAPI.get_ticker_information",
|
||||
return_value=TICKER_INFORMATION_RESPONSE,
|
||||
):
|
||||
"homeassistant.components.kraken.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
assert result["type"] == "create_entry"
|
||||
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("sensor.xbt_usd_ask")
|
||||
assert state
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_already_configured(hass):
|
||||
"""Test we can not add a second config flow."""
|
||||
with patch(
|
||||
"pykrakenapi.KrakenAPI.get_tradable_asset_pairs",
|
||||
return_value=TRADEABLE_ASSET_PAIR_RESPONSE,
|
||||
), patch(
|
||||
"pykrakenapi.KrakenAPI.get_ticker_information",
|
||||
return_value=TICKER_INFORMATION_RESPONSE,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
MockConfigEntry(domain=DOMAIN).add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
assert result["type"] == "create_entry"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_configured"
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_options(hass):
|
||||
"""Test options for Kraken."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
options={
|
||||
CONF_SCAN_INTERVAL: 60,
|
||||
CONF_TRACKED_ASSET_PAIRS: [
|
||||
"ADA/XBT",
|
||||
"ADA/ETH",
|
||||
"XBT/EUR",
|
||||
"XBT/GBP",
|
||||
"XBT/USD",
|
||||
"XBT/JPY",
|
||||
],
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.kraken.config_flow.KrakenAPI.get_tradable_asset_pairs",
|
||||
return_value=TRADEABLE_ASSET_PAIR_RESPONSE,
|
||||
), patch(
|
||||
"pykrakenapi.KrakenAPI.get_tradable_asset_pairs",
|
||||
return_value=TRADEABLE_ASSET_PAIR_RESPONSE,
|
||||
), patch(
|
||||
"pykrakenapi.KrakenAPI.get_ticker_information",
|
||||
return_value=TICKER_INFORMATION_RESPONSE,
|
||||
):
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
options={
|
||||
CONF_SCAN_INTERVAL: 60,
|
||||
CONF_TRACKED_ASSET_PAIRS: [
|
||||
"ADA/XBT",
|
||||
"ADA/ETH",
|
||||
"XBT/EUR",
|
||||
"XBT/GBP",
|
||||
"XBT/USD",
|
||||
"XBT/JPY",
|
||||
],
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user