Fix lingering tasks in KNX tests (#89201)

This commit is contained in:
Matthias Alphart 2023-03-06 02:19:42 +01:00 committed by GitHub
parent ff485d4648
commit 36dabaaea6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 16 deletions

View File

@ -69,3 +69,4 @@ Receive some telegrams and assert state.
- For `payload` in `assert_*` and `receive_*` use `int` for DPT 1, 2 and 3 payload values (DPTBinary) and `tuple` for other DPTs (DPTArray).
- `await self.hass.async_block_till_done()` is called before `KNXTestKit.assert_*` and after `KNXTestKit.receive_*` so you don't have to explicitly call it.
- Make sure to assert every outgoing telegram that was created in a test. `assert_no_telegram` is automatically called on teardown.
- Make sure to `knx.receive_response()` for every Read-request sent form StateUpdater, or to pass its timeout, to not have lingering tasks when finishing the tests.

View File

@ -10,6 +10,10 @@ from .conftest import KNXTestKit
from tests.common import async_capture_events
RAW_FLOAT_20_0 = (0x07, 0xD0)
RAW_FLOAT_21_0 = (0x0C, 0x1A)
RAW_FLOAT_22_0 = (0x0C, 0x4C)
async def test_climate_basic_temperature_set(
hass: HomeAssistant, knx: KNXTestKit
@ -34,6 +38,10 @@ async def test_climate_basic_temperature_set(
await knx.assert_read("1/2/3")
# read target temperature
await knx.assert_read("1/2/5")
# StateUpdater initialize state
await knx.receive_response("1/2/3", RAW_FLOAT_21_0)
await knx.receive_response("1/2/5", RAW_FLOAT_22_0)
events.clear()
# set new temperature
await hass.services.async_call(
@ -42,9 +50,8 @@ async def test_climate_basic_temperature_set(
{"entity_id": "climate.test", "temperature": 20},
blocking=True,
)
await knx.assert_write("1/2/4", (7, 208))
await knx.assert_write("1/2/4", RAW_FLOAT_20_0)
assert len(events) == 1
events.pop()
async def test_climate_hvac_mode(hass: HomeAssistant, knx: KNXTestKit) -> None:
@ -73,11 +80,12 @@ async def test_climate_hvac_mode(hass: HomeAssistant, knx: KNXTestKit) -> None:
await knx.assert_read("1/2/7")
await knx.assert_read("1/2/3")
# StateUpdater initialize state
await knx.receive_response("1/2/7", True)
await knx.receive_response("1/2/3", (0x21,))
await knx.receive_response("1/2/7", (0x01,))
await knx.receive_response("1/2/3", RAW_FLOAT_20_0)
# StateUpdater semaphore allows 2 concurrent requests
# read target temperature state
await knx.assert_read("1/2/5")
await knx.receive_response("1/2/5", RAW_FLOAT_22_0)
# turn hvac off
await hass.services.async_call(
@ -125,11 +133,13 @@ async def test_climate_preset_mode(
await knx.assert_read("1/2/7")
await knx.assert_read("1/2/3")
# StateUpdater initialize state
await knx.receive_response("1/2/7", True)
await knx.receive_response("1/2/3", (0x01,))
await knx.receive_response("1/2/7", (0x01,))
await knx.receive_response("1/2/3", RAW_FLOAT_21_0)
# StateUpdater semaphore allows 2 concurrent requests
# read target temperature state
await knx.assert_read("1/2/5")
await knx.receive_response("1/2/5", RAW_FLOAT_22_0)
events.clear()
# set preset mode
await hass.services.async_call(
@ -190,10 +200,11 @@ async def test_update_entity(hass: HomeAssistant, knx: KNXTestKit) -> None:
await knx.assert_read("1/2/7")
await knx.assert_read("1/2/3")
# StateUpdater initialize state
await knx.receive_response("1/2/7", True)
await knx.receive_response("1/2/3", (0x01,))
await knx.receive_response("1/2/7", (0x01,))
await knx.receive_response("1/2/3", RAW_FLOAT_21_0)
# StateUpdater semaphore allows 2 concurrent requests
await knx.assert_read("1/2/5")
await knx.receive_response("1/2/5", RAW_FLOAT_22_0)
# verify update entity retriggers group value reads to the bus
await hass.services.async_call(
@ -210,7 +221,6 @@ async def test_update_entity(hass: HomeAssistant, knx: KNXTestKit) -> None:
async def test_command_value_idle_mode(hass: HomeAssistant, knx: KNXTestKit) -> None:
"""Test KNX climate command_value."""
events = async_capture_events(hass, "state_changed")
await knx.setup_integration(
{
ClimateSchema.PLATFORM: {
@ -222,20 +232,17 @@ async def test_command_value_idle_mode(hass: HomeAssistant, knx: KNXTestKit) ->
}
}
)
assert len(hass.states.async_all()) == 1
assert len(events) == 1
events.pop()
await hass.async_block_till_done()
# read states state updater
await knx.assert_read("1/2/3")
await knx.assert_read("1/2/5")
# StateUpdater initialize state
await knx.receive_response("1/2/5", RAW_FLOAT_22_0)
await knx.receive_response("1/2/3", RAW_FLOAT_21_0)
# StateUpdater semaphore allows 2 concurrent requests
await knx.assert_read("1/2/6")
await knx.receive_response("1/2/6", (0x32,))
await knx.receive_response("1/2/3", (0x0C, 0x1A))
assert len(events) == 2
events.pop()
knx.assert_state("climate.test", HVACMode.HEAT, command_value=20)

View File

@ -31,6 +31,10 @@ async def test_cover_basic(hass: HomeAssistant, knx: KNXTestKit) -> None:
# read position state address and angle state address
await knx.assert_read("1/0/2")
await knx.assert_read("1/0/4")
# StateUpdater initialize state
await knx.receive_response("1/0/2", (0x0F,))
await knx.receive_response("1/0/4", (0x30,))
events.clear()
# open cover
await hass.services.async_call(