[ld6002b] Reject a setting configured twice on one hub

This commit is contained in:
J. Nick Koston
2026-10-16 13:19:16 -10:00
parent a8572e6885
commit 2c659096de
4 changed files with 40 additions and 5 deletions
+21 -2
View File
@@ -2,10 +2,11 @@ from esphome import pins
import esphome.codegen as cg
from esphome.components import uart
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_WAKEUP_PIN
from esphome.const import CONF_ID, CONF_PLATFORM, CONF_WAKEUP_PIN
import esphome.final_validate as fv
from esphome.types import ConfigType
from .const import CONF_AUTO_WAKE, CONF_WAKEUP_PULSE
from .const import CONF_AUTO_WAKE, CONF_LD6002B_ID, CONF_WAKEUP_PULSE
CODEOWNERS = ["@hepter"]
DEPENDENCIES = ["uart"]
@@ -73,3 +74,21 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_wakeup_pulse_ms(config[CONF_WAKEUP_PULSE].total_milliseconds))
cg.add(var.set_auto_wake(config[CONF_AUTO_WAKE]))
def validate_unique_settings(domain: str, config: ConfigType) -> None:
"""Reject a setting configured in two platform blocks of the same hub; the hub keeps one entity per setting."""
hub_id = config[CONF_LD6002B_ID]
entries = [
entry
for entry in fv.full_config.get().get(domain, [])
if entry.get(CONF_PLATFORM) == DOMAIN and entry.get(CONF_LD6002B_ID) == hub_id
]
for key in config:
if key in (CONF_PLATFORM, CONF_LD6002B_ID):
continue
if sum(key in entry for entry in entries) > 1:
raise cv.Invalid(
f"{key} is already set for this ld6002b in another {domain} block",
path=[key],
)
@@ -1,6 +1,7 @@
import esphome.codegen as cg
from esphome.components import number
from esphome.components.button import DOMAIN as BUTTON_DOMAIN
from esphome.components.number import DOMAIN as NUMBER_DOMAIN
from esphome.components.select import DOMAIN as SELECT_DOMAIN
import esphome.config_validation as cv
from esphome.const import (
@@ -15,7 +16,7 @@ from esphome.const import (
import esphome.final_validate as fv
from esphome.types import ConfigType
from .. import LD6002BComponent
from .. import LD6002BComponent, validate_unique_settings
from ..const import (
CONF_APPLY_AREA,
CONF_AREA_CONFIG,
@@ -104,6 +105,7 @@ CONFIG_SCHEMA = cv.Schema(
def final_validate(config: ConfigType) -> None:
validate_unique_settings(NUMBER_DOMAIN, config)
if config.get(CONF_AREA_CONFIG) is None:
return
@@ -4,7 +4,7 @@ import esphome.config_validation as cv
from esphome.const import CONF_AREA_ID, CONF_SENSITIVITY, ENTITY_CATEGORY_CONFIG
from esphome.types import ConfigType
from .. import LD6002BComponent
from .. import LD6002BComponent, validate_unique_settings
from ..const import CONF_INSTALLATION_MODE, CONF_LD6002B_ID, CONF_TRIGGER_SPEED
DEPENDENCIES = ["ld6002b"]
@@ -39,6 +39,13 @@ CONFIG_SCHEMA = cv.Schema(
)
def final_validate(config: ConfigType) -> None:
validate_unique_settings(select.DOMAIN, config)
FINAL_VALIDATE_SCHEMA = final_validate
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_LD6002B_ID])
control = {"apply": "set_select_value(entity, index)", "publish": "before"}
@@ -4,7 +4,7 @@ import esphome.config_validation as cv
from esphome.const import DEVICE_CLASS_SWITCH, ENTITY_CATEGORY_CONFIG
from esphome.types import ConfigType
from .. import LD6002BComponent
from .. import LD6002BComponent, validate_unique_settings
from ..const import (
CONF_LD6002B_ID,
CONF_LOW_POWER,
@@ -44,6 +44,13 @@ CONFIG_SCHEMA = cv.Schema(
)
def final_validate(config: ConfigType) -> None:
validate_unique_settings(switch.DOMAIN, config)
FINAL_VALIDATE_SCHEMA = final_validate
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_LD6002B_ID])
control = {"apply": "set_switch_state(entity, state)", "publish": "after"}