hass-core/tests/components/knx
Matthias Alphart 34e72ea16a
Add support for KNX UI to create light entities (#122342)
* Add light to KNX UI-createable entity platforms

* review from switch

* Add a test
2024-07-22 09:35:29 +02:00
..
fixtures Create, update and delete KNX entities from UI / WS-commands (#104079) 2024-07-21 20:01:48 +02:00
snapshots Use snapshot assertion for KNX diagnostics (#98724) 2023-08-21 11:48:55 +02:00
__init__.py Create, update and delete KNX entities from UI / WS-commands (#104079) 2024-07-21 20:01:48 +02:00
conftest.py Add support for KNX UI to create light entities (#122342) 2024-07-22 09:35:29 +02:00
README.md Create, update and delete KNX entities from UI / WS-commands (#104079) 2024-07-21 20:01:48 +02:00
test_binary_sensor.py Add empty line after module docstring [tests f-k] (#112709) 2024-03-08 14:50:04 +01:00
test_button.py Add empty line after module docstring [tests f-k] (#112709) 2024-03-08 14:50:04 +01:00
test_climate.py Fix KNX climate.set_hvac_mode not turning on (#119012) 2024-06-06 22:40:04 +02:00
test_config_flow.py Create, update and delete KNX entities from UI / WS-commands (#104079) 2024-07-21 20:01:48 +02:00
test_config_store.py Create, update and delete KNX entities from UI / WS-commands (#104079) 2024-07-21 20:01:48 +02:00
test_cover.py
test_date.py Add empty line after module docstring [tests f-k] (#112709) 2024-03-08 14:50:04 +01:00
test_datetime.py Fix blocking I/O in the event loop when loading timezones (#117721) 2024-05-20 11:06:03 +02:00
test_device.py Create, update and delete KNX entities from UI / WS-commands (#104079) 2024-07-21 20:01:48 +02:00
test_device_trigger.py Use service_calls fixture in knx tests (#120930) 2024-07-01 14:33:01 +02:00
test_diagnostic.py Move mock_hass_config fixture to decorator (#118807) 2024-06-04 16:05:56 +02:00
test_events.py Add empty line after module docstring (2) [tests.components] (#112737) 2024-03-08 19:16:21 +01:00
test_expose.py Add value_template option to KNX expose (#117732) 2024-05-22 00:09:42 +02:00
test_fan.py Add empty line after module docstring [tests f-k] (#112709) 2024-03-08 14:50:04 +01:00
test_init.py Use is in ConfigEntryState enum comparison in tests (A-M) (#114925) 2024-04-05 17:16:55 +02:00
test_interface_device.py Create, update and delete KNX entities from UI / WS-commands (#104079) 2024-07-21 20:01:48 +02:00
test_knx_selectors.py Create, update and delete KNX entities from UI / WS-commands (#104079) 2024-07-21 20:01:48 +02:00
test_light.py Add support for KNX UI to create light entities (#122342) 2024-07-22 09:35:29 +02:00
test_notify.py Migrate KNX notify service to entity platform (#115665) 2024-04-24 07:51:02 +02:00
test_number.py Raise ServiceValidationError on number out of range exception (#114589) 2024-04-02 12:22:00 +02:00
test_repairs.py Improve repair issue when notify service is still being used (#118855) 2024-06-05 15:41:22 +02:00
test_scene.py
test_select.py Add empty line after module docstring (2) [tests.components] (#112737) 2024-03-08 19:16:21 +01:00
test_sensor.py Add empty line after module docstring [tests f-k] (#112709) 2024-03-08 14:50:04 +01:00
test_services.py Enable Ruff PT012 (#113957) 2024-06-08 17:59:08 +02:00
test_switch.py Create, update and delete KNX entities from UI / WS-commands (#104079) 2024-07-21 20:01:48 +02:00
test_telegrams.py Fix consider-using-enumerate warnings in tests (#119506) 2024-06-12 16:27:56 +02:00
test_text.py Add empty line after module docstring [tests f-k] (#112709) 2024-03-08 14:50:04 +01:00
test_time.py Add empty line after module docstring [tests f-k] (#112709) 2024-03-08 14:50:04 +01:00
test_trigger.py Use service_calls fixture in knx tests (#120930) 2024-07-01 14:33:01 +02:00
test_weather.py Add empty line after module docstring [tests f-k] (#112709) 2024-03-08 14:50:04 +01:00
test_websocket.py Create, update and delete KNX entities from UI / WS-commands (#104079) 2024-07-21 20:01:48 +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, response: int | tuple[int, ...] | None = None) Asserts that a GroupValueRead telegram was sent to group_address. The telegram will be removed from the assertion queue. 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, ...]) 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.