Allow duplicate names in different modbus entities (#112701)

Allow duplicate names in different entities.
pull/113250/head
jan iversen 2024-03-08 15:10:35 +01:00 committed by Franck Nijhof
parent c2543289b7
commit f7b64244b8
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 63 additions and 2 deletions

View File

@ -301,13 +301,14 @@ def check_config(config: dict) -> dict:
def validate_entity( def validate_entity(
hub_name: str, hub_name: str,
component: str,
entity: dict, entity: dict,
minimum_scan_interval: int, minimum_scan_interval: int,
ent_names: set, ent_names: set,
ent_addr: set, ent_addr: set,
) -> bool: ) -> bool:
"""Validate entity.""" """Validate entity."""
name = entity[CONF_NAME] name = f"{component}.{entity[CONF_NAME]}"
addr = f"{hub_name}{entity[CONF_ADDRESS]}" addr = f"{hub_name}{entity[CONF_ADDRESS]}"
scan_interval = entity.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) scan_interval = entity.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
if 0 < scan_interval < 5: if 0 < scan_interval < 5:
@ -370,7 +371,7 @@ def check_config(config: dict) -> dict:
continue continue
minimum_scan_interval = 9999 minimum_scan_interval = 9999
no_entities = True no_entities = True
for _component, conf_key in PLATFORMS: for component, conf_key in PLATFORMS:
if conf_key not in hub: if conf_key not in hub:
continue continue
no_entities = False no_entities = False
@ -379,6 +380,7 @@ def check_config(config: dict) -> dict:
while entity_inx < len(entities): while entity_inx < len(entities):
if not validate_entity( if not validate_entity(
hub[CONF_NAME], hub[CONF_NAME],
component,
entities[entity_inx], entities[entity_inx],
minimum_scan_interval, minimum_scan_interval,
ent_names, ent_names,

View File

@ -858,6 +858,30 @@ async def test_duplicate_fan_mode_validator(do_config) -> None:
], ],
2, 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: 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 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( @pytest.mark.parametrize(
"do_config", "do_config",
[ [