From ff626b428f28042cebccfee2e3162e527f520bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Vikstr=C3=B6m?= Date: Tue, 6 May 2025 10:41:46 +0200 Subject: [PATCH] Attempt moving it to esphome config section --- esphome/components/devices/__init__.py | 26 ------------------- esphome/const.py | 1 + esphome/core/config.py | 22 +++++++++++++++- .../devices/devices.h => core/sub_device.h} | 2 -- tests/components/device/common.yaml | 11 -------- tests/components/esphome/common.yaml | 8 ++++++ 6 files changed, 30 insertions(+), 40 deletions(-) delete mode 100644 esphome/components/devices/__init__.py rename esphome/{components/devices/devices.h => core/sub_device.h} (92%) delete mode 100644 tests/components/device/common.yaml diff --git a/esphome/components/devices/__init__.py b/esphome/components/devices/__init__.py deleted file mode 100644 index 5365b8ba3e..0000000000 --- a/esphome/components/devices/__init__.py +++ /dev/null @@ -1,26 +0,0 @@ -from esphome import codegen as cg, config_validation as cv -from esphome.const import CONF_AREA, CONF_ID, CONF_NAME - -devices_ns = cg.esphome_ns.namespace("devices") -SubDevice = devices_ns.class_("SubDevice") - -MULTI_CONF = True - -CODEOWNERS = ["@dala318"] - -CONFIG_SCHEMA = cv.Schema( - { - cv.GenerateID(CONF_ID): cv.declare_id(SubDevice), - cv.Required(CONF_NAME): cv.string, - cv.Optional(CONF_AREA, default=""): cv.string, - } -).extend(cv.COMPONENT_SCHEMA) - - -async def to_code(config): - dev = cg.new_Pvariable(config[CONF_ID]) - cg.add(dev.set_uid(hash(str(config[CONF_ID])) % 0xFFFFFFFF)) - cg.add(dev.set_name(config[CONF_NAME])) - cg.add(dev.set_area(config[CONF_AREA])) - cg.add(cg.App.register_sub_device(dev)) - cg.add_define("USE_SUB_DEVICE") diff --git a/esphome/const.py b/esphome/const.py index 22320e824b..03e4010300 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -835,6 +835,7 @@ CONF_STEP_PIN = "step_pin" CONF_STOP = "stop" CONF_STOP_ACTION = "stop_action" CONF_STORE_BASELINE = "store_baseline" +CONF_SUB_DEVICES = "sub_devices" CONF_SUBNET = "subnet" CONF_SUBSCRIBE_QOS = "subscribe_qos" CONF_SUBSTITUTIONS = "substitutions" diff --git a/esphome/core/config.py b/esphome/core/config.py index 72e9f6a65c..f3d8b7e715 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -13,6 +13,7 @@ from esphome.const import ( CONF_DEBUG_SCHEDULER, CONF_ESPHOME, CONF_FRIENDLY_NAME, + CONF_ID, CONF_INCLUDES, CONF_LIBRARIES, CONF_MIN_VERSION, @@ -26,6 +27,7 @@ from esphome.const import ( CONF_PLATFORMIO_OPTIONS, CONF_PRIORITY, CONF_PROJECT, + CONF_SUB_DEVICES, CONF_TRIGGER_ID, CONF_VERSION, KEY_CORE, @@ -48,7 +50,7 @@ LoopTrigger = cg.esphome_ns.class_( ProjectUpdateTrigger = cg.esphome_ns.class_( "ProjectUpdateTrigger", cg.Component, automation.Trigger.template(cg.std_string) ) - +SubDevice = cg.esphome_ns.class_("SubDevice") VALID_INCLUDE_EXTS = {".h", ".hpp", ".tcc", ".ino", ".cpp", ".c"} @@ -167,6 +169,15 @@ CONFIG_SCHEMA = cv.All( cv.Optional( CONF_COMPILE_PROCESS_LIMIT, default=_compile_process_limit_default ): cv.int_range(min=1, max=get_usable_cpu_count()), + cv.Optional(CONF_SUB_DEVICES, default=[]): cv.ensure_list( + cv.Schema( + { + cv.GenerateID(CONF_ID): cv.declare_id(SubDevice), + cv.Required(CONF_NAME): cv.string, + cv.Optional(CONF_AREA, default=""): cv.string, + } + ), + ), } ), validate_hostname, @@ -405,3 +416,12 @@ async def to_code(config): if config[CONF_PLATFORMIO_OPTIONS]: CORE.add_job(_add_platformio_options, config[CONF_PLATFORMIO_OPTIONS]) + + if config[CONF_SUB_DEVICES]: + for dev_conf in config[CONF_SUB_DEVICES]: + dev = cg.new_Pvariable(dev_conf[CONF_ID]) + cg.add(dev.set_uid(hash(str(dev_conf[CONF_ID])) % 0xFFFFFFFF)) + cg.add(dev.set_name(dev_conf[CONF_NAME])) + cg.add(dev.set_area(dev_conf[CONF_AREA])) + cg.add(cg.App.register_sub_device(dev)) + cg.add_define("USE_SUB_DEVICE") diff --git a/esphome/components/devices/devices.h b/esphome/core/sub_device.h similarity index 92% rename from esphome/components/devices/devices.h rename to esphome/core/sub_device.h index 06f9309360..9e7c4d2261 100644 --- a/esphome/components/devices/devices.h +++ b/esphome/core/sub_device.h @@ -3,7 +3,6 @@ #include "esphome/core/string_ref.h" namespace esphome { -namespace devices { class SubDevice { public: @@ -20,5 +19,4 @@ class SubDevice { std::string area_ = ""; }; -} // namespace devices } // namespace esphome diff --git a/tests/components/device/common.yaml b/tests/components/device/common.yaml deleted file mode 100644 index 232bb631c9..0000000000 --- a/tests/components/device/common.yaml +++ /dev/null @@ -1,11 +0,0 @@ -devices: - - id: other_device - name: Another device - -binary_sensor: - - platform: template - name: Basic sensor - - - platform: template - name: Other device sensor - device_id: other_device diff --git a/tests/components/esphome/common.yaml b/tests/components/esphome/common.yaml index 05954e37d7..3754390e89 100644 --- a/tests/components/esphome/common.yaml +++ b/tests/components/esphome/common.yaml @@ -17,4 +17,12 @@ esphome: version: "1.1" on_update: logger.log: on_update + sub_devices: + - id: other_device + name: Another device + area: Another area +binary_sensor: + - platform: template + name: Other device sensor + device_id: other_device