hass-core/tests/components/knx
2022-11-10 14:25:41 +01:00
..
__init__.py Refactor KNX tests (#53183) 2021-07-19 23:39:19 -05:00
conftest.py Wait for config entry platforms in KNX (#77437) 2022-08-28 12:36:31 +02:00
README.md Test KNX switch (#53289) 2021-07-21 15:04:14 -07:00
test_binary_sensor.py Use mock_restore_cache in tests (#77298) 2022-08-25 09:28:53 +02:00
test_button.py Use new Platform enum in KNX (#60902) 2021-12-03 09:29:38 -08:00
test_climate.py Cleanup root component imports in tests (#78893) 2022-09-21 06:57:41 -10:00
test_config_flow.py Refactor KNX Config and Options flows (#80641) 2022-11-10 14:25:41 +01:00
test_cover.py Full test coverage for KNX integration (#69697) 2022-04-09 00:33:50 +02:00
test_diagnostic.py Apply hass-relative-import to tests (i-r) (#78732) 2022-09-19 09:46:59 +02:00
test_events.py Cleanup KNX integration (#68820) 2022-03-29 16:46:02 +02:00
test_expose.py Fix KNX Expose for strings longer than 14 bytes (#63026) 2022-01-29 14:32:12 +01:00
test_fan.py Use new Platform enum in KNX (#60902) 2021-12-03 09:29:38 -08:00
test_init.py Add support for IP secure to KNX config flow (#68906) 2022-03-30 21:10:47 +02:00
test_light.py Migrate KNX to use kelvin for color temperature (#81112) 2022-10-27 16:00:34 -04:00
test_notify.py Add optional type for KNX notify entity configuration (#70451) 2022-04-23 12:21:58 +02:00
test_number.py Migrate knx NumberEntity to native_value (#73536) 2022-06-15 14:39:56 +02:00
test_scene.py Cleanup deprecated async_get_registry in tests (#72059) 2022-05-18 13:12:38 +02:00
test_select.py Use mock_restore_cache in tests (#77298) 2022-08-25 09:28:53 +02:00
test_sensor.py Use new Platform enum in KNX (#60902) 2021-12-03 09:29:38 -08:00
test_services.py Allow sending GroupValueResponse telegrams with knx.send service (#62639) 2021-12-29 18:15:48 +01:00
test_switch.py Use mock_restore_cache in tests (#77298) 2022-08-25 09:28:53 +02:00
test_weather.py Weather unit conversion (#73441) 2022-06-23 10:48:30 +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.