Rework entity description functions in Tessie (#106287)

* use lamdba to return the library function

* Rename mocks

* lambda number

* Lambda button

* Add missing

* Remove context manager
This commit is contained in:
Brett Adams 2023-12-23 22:45:06 +10:00 committed by GitHub
parent ea7c839423
commit 043f3e640c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 57 deletions

View file

@ -1,8 +1,7 @@
"""Tessie common helpers for tests."""
from contextlib import contextmanager
from http import HTTPStatus
from unittest.mock import AsyncMock, patch
from unittest.mock import patch
from aiohttp import ClientConnectionError, ClientResponseError
from aiohttp.client import RequestInfo
@ -10,7 +9,6 @@ from aiohttp.client import RequestInfo
from homeassistant.components.tessie.const import DOMAIN
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityDescription
from tests.common import MockConfigEntry, load_json_object_fixture
@ -61,16 +59,3 @@ async def setup_platform(hass: HomeAssistant, side_effect=None):
await hass.async_block_till_done()
return mock_entry
@contextmanager
def patch_description(
key: str, attr: str, descriptions: tuple[EntityDescription]
) -> AsyncMock:
"""Patch a description."""
to_patch = next(filter(lambda x: x.key == key, descriptions))
original = to_patch.func
mock = AsyncMock()
object.__setattr__(to_patch, attr, mock)
yield mock
object.__setattr__(to_patch, attr, original)

View file

@ -1,11 +1,11 @@
"""Test the Tessie button platform."""
from unittest.mock import patch
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.components.tessie.button import DESCRIPTIONS
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from .common import patch_description, setup_platform
from .common import setup_platform
async def test_buttons(hass: HomeAssistant) -> None:
@ -14,7 +14,9 @@ async def test_buttons(hass: HomeAssistant) -> None:
await setup_platform(hass)
# Test wake button
with patch_description("wake", "func", DESCRIPTIONS) as mock_wake:
with patch(
"homeassistant.components.tessie.button.wake",
) as mock_wake:
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,

View file

@ -1,12 +1,13 @@
"""Test the Tessie number platform."""
from unittest.mock import patch
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN, SERVICE_SET_VALUE
from homeassistant.components.tessie.number import DESCRIPTIONS
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from .common import TEST_VEHICLE_STATE_ONLINE, patch_description, setup_platform
from .common import TEST_VEHICLE_STATE_ONLINE, setup_platform
async def test_numbers(hass: HomeAssistant) -> None:
@ -33,8 +34,8 @@ async def test_numbers(hass: HomeAssistant) -> None:
)
# Test number set value functions
with patch_description(
"charge_state_charge_current_request", "func", DESCRIPTIONS
with patch(
"homeassistant.components.tessie.number.set_charging_amps",
) as mock_set_charging_amps:
await hass.services.async_call(
NUMBER_DOMAIN,
@ -45,8 +46,8 @@ async def test_numbers(hass: HomeAssistant) -> None:
assert hass.states.get("number.test_charge_current").state == "16.0"
mock_set_charging_amps.assert_called_once()
with patch_description(
"charge_state_charge_limit_soc", "func", DESCRIPTIONS
with patch(
"homeassistant.components.tessie.number.set_charge_limit",
) as mock_set_charge_limit:
await hass.services.async_call(
NUMBER_DOMAIN,
@ -57,8 +58,8 @@ async def test_numbers(hass: HomeAssistant) -> None:
assert hass.states.get("number.test_charge_limit").state == "80.0"
mock_set_charge_limit.assert_called_once()
with patch_description(
"vehicle_state_speed_limit_mode_current_limit_mph", "func", DESCRIPTIONS
with patch(
"homeassistant.components.tessie.number.set_speed_limit",
) as mock_set_speed_limit:
await hass.services.async_call(
NUMBER_DOMAIN,

View file

@ -30,9 +30,8 @@ async def test_switches(hass: HomeAssistant) -> None:
)
with patch(
"homeassistant.components.tessie.entity.TessieEntity.run",
return_value=True,
) as mock_run:
"homeassistant.components.tessie.switch.start_charging",
) as mock_start_charging:
# Test Switch On
await hass.services.async_call(
SWITCH_DOMAIN,
@ -40,9 +39,10 @@ async def test_switches(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: ["switch.test_charge"]},
blocking=True,
)
mock_run.assert_called_once()
mock_run.reset_mock()
mock_start_charging.assert_called_once()
with patch(
"homeassistant.components.tessie.switch.stop_charging",
) as mock_stop_charging:
# Test Switch Off
await hass.services.async_call(
SWITCH_DOMAIN,
@ -50,4 +50,4 @@ async def test_switches(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: ["switch.test_charge"]},
blocking=True,
)
mock_run.assert_called_once()
mock_stop_charging.assert_called_once()