Convert evohome's test factory into an async generator (#126925)

pull/127488/head^2
David Bonnes 2024-10-04 07:29:36 +01:00 committed by GitHub
parent 8754b54d81
commit 49e634a62f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 39 deletions

View File

@ -2,7 +2,7 @@
from __future__ import annotations
from collections.abc import Callable
from collections.abc import AsyncGenerator, Callable
from datetime import datetime, timedelta, timezone
from http import HTTPMethod
from typing import Any
@ -112,16 +112,16 @@ def config() -> dict[str, str]:
async def setup_evohome(
hass: HomeAssistant,
test_config: dict[str, str],
config: dict[str, str],
install: str = "default",
) -> MagicMock:
) -> AsyncGenerator[MagicMock]:
"""Set up the evohome integration and return its client.
The class is mocked here to check the client was instantiated with the correct args.
"""
# set the time zone as for the active evohome location
loc_idx: int = test_config.get("location_idx", 0) # type: ignore[assignment]
loc_idx: int = config.get("location_idx", 0) # type: ignore[assignment]
try:
locn = user_locations_config_fixture(install)[loc_idx]
@ -140,16 +140,16 @@ async def setup_evohome(
):
mock_client.side_effect = EvohomeClient
assert await async_setup_component(hass, DOMAIN, {DOMAIN: test_config})
assert await async_setup_component(hass, DOMAIN, {DOMAIN: config})
await hass.async_block_till_done()
mock_client.assert_called_once()
assert mock_client.call_args.args[0] == test_config[CONF_USERNAME]
assert mock_client.call_args.args[1] == test_config[CONF_PASSWORD]
assert mock_client.call_args.args[0] == config[CONF_USERNAME]
assert mock_client.call_args.args[1] == config[CONF_PASSWORD]
assert isinstance(mock_client.call_args.kwargs["session"], ClientSession)
assert mock_client.account_info is not None
return mock_client
yield mock_client

View File

@ -23,8 +23,9 @@ async def test_entities(
"""Test entities and state after setup of a Honeywell TCC-compatible system."""
# some extended state attrs are relative the current time
freezer.move_to("2024-07-10 12:00:00+00:00")
freezer.move_to("2024-07-10T12:00:00Z")
await setup_evohome(hass, config, install=install)
async for _ in setup_evohome(hass, config, install=install):
pass
assert hass.states.async_all() == snapshot

View File

@ -96,12 +96,11 @@ async def test_auth_tokens_null(
hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_NULL[idx]}
mock_client = await setup_evohome(hass, config, install=install)
# Confirm client was instantiated without tokens, as cache was empty...
assert SZ_REFRESH_TOKEN not in mock_client.call_args.kwargs
assert SZ_ACCESS_TOKEN not in mock_client.call_args.kwargs
assert SZ_ACCESS_TOKEN_EXPIRES not in mock_client.call_args.kwarg
async for mock_client in setup_evohome(hass, config, install=install):
# Confirm client was instantiated without tokens, as cache was empty...
assert SZ_REFRESH_TOKEN not in mock_client.call_args.kwargs
assert SZ_ACCESS_TOKEN not in mock_client.call_args.kwargs
assert SZ_ACCESS_TOKEN_EXPIRES not in mock_client.call_args.kwarg
# Confirm the expected tokens were cached to storage...
data: _TokenStoreT = hass_storage[DOMAIN]["data"]
@ -128,14 +127,13 @@ async def test_auth_tokens_same(
hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_DATA[idx]}
mock_client = await setup_evohome(hass, config, install="minimal")
# Confirm client was instantiated with the cached tokens...
assert mock_client.call_args.kwargs[SZ_REFRESH_TOKEN] == REFRESH_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN] == ACCESS_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN_EXPIRES] == dt_aware_to_naive(
ACCESS_TOKEN_EXP_DTM
)
async for mock_client in setup_evohome(hass, config, install=install):
# Confirm client was instantiated with the cached tokens...
assert mock_client.call_args.kwargs[SZ_REFRESH_TOKEN] == REFRESH_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN] == ACCESS_TOKEN
assert mock_client.call_args.kwargs[
SZ_ACCESS_TOKEN_EXPIRES
] == dt_aware_to_naive(ACCESS_TOKEN_EXP_DTM)
# Confirm the expected tokens were cached to storage...
data: _TokenStoreT = hass_storage[DOMAIN]["data"]
@ -165,14 +163,13 @@ async def test_auth_tokens_past(
hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": test_data}
mock_client = await setup_evohome(hass, config, install="minimal")
# Confirm client was instantiated with the cached tokens...
assert mock_client.call_args.kwargs[SZ_REFRESH_TOKEN] == REFRESH_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN] == ACCESS_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN_EXPIRES] == dt_aware_to_naive(
dt_dtm
)
async for mock_client in setup_evohome(hass, config, install=install):
# Confirm client was instantiated with the cached tokens...
assert mock_client.call_args.kwargs[SZ_REFRESH_TOKEN] == REFRESH_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN] == ACCESS_TOKEN
assert mock_client.call_args.kwargs[
SZ_ACCESS_TOKEN_EXPIRES
] == dt_aware_to_naive(dt_dtm)
# Confirm the expected tokens were cached to storage...
data: _TokenStoreT = hass_storage[DOMAIN]["data"]
@ -199,13 +196,13 @@ async def test_auth_tokens_diff(
hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_DATA[idx]}
mock_client = await setup_evohome(
hass, config | {CONF_USERNAME: USERNAME_DIFF}, install="minimal"
)
# Confirm client was instantiated without tokens, as username was different...
assert SZ_REFRESH_TOKEN not in mock_client.call_args.kwargs
assert SZ_ACCESS_TOKEN not in mock_client.call_args.kwargs
assert SZ_ACCESS_TOKEN_EXPIRES not in mock_client.call_args.kwarg
async for mock_client in setup_evohome(
hass, config | {CONF_USERNAME: USERNAME_DIFF}, install=install
):
# Confirm client was instantiated without tokens, as username was different...
assert SZ_REFRESH_TOKEN not in mock_client.call_args.kwargs
assert SZ_ACCESS_TOKEN not in mock_client.call_args.kwargs
assert SZ_ACCESS_TOKEN_EXPIRES not in mock_client.call_args.kwarg
# Confirm the expected tokens were cached to storage...
data: _TokenStoreT = hass_storage[DOMAIN]["data"]