create issues for modbus config errors (#113431)

This commit is contained in:
jan iversen 2024-03-15 13:44:29 +01:00 committed by GitHub
parent 4181c62ec0
commit 1c938f6422
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 84 additions and 19 deletions

View File

@ -77,6 +77,22 @@
"deprecated_retries": { "deprecated_retries": {
"title": "`{config_key}` configuration key is being removed", "title": "`{config_key}` configuration key is being removed",
"description": "Please remove the `{config_key}` key from the {integration} entry in your configuration.yaml file and restart Home Assistant to fix this issue.\n\nThe maximum number of retries is now fixed to 3." "description": "Please remove the `{config_key}` key from the {integration} entry in your configuration.yaml file and restart Home Assistant to fix this issue.\n\nThe maximum number of retries is now fixed to 3."
},
"missing_modbus_name": {
"title": "Modbus entry with host `{sub_2}` missing name",
"description": "Please add `{sub_1}` key to the {integration} entry with host `{sub_2}` in your configuration.yaml file and restart Home Assistant to fix this issue\n\n. `{sub_1}: {sub_3}` have been added."
},
"duplicate_modbus_entry": {
"title": "Modbus {sub_2} host/port {sub_1} is duplicate, second entry not loaded.",
"description": "Please update {sub_2} and/or {sub_1} for the entry in your configuration.yaml file and restart Home Assistant to fix this issue."
},
"duplicate_entity_entry": {
"title": "Modbus {sub_1} address {sub_2} is duplicate, second entry not loaded.",
"description": "An address can only be associated with on entity, Please correct the entry in your configuration.yaml file and restart Home Assistant to fix this issue."
},
"duplicate_entity_name": {
"title": "Modbus {sub_1} is duplicate, second entry not loaded.",
"description": "A entity name must be unique, Please correct the entry in your configuration.yaml file and restart Home Assistant to fix this issue."
} }
} }
} }

View File

@ -25,6 +25,7 @@ from homeassistant.const import (
CONF_TYPE, CONF_TYPE,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from .const import ( from .const import (
CONF_DATA_TYPE, CONF_DATA_TYPE,
@ -44,6 +45,7 @@ from .const import (
CONF_WRITE_TYPE, CONF_WRITE_TYPE,
DEFAULT_HUB, DEFAULT_HUB,
DEFAULT_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL,
MODBUS_DOMAIN as DOMAIN,
PLATFORMS, PLATFORMS,
SERIAL, SERIAL,
DataType, DataType,
@ -112,6 +114,29 @@ DEFAULT_STRUCT_FORMAT = {
} }
def modbus_create_issue(
hass: HomeAssistant, key: str, subs: list[str], err: str
) -> None:
"""Create issue modbus style."""
async_create_issue(
hass,
DOMAIN,
key,
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key=key,
translation_placeholders={
"sub_1": subs[0],
"sub_2": subs[1],
"sub_3": subs[2],
"integration": DOMAIN,
},
issue_domain=DOMAIN,
learn_more_url="https://www.home-assistant.io/integrations/modbus",
)
_LOGGER.warning(err)
def struct_validator(config: dict[str, Any]) -> dict[str, Any]: def struct_validator(config: dict[str, Any]) -> dict[str, Any]:
"""Sensor schema validator.""" """Sensor schema validator."""
@ -289,12 +314,28 @@ def validate_modbus(
DEFAULT_HUB if not hub_name_inx else f"{DEFAULT_HUB}_{hub_name_inx}" DEFAULT_HUB if not hub_name_inx else f"{DEFAULT_HUB}_{hub_name_inx}"
) )
hub_name_inx += 1 hub_name_inx += 1
err = f"Modbus host/port {host} is missing name, added {hub[CONF_NAME]}!" modbus_create_issue(
_LOGGER.warning(err) hass,
"missing_modbus_name",
[
"name",
host,
hub[CONF_NAME],
],
f"Modbus host/port {host} is missing name, added {hub[CONF_NAME]}!",
)
name = hub[CONF_NAME] name = hub[CONF_NAME]
if host in hosts or name in hub_names: if host in hosts or name in hub_names:
err = f"Modbus {name} host/port {host} is duplicate, not loaded!" modbus_create_issue(
_LOGGER.warning(err) hass,
"duplicate_modbus_entry",
[
host,
hub[CONF_NAME],
"",
],
f"Modbus {name} host/port {host} is duplicate, not loaded!",
)
return False return False
hosts.add(host) hosts.add(host)
hub_names.add(name) hub_names.add(name)
@ -315,15 +356,11 @@ def validate_entity(
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:
_LOGGER.warning( err = (
( f"{hub_name} {name} scan_interval is lower than 5 seconds, "
"%s %s scan_interval(%d) is lower than 5 seconds, " "which may cause Home Assistant stability issues"
"which may cause Home Assistant stability issues"
),
hub_name,
name,
scan_interval,
) )
_LOGGER.warning(err)
entity[CONF_SCAN_INTERVAL] = scan_interval entity[CONF_SCAN_INTERVAL] = scan_interval
minimum_scan_interval = min(scan_interval, minimum_scan_interval) minimum_scan_interval = min(scan_interval, minimum_scan_interval)
for conf_type in ( for conf_type in (
@ -337,7 +374,6 @@ def validate_entity(
inx = entity.get(CONF_SLAVE) or entity.get(CONF_DEVICE_ADDRESS, 0) inx = entity.get(CONF_SLAVE) or entity.get(CONF_DEVICE_ADDRESS, 0)
addr += f"_{inx}" addr += f"_{inx}"
loc_addr: set[str] = {addr} loc_addr: set[str] = {addr}
if CONF_TARGET_TEMP in entity: if CONF_TARGET_TEMP in entity:
loc_addr.add(f"{hub_name}{entity[CONF_TARGET_TEMP]}_{inx}") loc_addr.add(f"{hub_name}{entity[CONF_TARGET_TEMP]}_{inx}")
if CONF_HVAC_MODE_REGISTER in entity: if CONF_HVAC_MODE_REGISTER in entity:
@ -348,15 +384,28 @@ def validate_entity(
dup_addrs = ent_addr.intersection(loc_addr) dup_addrs = ent_addr.intersection(loc_addr)
if len(dup_addrs) > 0: if len(dup_addrs) > 0:
for addr in dup_addrs: for addr in dup_addrs:
err = ( modbus_create_issue(
f"Modbus {hub_name}/{name} address {addr} is duplicate, second" hass,
" entry not loaded!" "duplicate_entity_entry",
[
f"{hub_name}/{name}",
addr,
"",
],
f"Modbus {hub_name}/{name} address {addr} is duplicate, second entry not loaded!",
) )
_LOGGER.warning(err)
return False return False
if name in ent_names: if name in ent_names:
err = f"Modbus {hub_name}/{name} is duplicate, second entry not loaded!" modbus_create_issue(
_LOGGER.warning(err) hass,
"duplicate_entity_name",
[
f"{hub_name}/{name}",
"",
"",
],
f"Modbus {hub_name}/{name} is duplicate, second entry not loaded!",
)
return False return False
ent_names.add(name) ent_names.add(name)
ent_addr.update(loc_addr) ent_addr.update(loc_addr)