Use service_calls fixture in litejet tests (#120933)

This commit is contained in:
epenet 2024-07-01 17:47:21 +02:00 committed by GitHub
parent dcf4e91234
commit 813fee663e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -14,7 +14,7 @@ import homeassistant.util.dt as dt_util
from . import async_init_integration
from tests.common import async_fire_time_changed_exact, async_mock_service
from tests.common import async_fire_time_changed_exact
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
@ -30,12 +30,6 @@ ENTITY_OTHER_SWITCH = "switch.mock_switch_2"
ENTITY_OTHER_SWITCH_NUMBER = 2
@pytest.fixture
def calls(hass: HomeAssistant) -> list[ServiceCall]:
"""Track calls to a mock service."""
return async_mock_service(hass, "test", "automation")
async def simulate_press(hass, mock_litejet, number):
"""Test to simulate a press."""
_LOGGER.info("*** simulate press of %d", number)
@ -101,7 +95,7 @@ async def setup_automation(hass, trigger):
async def test_simple(
hass: HomeAssistant, calls: list[ServiceCall], mock_litejet
hass: HomeAssistant, service_calls: list[ServiceCall], mock_litejet
) -> None:
"""Test the simplest form of a LiteJet trigger."""
await setup_automation(
@ -111,12 +105,12 @@ async def test_simple(
await simulate_press(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
await simulate_release(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 1
assert calls[0].data["id"] == 0
assert len(service_calls) == 1
assert service_calls[0].data["id"] == 0
async def test_only_release(
hass: HomeAssistant, calls: list[ServiceCall], mock_litejet
hass: HomeAssistant, service_calls: list[ServiceCall], mock_litejet
) -> None:
"""Test the simplest form of a LiteJet trigger."""
await setup_automation(
@ -125,11 +119,11 @@ async def test_only_release(
await simulate_release(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 0
assert len(service_calls) == 0
async def test_held_more_than_short(
hass: HomeAssistant, calls: list[ServiceCall], mock_litejet
hass: HomeAssistant, service_calls: list[ServiceCall], mock_litejet
) -> None:
"""Test a too short hold."""
await setup_automation(
@ -144,11 +138,11 @@ async def test_held_more_than_short(
await simulate_press(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
await simulate_time(hass, mock_litejet, timedelta(seconds=1))
await simulate_release(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 0
assert len(service_calls) == 0
async def test_held_more_than_long(
hass: HomeAssistant, calls: list[ServiceCall], mock_litejet
hass: HomeAssistant, service_calls: list[ServiceCall], mock_litejet
) -> None:
"""Test a hold that is long enough."""
await setup_automation(
@ -161,16 +155,16 @@ async def test_held_more_than_long(
)
await simulate_press(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 0
assert len(service_calls) == 0
await simulate_time(hass, mock_litejet, timedelta(seconds=3))
assert len(calls) == 1
assert calls[0].data["id"] == 0
assert len(service_calls) == 1
assert service_calls[0].data["id"] == 0
await simulate_release(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 1
assert len(service_calls) == 1
async def test_held_less_than_short(
hass: HomeAssistant, calls: list[ServiceCall], mock_litejet
hass: HomeAssistant, service_calls: list[ServiceCall], mock_litejet
) -> None:
"""Test a hold that is short enough."""
await setup_automation(
@ -184,14 +178,14 @@ async def test_held_less_than_short(
await simulate_press(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
await simulate_time(hass, mock_litejet, timedelta(seconds=1))
assert len(calls) == 0
assert len(service_calls) == 0
await simulate_release(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 1
assert calls[0].data["id"] == 0
assert len(service_calls) == 1
assert service_calls[0].data["id"] == 0
async def test_held_less_than_long(
hass: HomeAssistant, calls: list[ServiceCall], mock_litejet
hass: HomeAssistant, service_calls: list[ServiceCall], mock_litejet
) -> None:
"""Test a hold that is too long."""
await setup_automation(
@ -204,15 +198,15 @@ async def test_held_less_than_long(
)
await simulate_press(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 0
assert len(service_calls) == 0
await simulate_time(hass, mock_litejet, timedelta(seconds=3))
assert len(calls) == 0
assert len(service_calls) == 0
await simulate_release(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 0
assert len(service_calls) == 0
async def test_held_in_range_short(
hass: HomeAssistant, calls: list[ServiceCall], mock_litejet
hass: HomeAssistant, service_calls: list[ServiceCall], mock_litejet
) -> None:
"""Test an in-range trigger with a too short hold."""
await setup_automation(
@ -228,11 +222,11 @@ async def test_held_in_range_short(
await simulate_press(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
await simulate_time(hass, mock_litejet, timedelta(seconds=0.5))
await simulate_release(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 0
assert len(service_calls) == 0
async def test_held_in_range_just_right(
hass: HomeAssistant, calls: list[ServiceCall], mock_litejet
hass: HomeAssistant, service_calls: list[ServiceCall], mock_litejet
) -> None:
"""Test an in-range trigger with a just right hold."""
await setup_automation(
@ -246,16 +240,16 @@ async def test_held_in_range_just_right(
)
await simulate_press(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 0
assert len(service_calls) == 0
await simulate_time(hass, mock_litejet, timedelta(seconds=2))
assert len(calls) == 0
assert len(service_calls) == 0
await simulate_release(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 1
assert calls[0].data["id"] == 0
assert len(service_calls) == 1
assert service_calls[0].data["id"] == 0
async def test_held_in_range_long(
hass: HomeAssistant, calls: list[ServiceCall], mock_litejet
hass: HomeAssistant, service_calls: list[ServiceCall], mock_litejet
) -> None:
"""Test an in-range trigger with a too long hold."""
await setup_automation(
@ -269,15 +263,15 @@ async def test_held_in_range_long(
)
await simulate_press(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 0
assert len(service_calls) == 0
await simulate_time(hass, mock_litejet, timedelta(seconds=4))
assert len(calls) == 0
assert len(service_calls) == 0
await simulate_release(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 0
assert len(service_calls) == 0
async def test_reload(
hass: HomeAssistant, calls: list[ServiceCall], mock_litejet
hass: HomeAssistant, service_calls: list[ServiceCall], mock_litejet
) -> None:
"""Test reloading automation."""
await setup_automation(
@ -312,8 +306,8 @@ async def test_reload(
await hass.async_block_till_done()
await simulate_press(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)
assert len(calls) == 0
assert len(service_calls) == 1
await simulate_time(hass, mock_litejet, timedelta(seconds=5))
assert len(calls) == 0
assert len(service_calls) == 1
await simulate_time(hass, mock_litejet, timedelta(seconds=12.5))
assert len(calls) == 1
assert len(service_calls) == 2