From 4ca339c5b1b5315c855ccd79385d812d9179894f Mon Sep 17 00:00:00 2001 From: jan iversen Date: Sun, 20 Feb 2022 11:56:38 +0100 Subject: [PATCH] Set slave default to 0, as already documented in Modbus (#66921) --- homeassistant/components/modbus/__init__.py | 2 +- homeassistant/components/modbus/validators.py | 3 +-- tests/components/modbus/conftest.py | 9 ++++++--- tests/components/modbus/test_init.py | 10 ++++++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index 56edf39311c..4d33e819a8f 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -118,7 +118,7 @@ BASE_COMPONENT_SCHEMA = vol.Schema( { vol.Required(CONF_NAME): cv.string, vol.Required(CONF_ADDRESS): cv.positive_int, - vol.Optional(CONF_SLAVE): cv.positive_int, + vol.Optional(CONF_SLAVE, default=0): cv.positive_int, vol.Optional( CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL ): cv.positive_int, diff --git a/homeassistant/components/modbus/validators.py b/homeassistant/components/modbus/validators.py index 74cd2a49861..4f910043d12 100644 --- a/homeassistant/components/modbus/validators.py +++ b/homeassistant/components/modbus/validators.py @@ -209,8 +209,7 @@ def duplicate_entity_validator(config: dict) -> dict: addr += "_" + str(entry[CONF_COMMAND_ON]) if CONF_COMMAND_OFF in entry: addr += "_" + str(entry[CONF_COMMAND_OFF]) - if CONF_SLAVE in entry: - addr += "_" + str(entry[CONF_SLAVE]) + addr += "_" + str(entry[CONF_SLAVE]) if addr in addresses: err = f"Modbus {component}/{name} address {addr} is duplicate, second entry not loaded!" _LOGGER.warning(err) diff --git a/tests/components/modbus/conftest.py b/tests/components/modbus/conftest.py index 91327b4e2a2..75d4d6099c9 100644 --- a/tests/components/modbus/conftest.py +++ b/tests/components/modbus/conftest.py @@ -9,7 +9,7 @@ from pymodbus.exceptions import ModbusException import pytest from homeassistant.components.modbus.const import MODBUS_DOMAIN as DOMAIN, TCP -from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_TYPE +from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_SLAVE, CONF_TYPE from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -82,9 +82,12 @@ async def mock_modbus_fixture( ): """Load integration modbus using mocked pymodbus.""" conf = copy.deepcopy(do_config) - if config_addon: - for key in conf.keys(): + for key in conf.keys(): + if config_addon: conf[key][0].update(config_addon) + for entity in conf[key]: + if CONF_SLAVE not in entity: + entity[CONF_SLAVE] = 0 caplog.set_level(logging.WARNING) config = { DOMAIN: [ diff --git a/tests/components/modbus/test_init.py b/tests/components/modbus/test_init.py index 7d9ab3e3471..c9beba694e8 100644 --- a/tests/components/modbus/test_init.py +++ b/tests/components/modbus/test_init.py @@ -76,6 +76,7 @@ from homeassistant.const import ( CONF_PORT, CONF_SCAN_INTERVAL, CONF_SENSORS, + CONF_SLAVE, CONF_STRUCTURE, CONF_TIMEOUT, CONF_TYPE, @@ -272,10 +273,12 @@ async def test_duplicate_modbus_validator(do_config): { CONF_NAME: TEST_ENTITY_NAME, CONF_ADDRESS: 117, + CONF_SLAVE: 0, }, { CONF_NAME: TEST_ENTITY_NAME, CONF_ADDRESS: 119, + CONF_SLAVE: 0, }, ], } @@ -290,10 +293,12 @@ async def test_duplicate_modbus_validator(do_config): { CONF_NAME: TEST_ENTITY_NAME, CONF_ADDRESS: 117, + CONF_SLAVE: 0, }, { CONF_NAME: TEST_ENTITY_NAME + "2", CONF_ADDRESS: 117, + CONF_SLAVE: 0, }, ], } @@ -409,6 +414,7 @@ async def test_duplicate_entity_validator(do_config): { CONF_NAME: TEST_ENTITY_NAME, CONF_ADDRESS: 117, + CONF_SLAVE: 0, CONF_SCAN_INTERVAL: 0, } ], @@ -544,6 +550,7 @@ async def mock_modbus_read_pymodbus_fixture( CONF_INPUT_TYPE: do_type, CONF_NAME: TEST_ENTITY_NAME, CONF_ADDRESS: 51, + CONF_SLAVE: 0, CONF_SCAN_INTERVAL: do_scan_interval, } ], @@ -688,6 +695,7 @@ async def test_delay(hass, mock_pymodbus): CONF_INPUT_TYPE: CALL_TYPE_COIL, CONF_NAME: TEST_ENTITY_NAME, CONF_ADDRESS: 52, + CONF_SLAVE: 0, CONF_SCAN_INTERVAL: set_scan_interval, }, ], @@ -736,6 +744,7 @@ async def test_delay(hass, mock_pymodbus): { CONF_NAME: TEST_ENTITY_NAME, CONF_ADDRESS: 117, + CONF_SLAVE: 0, CONF_SCAN_INTERVAL: 0, } ], @@ -759,6 +768,7 @@ async def test_shutdown(hass, caplog, mock_pymodbus, mock_modbus_with_pymodbus): { CONF_NAME: TEST_ENTITY_NAME, CONF_ADDRESS: 51, + CONF_SLAVE: 0, } ] },