mirror of
https://github.com/home-assistant/core.git
synced 2025-11-22 01:06:59 +00:00
78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
"""Test loading of the Tibber config entry."""
|
|
|
|
from unittest.mock import ANY, AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from homeassistant.components.recorder import Recorder
|
|
from homeassistant.components.tibber import DOMAIN, TibberDataAPIRuntimeData
|
|
from homeassistant.config_entries import ConfigEntryState
|
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
|
|
|
|
|
async def test_entry_unload(
|
|
recorder_mock: Recorder, hass: HomeAssistant, mock_tibber_setup: MagicMock
|
|
) -> None:
|
|
"""Test unloading the entry."""
|
|
entry = hass.config_entries.async_entry_for_domain_unique_id(DOMAIN, "tibber")
|
|
assert entry.state == ConfigEntryState.LOADED
|
|
|
|
await hass.config_entries.async_unload(entry.entry_id)
|
|
mock_tibber_setup.rt_disconnect.assert_called_once()
|
|
await hass.async_block_till_done(wait_background_tasks=True)
|
|
assert entry.state == ConfigEntryState.NOT_LOADED
|
|
|
|
|
|
@pytest.mark.usefixtures("recorder_mock")
|
|
async def test_data_api_runtime_creates_client(
|
|
hass: HomeAssistant,
|
|
) -> None:
|
|
"""Ensure the data API runtime creates and caches the client."""
|
|
session = MagicMock()
|
|
session.async_ensure_token_valid = AsyncMock() # type: ignore[assignment]
|
|
session.token = {CONF_ACCESS_TOKEN: "access-token"}
|
|
|
|
runtime = TibberDataAPIRuntimeData(session=session)
|
|
|
|
with patch(
|
|
"homeassistant.components.tibber.__init__.tibber_data_api.TibberDataAPI"
|
|
) as mock_client_cls:
|
|
mock_client = MagicMock()
|
|
mock_client.set_access_token = MagicMock()
|
|
mock_client_cls.return_value = mock_client
|
|
|
|
client = await runtime.async_get_client(hass)
|
|
|
|
mock_client_cls.assert_called_once_with("access-token", websession=ANY)
|
|
session.async_ensure_token_valid.assert_awaited_once()
|
|
mock_client.set_access_token.assert_called_once_with("access-token")
|
|
assert client is mock_client
|
|
|
|
mock_client.set_access_token.reset_mock()
|
|
session.async_ensure_token_valid.reset_mock()
|
|
|
|
cached_client = await runtime.async_get_client(hass)
|
|
|
|
mock_client_cls.assert_called_once()
|
|
session.async_ensure_token_valid.assert_awaited_once()
|
|
mock_client.set_access_token.assert_called_once_with("access-token")
|
|
assert cached_client is client
|
|
|
|
|
|
@pytest.mark.usefixtures("recorder_mock")
|
|
async def test_data_api_runtime_missing_token_raises(
|
|
hass: HomeAssistant,
|
|
) -> None:
|
|
"""Ensure missing tokens trigger reauthentication."""
|
|
session = MagicMock()
|
|
session.async_ensure_token_valid = AsyncMock() # type: ignore[assignment]
|
|
session.token = {}
|
|
|
|
runtime = TibberDataAPIRuntimeData(session=session)
|
|
|
|
with pytest.raises(ConfigEntryAuthFailed):
|
|
await runtime.async_get_client(hass)
|
|
session.async_ensure_token_valid.assert_awaited_once()
|