From 1a3be6a1337c13843a8f0c10f04879563873f7ce Mon Sep 17 00:00:00 2001 From: jan Iversen Date: Tue, 30 Mar 2021 16:35:46 +0200 Subject: [PATCH 1/3] Parametrize test_init. Add parameters to test_number_validator. --- tests/components/modbus/test_init.py | 31 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/tests/components/modbus/test_init.py b/tests/components/modbus/test_init.py index b0350c7271a58b..cd8edb656a091d 100644 --- a/tests/components/modbus/test_init.py +++ b/tests/components/modbus/test_init.py @@ -5,25 +5,30 @@ from homeassistant.components.modbus import number -async def test_number_validator(): +@pytest.mark.parametrize( + "value,value_type", + [ + (15, int), + (15.1, float), + ("15", int), + ("15.1", float), + (-15, int), + (-15.1, float), + ("-15", int), + ("-15.1", float), + ], +) +async def test_number_validator(value, value_type): """Test number validator.""" - # positive tests - value = number(15) - assert isinstance(value, int) + assert isinstance(number(value), value_type) - value = number(15.1) - assert isinstance(value, float) - value = number("15") - assert isinstance(value, int) +async def test_number_exception(): + """Test number exception.""" - value = number("15.1") - assert isinstance(value, float) - - # exception test try: - value = number("x15.1") + number("x15.1") except (vol.Invalid): return From 779c05250d072766bf5b5aeb2be1be01a4f3c20d Mon Sep 17 00:00:00 2001 From: jan Iversen Date: Tue, 30 Mar 2021 16:39:27 +0200 Subject: [PATCH 2/3] Remove if/else from test cases. --- .../modbus/test_modbus_binary_sensor.py | 20 ++- .../components/modbus/test_modbus_climate.py | 19 +- tests/components/modbus/test_modbus_cover.py | 19 +- tests/components/modbus/test_modbus_sensor.py | 97 +++++++--- tests/components/modbus/test_modbus_switch.py | 169 +++++++++++++----- 5 files changed, 233 insertions(+), 91 deletions(-) diff --git a/tests/components/modbus/test_modbus_binary_sensor.py b/tests/components/modbus/test_modbus_binary_sensor.py index e9120c0d8403b9..638238a0aa7795 100644 --- a/tests/components/modbus/test_modbus_binary_sensor.py +++ b/tests/components/modbus/test_modbus_binary_sensor.py @@ -22,7 +22,17 @@ @pytest.mark.parametrize("do_discovery", [False, True]) -@pytest.mark.parametrize("do_options", [False, True]) +@pytest.mark.parametrize( + "do_options", + [ + {}, + { + CONF_SLAVE: 10, + CONF_INPUT_TYPE: CALL_TYPE_DISCRETE, + CONF_DEVICE_CLASS: "door", + }, + ], +) async def test_config_binary_sensor(hass, do_discovery, do_options): """Run test for binary sensor.""" sensor_name = "test_sensor" @@ -31,13 +41,7 @@ async def test_config_binary_sensor(hass, do_discovery, do_options): CONF_ADDRESS: 51, } if do_options: - config_sensor.update( - { - CONF_SLAVE: 10, - CONF_INPUT_TYPE: CALL_TYPE_DISCRETE, - CONF_DEVICE_CLASS: "door", - } - ) + config_sensor.update() await base_config_test( hass, config_sensor, diff --git a/tests/components/modbus/test_modbus_climate.py b/tests/components/modbus/test_modbus_climate.py index bbdaed63995b3d..cd5cc9392d25b3 100644 --- a/tests/components/modbus/test_modbus_climate.py +++ b/tests/components/modbus/test_modbus_climate.py @@ -13,7 +13,16 @@ from .conftest import base_config_test, base_test -@pytest.mark.parametrize("do_options", [False, True]) +@pytest.mark.parametrize( + "do_options", + [ + {}, + { + CONF_SCAN_INTERVAL: 20, + CONF_DATA_COUNT: 2, + }, + ], +) async def test_config_climate(hass, do_options): """Run test for climate.""" device_name = "test_climate" @@ -23,13 +32,7 @@ async def test_config_climate(hass, do_options): CONF_CURRENT_TEMP: 117, CONF_SLAVE: 10, } - if do_options: - device_config.update( - { - CONF_SCAN_INTERVAL: 20, - CONF_DATA_COUNT: 2, - } - ) + device_config.update(do_options) await base_config_test( hass, device_config, diff --git a/tests/components/modbus/test_modbus_cover.py b/tests/components/modbus/test_modbus_cover.py index ff765314745994..a2b199d18e5a74 100644 --- a/tests/components/modbus/test_modbus_cover.py +++ b/tests/components/modbus/test_modbus_cover.py @@ -15,7 +15,16 @@ from .conftest import base_config_test, base_test -@pytest.mark.parametrize("do_options", [False, True]) +@pytest.mark.parametrize( + "do_options", + [ + {}, + { + CONF_SLAVE: 10, + CONF_SCAN_INTERVAL: 20, + }, + ], +) @pytest.mark.parametrize("read_type", [CALL_TYPE_COIL, CONF_REGISTER]) async def test_config_cover(hass, do_options, read_type): """Run test for cover.""" @@ -24,13 +33,7 @@ async def test_config_cover(hass, do_options, read_type): CONF_NAME: device_name, read_type: 1234, } - if do_options: - device_config.update( - { - CONF_SLAVE: 10, - CONF_SCAN_INTERVAL: 20, - } - ) + device_config.update(do_options) await base_config_test( hass, device_config, diff --git a/tests/components/modbus/test_modbus_sensor.py b/tests/components/modbus/test_modbus_sensor.py index 02fc4721186377..8196dc4092a7b4 100644 --- a/tests/components/modbus/test_modbus_sensor.py +++ b/tests/components/modbus/test_modbus_sensor.py @@ -31,21 +31,34 @@ from .conftest import base_config_test, base_test -@pytest.mark.parametrize("do_discovery", [False, True]) -@pytest.mark.parametrize("do_options", [False, True]) @pytest.mark.parametrize( - "do_type", [CALL_TYPE_REGISTER_HOLDING, CALL_TYPE_REGISTER_INPUT] -) -async def test_config_sensor(hass, do_discovery, do_options, do_type): - """Run test for sensor.""" - sensor_name = "test_sensor" - config_sensor = { - CONF_NAME: sensor_name, - CONF_ADDRESS: 51, - } - if do_options: - config_sensor.update( + "do_discovery, do_config", + [ + ( + False, + { + CONF_REGISTER: 51, + }, + ), + ( + False, + { + CONF_REGISTER: 51, + CONF_SLAVE: 10, + CONF_COUNT: 1, + CONF_DATA_TYPE: "int", + CONF_PRECISION: 0, + CONF_SCALE: 1, + CONF_REVERSE_ORDER: False, + CONF_OFFSET: 0, + CONF_REGISTER_TYPE: CALL_TYPE_REGISTER_HOLDING, + CONF_DEVICE_CLASS: "battery", + }, + ), + ( + False, { + CONF_REGISTER: 51, CONF_SLAVE: 10, CONF_COUNT: 1, CONF_DATA_TYPE: "int", @@ -53,17 +66,55 @@ async def test_config_sensor(hass, do_discovery, do_options, do_type): CONF_SCALE: 1, CONF_REVERSE_ORDER: False, CONF_OFFSET: 0, - CONF_INPUT_TYPE: do_type, + CONF_REGISTER_TYPE: CALL_TYPE_REGISTER_INPUT, CONF_DEVICE_CLASS: "battery", - } - ) - if not do_discovery: - # bridge difference in configuration - config_sensor[CONF_REGISTER] = config_sensor[CONF_ADDRESS] - del config_sensor[CONF_ADDRESS] - if do_options: - config_sensor[CONF_REGISTER_TYPE] = config_sensor[CONF_INPUT_TYPE] - del config_sensor[CONF_INPUT_TYPE] + }, + ), + ( + True, + { + CONF_ADDRESS: 51, + }, + ), + ( + True, + { + CONF_ADDRESS: 51, + CONF_SLAVE: 10, + CONF_COUNT: 1, + CONF_DATA_TYPE: "int", + CONF_PRECISION: 0, + CONF_SCALE: 1, + CONF_REVERSE_ORDER: False, + CONF_OFFSET: 0, + CONF_INPUT_TYPE: CALL_TYPE_REGISTER_HOLDING, + CONF_DEVICE_CLASS: "battery", + }, + ), + ( + True, + { + CONF_ADDRESS: 51, + CONF_SLAVE: 10, + CONF_COUNT: 1, + CONF_DATA_TYPE: "int", + CONF_PRECISION: 0, + CONF_SCALE: 1, + CONF_REVERSE_ORDER: False, + CONF_OFFSET: 0, + CONF_INPUT_TYPE: CALL_TYPE_REGISTER_INPUT, + CONF_DEVICE_CLASS: "battery", + }, + ), + ], +) +async def test_config_sensor(hass, do_discovery, do_config): + """Run test for sensor.""" + sensor_name = "test_sensor" + config_sensor = { + CONF_NAME: sensor_name, + } + config_sensor.update(do_config) await base_config_test( hass, config_sensor, diff --git a/tests/components/modbus/test_modbus_switch.py b/tests/components/modbus/test_modbus_switch.py index 8cd61443f1a568..37013c77b43298 100644 --- a/tests/components/modbus/test_modbus_switch.py +++ b/tests/components/modbus/test_modbus_switch.py @@ -31,57 +31,138 @@ from .conftest import base_config_test, base_test -@pytest.mark.parametrize("do_discovery", [False, True]) -@pytest.mark.parametrize("do_options", [False, True]) @pytest.mark.parametrize( - "read_type", [CALL_TYPE_REGISTER_HOLDING, CALL_TYPE_REGISTER_INPUT, CALL_TYPE_COIL] + "array_type, do_config", + [ + ( + None, + { + CONF_ADDRESS: 1234, + }, + ), + ( + None, + { + CONF_ADDRESS: 1234, + }, + ), + ( + None, + { + CONF_ADDRESS: 1234, + CONF_INPUT_TYPE: CALL_TYPE_COIL, + }, + ), + ( + None, + { + CONF_ADDRESS: 1234, + CONF_SLAVE: 1, + CONF_STATE_OFF: 0, + CONF_STATE_ON: 1, + CONF_VERIFY_REGISTER: 1235, + CONF_COMMAND_OFF: 0x00, + CONF_COMMAND_ON: 0x01, + CONF_DEVICE_CLASS: "switch", + CONF_INPUT_TYPE: CALL_TYPE_REGISTER_HOLDING, + }, + ), + ( + None, + { + CONF_ADDRESS: 1234, + CONF_SLAVE: 1, + CONF_STATE_OFF: 0, + CONF_STATE_ON: 1, + CONF_VERIFY_REGISTER: 1235, + CONF_COMMAND_OFF: 0x00, + CONF_COMMAND_ON: 0x01, + CONF_DEVICE_CLASS: "switch", + CONF_INPUT_TYPE: CALL_TYPE_REGISTER_INPUT, + }, + ), + ( + None, + { + CONF_ADDRESS: 1234, + CONF_INPUT_TYPE: CALL_TYPE_COIL, + CONF_SLAVE: 1, + CONF_DEVICE_CLASS: "switch", + CONF_INPUT_TYPE: CALL_TYPE_COIL, + }, + ), + ( + CONF_REGISTERS, + { + CONF_REGISTER: 1234, + CONF_COMMAND_OFF: 0x00, + CONF_COMMAND_ON: 0x01, + }, + ), + ( + CONF_REGISTERS, + { + CONF_REGISTER: 1234, + CONF_COMMAND_OFF: 0x00, + CONF_COMMAND_ON: 0x01, + }, + ), + ( + CONF_COILS, + { + CALL_TYPE_COIL: 1234, + CONF_SLAVE: 1, + }, + ), + ( + CONF_REGISTERS, + { + CONF_REGISTER: 1234, + CONF_COMMAND_OFF: 0x00, + CONF_COMMAND_ON: 0x01, + CONF_SLAVE: 1, + CONF_STATE_OFF: 0, + CONF_STATE_ON: 1, + CONF_VERIFY_REGISTER: 1235, + CONF_COMMAND_OFF: 0x00, + CONF_COMMAND_ON: 0x01, + CONF_VERIFY_STATE: True, + CONF_REGISTER_TYPE: CALL_TYPE_REGISTER_INPUT, + }, + ), + ( + CONF_REGISTERS, + { + CONF_REGISTER: 1234, + CONF_COMMAND_OFF: 0x00, + CONF_COMMAND_ON: 0x01, + CONF_SLAVE: 1, + CONF_STATE_OFF: 0, + CONF_STATE_ON: 1, + CONF_VERIFY_REGISTER: 1235, + CONF_COMMAND_OFF: 0x00, + CONF_COMMAND_ON: 0x01, + CONF_VERIFY_STATE: True, + CONF_REGISTER_TYPE: CALL_TYPE_REGISTER_HOLDING, + }, + ), + ( + CONF_COILS, + { + CALL_TYPE_COIL: 1234, + CONF_SLAVE: 1, + }, + ), + ], ) -async def test_config_switch(hass, do_discovery, do_options, read_type): +async def test_config_switch(hass, array_type, do_config): """Run test for switch.""" device_name = "test_switch" device_config = { CONF_NAME: device_name, } - if not do_discovery: - if read_type == CALL_TYPE_COIL: - array_type = CONF_COILS - device_config[CALL_TYPE_COIL] = 1234 - device_config[CONF_SLAVE] = 1 - else: - array_type = CONF_REGISTERS - device_config[CONF_REGISTER] = 1234 - device_config[CONF_COMMAND_OFF] = 0x00 - device_config[CONF_COMMAND_ON] = 0x01 - else: - array_type = None - device_config[CONF_ADDRESS] = 1234 - if read_type == CALL_TYPE_COIL: - device_config[CONF_INPUT_TYPE] = CALL_TYPE_COIL - - if do_options: - device_config[CONF_SLAVE] = 1 - if read_type != CALL_TYPE_COIL: - device_config.update( - { - CONF_STATE_OFF: 0, - CONF_STATE_ON: 1, - CONF_VERIFY_REGISTER: 1235, - CONF_COMMAND_OFF: 0x00, - CONF_COMMAND_ON: 0x01, - } - ) - if do_discovery: - device_config.update( - { - CONF_DEVICE_CLASS: "switch", - CONF_INPUT_TYPE: read_type, - } - ) - else: - if read_type != CALL_TYPE_COIL: - device_config[CONF_VERIFY_STATE] = True - device_config[CONF_REGISTER_TYPE] = read_type + device_config.update(do_config) await base_config_test( hass, @@ -90,7 +171,7 @@ async def test_config_switch(hass, do_discovery, do_options, read_type): SWITCH_DOMAIN, CONF_SWITCHES, array_type, - method_discovery=do_discovery, + method_discovery=(array_type is None), ) From a942080fa55c3ad8c6e177c76bda3f816303ab7d Mon Sep 17 00:00:00 2001 From: jan Iversen Date: Wed, 31 Mar 2021 09:13:56 +0200 Subject: [PATCH 3/3] correct use of do_options. --- tests/components/modbus/test_modbus.py | 6 ++++-- tests/components/modbus/test_modbus_binary_sensor.py | 3 +-- tests/components/modbus/test_modbus_climate.py | 2 +- tests/components/modbus/test_modbus_cover.py | 2 +- tests/components/modbus/test_modbus_sensor.py | 2 +- tests/components/modbus/test_modbus_switch.py | 2 +- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/components/modbus/test_modbus.py b/tests/components/modbus/test_modbus.py index 92519b754828d5..708519bbb440d6 100644 --- a/tests/components/modbus/test_modbus.py +++ b/tests/components/modbus/test_modbus.py @@ -55,9 +55,11 @@ async def test_config_modbus(hass, do_discovery, do_options, do_config): """Run test for modbus.""" config = { - DOMAIN: do_config, + DOMAIN: { + **do_config, + **do_options, + } } - config.update(do_options) await base_config_test( hass, None, diff --git a/tests/components/modbus/test_modbus_binary_sensor.py b/tests/components/modbus/test_modbus_binary_sensor.py index 638238a0aa7795..bc91e3714bbfd8 100644 --- a/tests/components/modbus/test_modbus_binary_sensor.py +++ b/tests/components/modbus/test_modbus_binary_sensor.py @@ -39,9 +39,8 @@ async def test_config_binary_sensor(hass, do_discovery, do_options): config_sensor = { CONF_NAME: sensor_name, CONF_ADDRESS: 51, + **do_options, } - if do_options: - config_sensor.update() await base_config_test( hass, config_sensor, diff --git a/tests/components/modbus/test_modbus_climate.py b/tests/components/modbus/test_modbus_climate.py index cd5cc9392d25b3..f932817b12e980 100644 --- a/tests/components/modbus/test_modbus_climate.py +++ b/tests/components/modbus/test_modbus_climate.py @@ -31,8 +31,8 @@ async def test_config_climate(hass, do_options): CONF_TARGET_TEMP: 117, CONF_CURRENT_TEMP: 117, CONF_SLAVE: 10, + **do_options, } - device_config.update(do_options) await base_config_test( hass, device_config, diff --git a/tests/components/modbus/test_modbus_cover.py b/tests/components/modbus/test_modbus_cover.py index a2b199d18e5a74..b101c6784d551d 100644 --- a/tests/components/modbus/test_modbus_cover.py +++ b/tests/components/modbus/test_modbus_cover.py @@ -32,8 +32,8 @@ async def test_config_cover(hass, do_options, read_type): device_config = { CONF_NAME: device_name, read_type: 1234, + **do_options, } - device_config.update(do_options) await base_config_test( hass, device_config, diff --git a/tests/components/modbus/test_modbus_sensor.py b/tests/components/modbus/test_modbus_sensor.py index 8196dc4092a7b4..dd485e59835a23 100644 --- a/tests/components/modbus/test_modbus_sensor.py +++ b/tests/components/modbus/test_modbus_sensor.py @@ -113,8 +113,8 @@ async def test_config_sensor(hass, do_discovery, do_config): sensor_name = "test_sensor" config_sensor = { CONF_NAME: sensor_name, + **do_config, } - config_sensor.update(do_config) await base_config_test( hass, config_sensor, diff --git a/tests/components/modbus/test_modbus_switch.py b/tests/components/modbus/test_modbus_switch.py index 37013c77b43298..8af8f3067e12b6 100644 --- a/tests/components/modbus/test_modbus_switch.py +++ b/tests/components/modbus/test_modbus_switch.py @@ -161,8 +161,8 @@ async def test_config_switch(hass, array_type, do_config): device_config = { CONF_NAME: device_name, + **do_config, } - device_config.update(do_config) await base_config_test( hass,