hass-core/tests/components/knx
2024-01-07 23:26:46 +01:00
..
fixtures Add a custom panel for KNX with a group monitor (#92355) 2023-05-11 00:13:22 +02:00
snapshots Use snapshot assertion for KNX diagnostics (#98724) 2023-08-21 11:48:55 +02:00
__init__.py Refactor KNX tests (#53183) 2021-07-19 23:39:19 -05:00
conftest.py Move overlapping pylint rules to ruff, disable mypy overlap (#94359) 2023-06-27 17:42:46 +02:00
README.md Fix lingering tasks in KNX tests (#89201) 2023-03-05 20:19:42 -05:00
test_binary_sensor.py Update k-l* tests to use entity & device registry fixtures (#103929) 2023-11-13 20:02:33 +01:00
test_button.py Deduplicate constants E-Z (#105657) 2023-12-13 17:05:37 +01:00
test_climate.py Add KNX interface device with diagnostic entities (#89213) 2023-03-19 02:13:52 -11:00
test_config_flow.py Switch formatting from black to ruff-format (#102893) 2023-11-27 14:38:59 +01:00
test_cover.py KNX Cover: Use absolute tilt position if available (#96192) 2023-07-09 12:00:51 +02:00
test_date.py Add date platform to KNX (#97154) 2023-07-24 21:12:37 +02:00
test_datetime.py Add datetime platform to KNX (#97190) 2023-07-25 11:04:05 +02:00
test_device_trigger.py Fix KNX telegram device trigger not firing after integration reload (#107388) 2024-01-07 20:32:17 +01:00
test_diagnostic.py Use snapshot assertion for KNX diagnostics (#98724) 2023-08-21 11:48:55 +02:00
test_events.py Update xknx to 2.9.0 (#91282) 2023-04-22 18:25:14 +02:00
test_expose.py Fix KNX expose default value when attribute is None (#103446) 2023-11-06 08:39:40 +01:00
test_fan.py Add KNX interface device with diagnostic entities (#89213) 2023-03-19 02:13:52 -11:00
test_init.py Restore KNX telegram history (#95800) 2023-07-09 21:15:55 +02:00
test_interface_device.py Use async_on_remove for KNX entities removal (#95658) 2023-07-01 07:16:45 -04:00
test_light.py Import util.dt as dt_util in components/[k-o]* (#93760) 2023-05-29 23:02:14 +02:00
test_notify.py Add type hints to integration tests (part 12) (#87997) 2023-02-13 13:03:51 +01:00
test_number.py Add type hints to integration tests (part 12) (#87997) 2023-02-13 13:03:51 +01:00
test_scene.py Add KNX interface device with diagnostic entities (#89213) 2023-03-19 02:13:52 -11:00
test_select.py Raise ServiceValidationError on invalid select option (#106350) 2023-12-27 09:45:55 +01:00
test_sensor.py Add KNX interface device with diagnostic entities (#89213) 2023-03-19 02:13:52 -11:00
test_services.py Move KNX service registration to async_setup (#106635) 2024-01-07 23:26:46 +01:00
test_switch.py Add KNX interface device with diagnostic entities (#89213) 2023-03-19 02:13:52 -11:00
test_telegrams.py Restore KNX telegram history (#95800) 2023-07-09 21:15:55 +02:00
test_text.py Add type hints to integration tests (part 12) (#87997) 2023-02-13 13:03:51 +01:00
test_time.py Add time platform to KNX (#95302) 2023-06-28 15:19:32 +02:00
test_weather.py Add KNX interface device with diagnostic entities (#89213) 2023-03-19 02:13:52 -11:00
test_websocket.py KNX: Provide project data and parser version via websocket (#100676) 2023-09-26 23:17:37 +02:00

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 pushed to an assertion queue. Assert them in order they were sent.

  • knx.assert_no_telegram Asserts that no telegram was sent (assertion queue is empty).
  • knx.assert_telegram_count(count: int) Asserts that count telegrams were sent.
  • knx.assert_read(group_address: str) Asserts that a GroupValueRead telegram was sent to group_address. The telegram will be removed from the assertion queue.
  • knx.assert_response(group_address: str, payload: int | tuple[int, ...]) Asserts that a GroupValueResponse telegram with payload was sent to group_address. The telegram will be removed from the assertion queue.
  • knx.assert_write(group_address: str, payload: int | tuple[int, ...]) Asserts that a GroupValueWrite telegram with payload was sent to group_address. The telegram will be removed from the assertion queue.

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.