mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Allow discovery configuration of modbus platforms (#46591)
* Change modbus configuration to new style. The old (frozen) configuration is still supported, but when detected a big warning is issued that it will soon be removed. This allows users to change their configuration at their pace. Clean configuration SCHEMAs and move common modbus parts to MODBUS_SCHEMA (renamed from BASE_SCHEMA). Add BASE_COMPONENT_SCHEMA to ensure common configuration of components. All component define e.g. NAME, move these to a common schema. change components (binary_sensor, sensor, switch) to new config Add test set for modbus itself (old config and discovery_info). Add test of devices discovery_info configuration * Update discovery_info configuration for binary_sensor. * Update discovery_info configuration for sensor. * Update discovery_info configuration for switch. * Review comments. * update due to change in core * flake8 problem. * Correct log message. * add should_poll property. * Fix polling for Modbus binary sensor * Fix polling for Modbus sensor * Fix polling for Modbus switch * Fix switch. * Fix pytest errors. * Update homeassistant/components/modbus/binary_sensor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/modbus/binary_sensor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/modbus/modbus.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/modbus/sensor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/modbus/sensor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/modbus/sensor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/modbus/switch.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/modbus/switch.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/modbus/switch.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * ToogleEntity -> SwitchEntity and add abastract * Update homeassistant/components/modbus/switch.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/modbus/test_init.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * removed if/else in test. * Remove other if. Co-authored-by: Vladimir Zahradnik <vladimir@zahradnik.io> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
@@ -3,14 +3,25 @@ import pytest
|
||||
|
||||
from homeassistant.components.modbus.const import (
|
||||
CALL_TYPE_COIL,
|
||||
CALL_TYPE_REGISTER_HOLDING,
|
||||
CALL_TYPE_REGISTER_INPUT,
|
||||
CONF_COILS,
|
||||
CONF_INPUT_TYPE,
|
||||
CONF_REGISTER,
|
||||
CONF_REGISTER_TYPE,
|
||||
CONF_REGISTERS,
|
||||
CONF_STATE_OFF,
|
||||
CONF_STATE_ON,
|
||||
CONF_SWITCHES,
|
||||
CONF_VERIFY_REGISTER,
|
||||
CONF_VERIFY_STATE,
|
||||
)
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.const import (
|
||||
CONF_ADDRESS,
|
||||
CONF_COMMAND_OFF,
|
||||
CONF_COMMAND_ON,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_NAME,
|
||||
CONF_SLAVE,
|
||||
STATE_OFF,
|
||||
@@ -20,39 +31,66 @@ 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_COIL, CONF_REGISTER])
|
||||
async def test_config_switch(hass, do_options, read_type):
|
||||
@pytest.mark.parametrize(
|
||||
"read_type", [CALL_TYPE_REGISTER_HOLDING, CALL_TYPE_REGISTER_INPUT, CALL_TYPE_COIL]
|
||||
)
|
||||
async def test_config_switch(hass, do_discovery, 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
|
||||
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:
|
||||
device_config = {
|
||||
CONF_NAME: device_name,
|
||||
read_type: 1234,
|
||||
CONF_SLAVE: 10,
|
||||
}
|
||||
array_type = CONF_COILS
|
||||
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.update({})
|
||||
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,
|
||||
device_config,
|
||||
device_name,
|
||||
SWITCH_DOMAIN,
|
||||
None,
|
||||
CONF_SWITCHES,
|
||||
array_type,
|
||||
method_discovery=False,
|
||||
method_discovery=do_discovery,
|
||||
)
|
||||
|
||||
|
||||
@@ -88,16 +126,16 @@ async def test_coil_switch(hass, regs, expected):
|
||||
hass,
|
||||
{
|
||||
CONF_NAME: switch_name,
|
||||
CALL_TYPE_COIL: 1234,
|
||||
CONF_SLAVE: 1,
|
||||
CONF_ADDRESS: 1234,
|
||||
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
||||
},
|
||||
switch_name,
|
||||
SWITCH_DOMAIN,
|
||||
None,
|
||||
CONF_SWITCHES,
|
||||
CONF_COILS,
|
||||
regs,
|
||||
expected,
|
||||
method_discovery=False,
|
||||
method_discovery=True,
|
||||
scan_interval=5,
|
||||
)
|
||||
assert state == expected
|
||||
@@ -142,7 +180,7 @@ async def test_register_switch(hass, regs, expected):
|
||||
},
|
||||
switch_name,
|
||||
SWITCH_DOMAIN,
|
||||
None,
|
||||
CONF_SWITCHES,
|
||||
CONF_REGISTERS,
|
||||
regs,
|
||||
expected,
|
||||
@@ -183,7 +221,7 @@ async def test_register_state_switch(hass, regs, expected):
|
||||
},
|
||||
switch_name,
|
||||
SWITCH_DOMAIN,
|
||||
None,
|
||||
CONF_SWITCHES,
|
||||
CONF_REGISTERS,
|
||||
regs,
|
||||
expected,
|
||||
|
||||
Reference in New Issue
Block a user