From 962e0c4c336be1869a6d5960074fadb5122e59c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Vikstr=C3=B6m?= Date: Wed, 9 Apr 2025 19:09:31 +0200 Subject: [PATCH] Make it a Class but only use the id in entities --- esphome/components/devices/__init__.py | 23 ++++++----------------- esphome/components/devices/devices.h | 6 +++++- esphome/config_validation.py | 10 +++++----- esphome/core/entity_base.cpp | 4 ++-- esphome/core/entity_base.h | 6 +++--- esphome/cpp_helpers.py | 2 +- 6 files changed, 22 insertions(+), 29 deletions(-) diff --git a/esphome/components/devices/__init__.py b/esphome/components/devices/__init__.py index b38c051259..5a70be82a7 100644 --- a/esphome/components/devices/__init__.py +++ b/esphome/components/devices/__init__.py @@ -1,9 +1,8 @@ from esphome import codegen as cg, config_validation as cv from esphome.const import CONF_AREA, CONF_ID, CONF_NAME -ns = cg.esphome_ns.namespace("devices") -DeviceClass = ns.Class("SubDevice") -StringRef = cg.esphome_ns.struct("StringRef") +devices_ns = cg.esphome_ns.namespace("devices") +SubDevice = devices_ns.class_("SubDevice") MULTI_CONF = True @@ -11,27 +10,17 @@ CODEOWNERS = ["@dala318"] CONFIG_SCHEMA = cv.Schema( { - cv.GenerateID(CONF_ID): cv.declare_id(DeviceClass), - # cv.Required(CONF_NAME): cv.declare_id(StringRef), - # cv.Optional(CONF_AREA, ""): cv.declare_id(StringRef), + cv.GenerateID(CONF_ID): cv.declare_id(SubDevice), cv.Required(CONF_NAME): cv.string, - cv.Optional(CONF_AREA, ""): 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_id(str(config[CONF_ID]))) cg.add(dev.set_name(config[CONF_NAME])) - if CONF_AREA in config: - cg.add(dev.set_area(config[CONF_AREA])) + cg.add(dev.set_area(config[CONF_AREA])) cg.add(cg.App.register_sub_device(dev)) - # cg.add( - # cg.App.register_sub_device( - # config[CONF_ID], - # config[CONF_NAME], - # config[CONF_AREA], - # # config.get(CONF_COMMENT, ""), - # ) - # ) cg.add_define("USE_SUB_DEVICE") diff --git a/esphome/components/devices/devices.h b/esphome/components/devices/devices.h index 96e3b84887..d8bd0d70a3 100644 --- a/esphome/components/devices/devices.h +++ b/esphome/components/devices/devices.h @@ -1,17 +1,21 @@ #pragma once +#include "esphome/core/string_ref.h" + namespace esphome { namespace devices { class SubDevice { public: + void set_id(std::string id) { id_ = std::move(id); } + std::string get_id() { return id_; } void set_name(std::string name) { name_ = std::move(name); } std::string get_name() { return name_; } void set_area(std::string area) { area_ = std::move(area); } std::string get_area() { return area_; } protected: - // std::string id_ = ""; + std::string id_ = ""; std::string name_ = ""; std::string area_ = ""; }; diff --git a/esphome/config_validation.py b/esphome/config_validation.py index 14a64d2277..f883b6fed9 100644 --- a/esphome/config_validation.py +++ b/esphome/config_validation.py @@ -349,9 +349,10 @@ def icon(value): ) -def device_id(value): - StringRef = cg.esphome_ns.struct("StringRef") - validator = use_id(StringRef) +def sub_device_id(value): + devices_ns = cg.esphome_ns.namespace("devices") + SubDevice = devices_ns.class_("SubDevice") + validator = use_id(SubDevice) return validator(value) @@ -1874,8 +1875,7 @@ ENTITY_BASE_SCHEMA = Schema( Optional(CONF_DISABLED_BY_DEFAULT, default=False): boolean, Optional(CONF_ICON): icon, Optional(CONF_ENTITY_CATEGORY): entity_category, - # Optional(CONF_DEVICE_ID): use_id(StringRef), - Optional(CONF_DEVICE_ID): device_id, + Optional(CONF_DEVICE_ID): sub_device_id, } ) diff --git a/esphome/core/entity_base.cpp b/esphome/core/entity_base.cpp index a08cab622a..8073879982 100644 --- a/esphome/core/entity_base.cpp +++ b/esphome/core/entity_base.cpp @@ -36,13 +36,13 @@ std::string EntityBase::get_icon() const { void EntityBase::set_icon(const char *icon) { this->icon_c_str_ = icon; } // Entity Device Name -StringRef EntityBase::get_device_id() const { +const StringRef &EntityBase::get_device_id() const { if (this->device_id_.empty()) { return StringRef(""); } return this->device_id_; } -void EntityBase::set_device_id(const StringRef *device_id) { this->device_id_ = *device_id; } +void EntityBase::set_device_id(const std::string device_id) { this->device_id_ = StringRef(device_id); } // Entity Category EntityCategory EntityBase::get_entity_category() const { return this->entity_category_; } diff --git a/esphome/core/entity_base.h b/esphome/core/entity_base.h index 6975c524f6..e52406c425 100644 --- a/esphome/core/entity_base.h +++ b/esphome/core/entity_base.h @@ -47,9 +47,9 @@ class EntityBase { std::string get_icon() const; void set_icon(const char *icon); - // Get/set this entity's device name - StringRef get_device_id() const; - void set_device_id(const StringRef *device_id); + // Get/set this entity's device id + const StringRef &get_device_id() const; + void set_device_id(const std::string device_id); protected: /// The hash_base() function has been deprecated. It is kept in this diff --git a/esphome/cpp_helpers.py b/esphome/cpp_helpers.py index df191bafe2..bfc9b3dc9b 100644 --- a/esphome/cpp_helpers.py +++ b/esphome/cpp_helpers.py @@ -117,7 +117,7 @@ async def setup_entity(var, config): add(var.set_entity_category(config[CONF_ENTITY_CATEGORY])) if CONF_DEVICE_ID in config: parent = await get_variable(config[CONF_DEVICE_ID]) - add(var.set_device_id(parent)) + add(var.set_device_id(parent.get_id())) def extract_registry_entry_config(