From 64d5dd1f6ba65afaf3ab01bf7df6471af5b5f683 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Wed, 31 Mar 2021 11:20:14 +0200 Subject: [PATCH] Remove if/else from modbus test cases (#48514) --- tests/components/modbus/test_init.py | 31 ++-- tests/components/modbus/test_modbus.py | 6 +- .../modbus/test_modbus_binary_sensor.py | 21 ++- .../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 +++++++++++++----- 7 files changed, 255 insertions(+), 107 deletions(-) diff --git a/tests/components/modbus/test_init.py b/tests/components/modbus/test_init.py index b0350c7271a..cd8edb656a0 100644 --- a/tests/components/modbus/test_init.py +++ b/tests/components/modbus/test_init.py @@ -5,25 +5,30 @@ import voluptuous as vol 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 diff --git a/tests/components/modbus/test_modbus.py b/tests/components/modbus/test_modbus.py index 92519b75482..708519bbb44 100644 --- a/tests/components/modbus/test_modbus.py +++ b/tests/components/modbus/test_modbus.py @@ -55,9 +55,11 @@ from .conftest import base_config_test 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 e9120c0d840..bc91e3714bb 100644 --- a/tests/components/modbus/test_modbus_binary_sensor.py +++ b/tests/components/modbus/test_modbus_binary_sensor.py @@ -22,22 +22,25 @@ 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_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" config_sensor = { CONF_NAME: sensor_name, CONF_ADDRESS: 51, + **do_options, } - if do_options: - config_sensor.update( - { - CONF_SLAVE: 10, - CONF_INPUT_TYPE: CALL_TYPE_DISCRETE, - CONF_DEVICE_CLASS: "door", - } - ) 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 bbdaed63995..f932817b12e 100644 --- a/tests/components/modbus/test_modbus_climate.py +++ b/tests/components/modbus/test_modbus_climate.py @@ -13,7 +13,16 @@ from homeassistant.const import CONF_NAME, CONF_SCAN_INTERVAL, CONF_SLAVE 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" @@ -22,14 +31,8 @@ async def test_config_climate(hass, do_options): CONF_TARGET_TEMP: 117, CONF_CURRENT_TEMP: 117, CONF_SLAVE: 10, + **do_options, } - if do_options: - device_config.update( - { - CONF_SCAN_INTERVAL: 20, - CONF_DATA_COUNT: 2, - } - ) 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 ff765314745..b101c6784d5 100644 --- a/tests/components/modbus/test_modbus_cover.py +++ b/tests/components/modbus/test_modbus_cover.py @@ -15,7 +15,16 @@ from homeassistant.const import ( 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.""" @@ -23,14 +32,8 @@ async def test_config_cover(hass, do_options, read_type): device_config = { CONF_NAME: device_name, read_type: 1234, + **do_options, } - if do_options: - device_config.update( - { - CONF_SLAVE: 10, - CONF_SCAN_INTERVAL: 20, - } - ) 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 02fc4721186..dd485e59835 100644 --- a/tests/components/modbus/test_modbus_sensor.py +++ b/tests/components/modbus/test_modbus_sensor.py @@ -31,21 +31,19 @@ from homeassistant.const import ( 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", @@ -53,17 +51,70 @@ 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_HOLDING, 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] + }, + ), + ( + 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_INPUT, + CONF_DEVICE_CLASS: "battery", + }, + ), + ( + 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, + **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 8cd61443f1a..8af8f3067e1 100644 --- a/tests/components/modbus/test_modbus_switch.py +++ b/tests/components/modbus/test_modbus_switch.py @@ -31,57 +31,138 @@ from homeassistant.const import ( 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, + **do_config, } - 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 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), )