1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Update modbus test harness (#44892)

* Update test harness to allow discovery_info testing.

This is a needed update, before the whole config is converted
to the new structure, because it allows test of both the old
and new configuration style.

The following changes have been made:
- Combine test functions into a single base_test.
- Prepare for new mock by combining the 2 common test functions into one.
- Change mock to be only modbusHub
- Do not replace whole modbus class, but only the class that
  connects to pymodbus (modbusHub). This allows test of modbus
  configurations and not only component configurations, and is needed
  when testing discovery activated platform.
- Add test of climate.
  Warning this is merely test of discovery,
  the real cover tests still needs to be added.
- Add test of cover.
  Warning this is merely test of discovery,
  the real cover tests still needs to be added.

* Update comment for old config

* Do not use hass.data, but instead patch pymodbus library.

* Add test of configuration (old/new way as available).

* Move assert to test function.

Make assert more understandable by removing it from the helper.

add base_config_test (check just calls base_test) to make it clear if
test is wanted or just controlling the config.

* use setup_component to load Modbus since it is an integration.

* Optimized flow in test helper.
This commit is contained in:
jan iversen
2021-02-14 17:40:30 +01:00
committed by GitHub
parent 9777608861
commit 855bd653b4
6 changed files with 487 additions and 207 deletions

View File

@@ -1,11 +1,8 @@
"""The tests for the Modbus switch component."""
from datetime import timedelta
import pytest
from homeassistant.components.modbus.const import (
CALL_TYPE_COIL,
CALL_TYPE_REGISTER_HOLDING,
CONF_COILS,
CONF_REGISTER,
CONF_REGISTERS,
@@ -20,7 +17,43 @@ from homeassistant.const import (
STATE_ON,
)
from .conftest import run_base_read_test, setup_base_test
from .conftest import base_config_test, base_test
@pytest.mark.parametrize("do_options", [False, True])
@pytest.mark.parametrize("read_type", [CALL_TYPE_COIL, CONF_REGISTER])
async def test_config_switch(hass, do_options, read_type):
"""Run test for switch."""
device_name = "test_switch"
if read_type == CONF_REGISTER:
device_config = {
CONF_NAME: device_name,
CONF_REGISTER: 1234,
CONF_SLAVE: 1,
CONF_COMMAND_OFF: 0x00,
CONF_COMMAND_ON: 0x01,
}
array_type = CONF_REGISTERS
else:
device_config = {
CONF_NAME: device_name,
read_type: 1234,
CONF_SLAVE: 10,
}
array_type = CONF_COILS
if do_options:
device_config.update({})
await base_config_test(
hass,
device_config,
device_name,
SWITCH_DOMAIN,
None,
array_type,
method_discovery=False,
)
@pytest.mark.parametrize(
@@ -48,32 +81,26 @@ from .conftest import run_base_read_test, setup_base_test
),
],
)
async def test_coil_switch(hass, mock_hub, regs, expected):
async def test_coil_switch(hass, regs, expected):
"""Run test for given config."""
switch_name = "modbus_test_switch"
scan_interval = 5
entity_id, now, device = await setup_base_test(
switch_name,
state = await base_test(
hass,
mock_hub,
{
CONF_COILS: [
{CONF_NAME: switch_name, CALL_TYPE_COIL: 1234, CONF_SLAVE: 1},
]
CONF_NAME: switch_name,
CALL_TYPE_COIL: 1234,
CONF_SLAVE: 1,
},
switch_name,
SWITCH_DOMAIN,
scan_interval,
)
await run_base_read_test(
entity_id,
hass,
mock_hub,
CALL_TYPE_COIL,
None,
CONF_COILS,
regs,
expected,
now + timedelta(seconds=scan_interval + 1),
method_discovery=False,
scan_interval=5,
)
assert state == expected
@pytest.mark.parametrize(
@@ -101,38 +128,28 @@ async def test_coil_switch(hass, mock_hub, regs, expected):
),
],
)
async def test_register_switch(hass, mock_hub, regs, expected):
async def test_register_switch(hass, regs, expected):
"""Run test for given config."""
switch_name = "modbus_test_switch"
scan_interval = 5
entity_id, now, device = await setup_base_test(
switch_name,
state = await base_test(
hass,
mock_hub,
{
CONF_REGISTERS: [
{
CONF_NAME: switch_name,
CONF_REGISTER: 1234,
CONF_SLAVE: 1,
CONF_COMMAND_OFF: 0x00,
CONF_COMMAND_ON: 0x01,
},
]
CONF_NAME: switch_name,
CONF_REGISTER: 1234,
CONF_SLAVE: 1,
CONF_COMMAND_OFF: 0x00,
CONF_COMMAND_ON: 0x01,
},
switch_name,
SWITCH_DOMAIN,
scan_interval,
)
await run_base_read_test(
entity_id,
hass,
mock_hub,
CALL_TYPE_REGISTER_HOLDING,
None,
CONF_REGISTERS,
regs,
expected,
now + timedelta(seconds=scan_interval + 1),
method_discovery=False,
scan_interval=5,
)
assert state == expected
@pytest.mark.parametrize(
@@ -152,35 +169,25 @@ async def test_register_switch(hass, mock_hub, regs, expected):
),
],
)
async def test_register_state_switch(hass, mock_hub, regs, expected):
async def test_register_state_switch(hass, regs, expected):
"""Run test for given config."""
switch_name = "modbus_test_switch"
scan_interval = 5
entity_id, now, device = await setup_base_test(
switch_name,
state = await base_test(
hass,
mock_hub,
{
CONF_REGISTERS: [
{
CONF_NAME: switch_name,
CONF_REGISTER: 1234,
CONF_SLAVE: 1,
CONF_COMMAND_OFF: 0x04,
CONF_COMMAND_ON: 0x40,
},
]
CONF_NAME: switch_name,
CONF_REGISTER: 1234,
CONF_SLAVE: 1,
CONF_COMMAND_OFF: 0x04,
CONF_COMMAND_ON: 0x40,
},
switch_name,
SWITCH_DOMAIN,
scan_interval,
)
await run_base_read_test(
entity_id,
hass,
mock_hub,
CALL_TYPE_REGISTER_HOLDING,
None,
CONF_REGISTERS,
regs,
expected,
now + timedelta(seconds=scan_interval + 1),
method_discovery=False,
scan_interval=5,
)
assert state == expected