Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions homeassistant/components/modbus/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,14 @@ def validate_modbus(hub: dict, hub_name_inx: int) -> bool:

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:
Expand Down Expand Up @@ -368,7 +369,7 @@ def validate_entity(
if not validate_modbus(hub, hub_name_inx):
del config[hub_inx]
continue
for _component, conf_key in PLATFORMS:
for component, conf_key in PLATFORMS:
if conf_key not in hub:
continue
entity_inx = 0
Expand All @@ -377,6 +378,7 @@ def validate_entity(
while entity_inx < len(entities):
if not validate_entity(
hub[CONF_NAME],
component,
entities[entity_inx],
minimum_scan_interval,
ent_names,
Expand Down
59 changes: 59 additions & 0 deletions tests/components/modbus/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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",
[
Expand Down