diff --git a/homeassistant/components/modbus/validators.py b/homeassistant/components/modbus/validators.py index 202425bca48..650083dc7e4 100644 --- a/homeassistant/components/modbus/validators.py +++ b/homeassistant/components/modbus/validators.py @@ -301,13 +301,14 @@ def check_config(config: dict) -> dict: def validate_entity( hub_name: str, + component: str, entity: dict, minimum_scan_interval: int, ent_names: set, ent_addr: set, ) -> bool: """Validate entity.""" - name = entity[CONF_NAME] + name = f"{component}.{entity[CONF_NAME]}" addr = f"{hub_name}{entity[CONF_ADDRESS]}" scan_interval = entity.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) if 0 < scan_interval < 5: @@ -370,7 +371,7 @@ def check_config(config: dict) -> dict: continue minimum_scan_interval = 9999 no_entities = True - for _component, conf_key in PLATFORMS: + for component, conf_key in PLATFORMS: if conf_key not in hub: continue no_entities = False @@ -379,6 +380,7 @@ def check_config(config: dict) -> dict: while entity_inx < len(entities): if not validate_entity( hub[CONF_NAME], + component, entities[entity_inx], minimum_scan_interval, ent_names, diff --git a/tests/components/modbus/test_init.py b/tests/components/modbus/test_init.py index 4de9a439a01..bd590a9e15c 100644 --- a/tests/components/modbus/test_init.py +++ b/tests/components/modbus/test_init.py @@ -858,6 +858,30 @@ async def test_duplicate_fan_mode_validator(do_config) -> None: ], 2, ), + ( + [ + { + CONF_NAME: TEST_MODBUS_NAME, + CONF_TYPE: TCP, + CONF_HOST: TEST_MODBUS_HOST, + CONF_PORT: TEST_PORT_TCP, + CONF_TIMEOUT: 3, + CONF_SENSORS: [ + { + CONF_NAME: TEST_ENTITY_NAME, + CONF_ADDRESS: 117, + CONF_SLAVE: 0, + }, + { + CONF_NAME: TEST_ENTITY_NAME, + CONF_ADDRESS: 1179, + CONF_SLAVE: 0, + }, + ], + }, + ], + 1, + ), ], ) async def test_duplicate_addresses(do_config, sensor_cnt) -> None: @@ -867,6 +891,41 @@ async def test_duplicate_addresses(do_config, sensor_cnt) -> None: assert len(do_config[use_inx][CONF_SENSORS]) == sensor_cnt +@pytest.mark.parametrize( + "do_config", + [ + [ + { + CONF_NAME: TEST_MODBUS_NAME, + CONF_TYPE: TCP, + CONF_HOST: TEST_MODBUS_HOST, + CONF_PORT: TEST_PORT_TCP, + CONF_TIMEOUT: 3, + CONF_SENSORS: [ + { + CONF_NAME: TEST_ENTITY_NAME, + CONF_ADDRESS: 117, + CONF_SLAVE: 0, + }, + ], + CONF_BINARY_SENSORS: [ + { + CONF_NAME: TEST_ENTITY_NAME + "1", + CONF_ADDRESS: 1179, + CONF_SLAVE: 0, + }, + ], + }, + ], + ], +) +async def test_no_duplicate_names(do_config) -> None: + """Test duplicate entity validator.""" + check_config(do_config) + assert len(do_config[0][CONF_SENSORS]) == 1 + assert len(do_config[0][CONF_BINARY_SENSORS]) == 1 + + @pytest.mark.parametrize( "do_config", [