core/tests/components/knx
Marc Mueller c1b24e6ba2
Small typing improvements (#126818)
* Add from __future__ import annotations

* Use PEP 695 type aliases

* Fix generator typing
2024-09-26 13:51:27 +02:00
..
fixtures Fix loading KNX UI entities with entity category set (#126290) 2024-09-20 11:16:58 +02:00
snapshots
README.md Fix loading KNX UI entities with entity category set (#126290) 2024-09-20 11:16:58 +02:00
__init__.py Small typing improvements (#126818) 2024-09-26 13:51:27 +02:00
conftest.py Fix loading KNX UI entities with entity category set (#126290) 2024-09-20 11:16:58 +02:00
test_binary_sensor.py
test_button.py Remove KNX yaml config from `hass.data` (#124050) 2024-09-09 09:01:21 +02:00
test_climate.py Add fan support for KNX climate entities (#126368) 2024-09-24 21:38:09 +02:00
test_config_flow.py
test_config_store.py
test_cover.py
test_date.py Add alias to DOMAIN import in tests [h-m] (#125577) 2024-09-09 15:21:01 +02:00
test_datetime.py Add alias to DOMAIN import in tests [h-m] (#125577) 2024-09-09 15:21:01 +02:00
test_device.py Fix loading KNX UI entities with entity category set (#126290) 2024-09-20 11:16:58 +02:00
test_device_trigger.py
test_diagnostic.py
test_events.py
test_expose.py
test_fan.py
test_init.py
test_interface_device.py
test_knx_selectors.py
test_light.py Fix loading KNX UI entities with entity category set (#126290) 2024-09-20 11:16:58 +02:00
test_notify.py
test_number.py
test_repairs.py Add helper functions for repair tests (#125886) 2024-09-14 03:31:44 +02:00
test_scene.py
test_select.py
test_sensor.py
test_services.py
test_switch.py
test_telegrams.py Remove KNX yaml config from `hass.data` (#124050) 2024-09-09 09:01:21 +02:00
test_text.py
test_time.py Add alias to DOMAIN import in tests [h-m] (#125577) 2024-09-09 15:21:01 +02:00
test_trigger.py
test_weather.py
test_websocket.py Remove KNX yaml config from `hass.data` (#124050) 2024-09-09 09:01:21 +02:00

README.md

Testing the KNX integration

A KNXTestKit instance can be requested from a fixture. It provides convenience methods to test outgoing KNX telegrams and inject incoming telegrams. To test something add a test function requesting the hass and knx fixture and set up the KNX integration by passing a KNX config dict to knx.setup_integration.

async def test_something(hass, knx):
    await knx.setup_integration({
            "switch": {
                "name": "test_switch",
                "address": "1/2/3",
            }
        }
    )

Asserting outgoing telegrams

All outgoing telegrams are appended to an assertion list. Assert them in order they were sent or pass ignore_order=True to the assertion method.

  • knx.assert_no_telegram Asserts that no telegram was sent (assertion list is empty).
  • knx.assert_telegram_count(count: int) Asserts that count telegrams were sent.
  • knx.assert_read(group_address: str, response: int | tuple[int, ...] | None = None, ignore_order: bool = False) Asserts that a GroupValueRead telegram was sent to group_address. The telegram will be removed from the assertion list. Optionally inject incoming GroupValueResponse telegram after reception to clear the value reader waiting task. This can also be done manually with knx.receive_response.
  • knx.assert_response(group_address: str, payload: int | tuple[int, ...], ignore_order: bool = False) Asserts that a GroupValueResponse telegram with payload was sent to group_address. The telegram will be removed from the assertion list.
  • knx.assert_write(group_address: str, payload: int | tuple[int, ...], ignore_order: bool = False) Asserts that a GroupValueWrite telegram with payload was sent to group_address. The telegram will be removed from the assertion list.

Change some states or call some services and assert outgoing telegrams.

    # turn on switch
    await hass.services.async_call(
        "switch", "turn_on", {"entity_id": "switch.test_switch"}, blocking=True
    )
    # assert ON telegram
    await knx.assert_write("1/2/3", True)

Injecting incoming telegrams

  • knx.receive_read(group_address: str) Inject and process a GroupValueRead telegram addressed to group_address.
  • knx.receive_response(group_address: str, payload: int | tuple[int, ...]) Inject and process a GroupValueResponse telegram addressed to group_address containing payload.
  • knx.receive_write(group_address: str, payload: int | tuple[int, ...]) Inject and process a GroupValueWrite telegram addressed to group_address containing payload.

Receive some telegrams and assert state.

    # receive OFF telegram
    await knx.receive_write("1/2/3", False)
    # assert OFF state
    state = hass.states.get("switch.test_switch")
    assert state.state is STATE_OFF

Notes

  • 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.