From 2c659096de9319e700cc225ef6c73f9659a846e2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 16 Oct 2026 13:19:16 -1000 Subject: [PATCH] [ld6002b] Reject a setting configured twice on one hub --- esphome/components/ld6002b/__init__.py | 23 +++++++++++++++++-- esphome/components/ld6002b/number/__init__.py | 4 +++- esphome/components/ld6002b/select/__init__.py | 9 +++++++- esphome/components/ld6002b/switch/__init__.py | 9 +++++++- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/esphome/components/ld6002b/__init__.py b/esphome/components/ld6002b/__init__.py index c7529f93149..8ba7bbb2da1 100644 --- a/esphome/components/ld6002b/__init__.py +++ b/esphome/components/ld6002b/__init__.py @@ -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], + ) diff --git a/esphome/components/ld6002b/number/__init__.py b/esphome/components/ld6002b/number/__init__.py index 4fcbb669251..dd38e4519d9 100644 --- a/esphome/components/ld6002b/number/__init__.py +++ b/esphome/components/ld6002b/number/__init__.py @@ -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 diff --git a/esphome/components/ld6002b/select/__init__.py b/esphome/components/ld6002b/select/__init__.py index 835d301e4cf..27c2e065428 100644 --- a/esphome/components/ld6002b/select/__init__.py +++ b/esphome/components/ld6002b/select/__init__.py @@ -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"} diff --git a/esphome/components/ld6002b/switch/__init__.py b/esphome/components/ld6002b/switch/__init__.py index 247cecb16d5..59477dc5973 100644 --- a/esphome/components/ld6002b/switch/__init__.py +++ b/esphome/components/ld6002b/switch/__init__.py @@ -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"}