From a8b76c617c09af7fb73ed1873e6c62c0d61d5acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Vikstr=C3=B6m?= Date: Sat, 6 Apr 2024 00:03:26 +0200 Subject: [PATCH] Some basic chain working --- esphome/components/device/__init__.py | 32 ++++++++++++--------------- esphome/components/device/device.h | 14 ++++++++++++ esphome/config_validation.py | 18 +++++---------- esphome/const.py | 1 - esphome/core/entity_base.cpp | 10 ++++----- esphome/core/entity_base.h | 6 ++--- esphome/cpp_helpers.py | 4 ++-- 7 files changed, 44 insertions(+), 41 deletions(-) create mode 100644 esphome/components/device/device.h diff --git a/esphome/components/device/__init__.py b/esphome/components/device/__init__.py index 7e45eb9c75..4d1be53a0b 100644 --- a/esphome/components/device/__init__.py +++ b/esphome/components/device/__init__.py @@ -2,34 +2,30 @@ from esphome import config_validation as cv from esphome import codegen as cg from esphome.const import CONF_ID, CONF_NAME -DeviceStruct = cg.esphome_ns.struct("Device") +# DeviceStruct = cg.esphome_ns.struct("Device") +# StringVar = cg.std_ns.struct("string") +StringRef = cg.esphome_ns.struct("StringRef") MULTI_CONF = True CONFIG_SCHEMA = cv.Schema( { - cv.Required(CONF_ID): cv.declare_id(DeviceStruct), + # cv.Required(CONF_ID): cv.declare_id(DeviceStruct), + # cv.Required(CONF_ID): cv.declare_id(StringVar), + cv.Required(CONF_ID): cv.declare_id(StringRef), cv.Required(CONF_NAME): cv.string, - # cv.Exclusive(CONF_RED, "red"): cv.percentage, - # cv.Exclusive(CONF_RED_INT, "red"): cv.uint8_t, - # cv.Exclusive(CONF_GREEN, "green"): cv.percentage, - # cv.Exclusive(CONF_GREEN_INT, "green"): cv.uint8_t, - # cv.Exclusive(CONF_BLUE, "blue"): cv.percentage, - # cv.Exclusive(CONF_BLUE_INT, "blue"): cv.uint8_t, - # cv.Exclusive(CONF_WHITE, "white"): cv.percentage, - # cv.Exclusive(CONF_WHITE_INT, "white"): cv.uint8_t, - }).extend(cv.COMPONENT_SCHEMA) + } +).extend(cv.COMPONENT_SCHEMA) async def to_code(config): - # paren = await cg.get_variable(config[CONF_WEB_SERVER_BASE_ID]) - # var = cg.new_Pvariable(config[CONF_ID], paren) - # await cg.register_component(var, config) - # cg.add_define("USE_CAPTIVE_PORTAL") - - cg.new_variable( + # cg.new_variable( + # config[CONF_ID], + # config[CONF_NAME], + # ) + cg.new_Pvariable( config[CONF_ID], - cg.new_Pvariable(config[CONF_NAME]), + config[CONF_NAME], ) # cg.add_define("USE_DEVICE_ID") diff --git a/esphome/components/device/device.h b/esphome/components/device/device.h new file mode 100644 index 0000000000..936c48b0da --- /dev/null +++ b/esphome/components/device/device.h @@ -0,0 +1,14 @@ +#pragma once + +namespace esphome { + +class Device { + public: + void set_name(std::string name) { name_ = name; } + std::string get_name(void) {return name_;} + + protected: + std::string name_ = ""; +}; + +} // namespace esphome diff --git a/esphome/config_validation.py b/esphome/config_validation.py index 0abbfc1aff..14a64d2277 100644 --- a/esphome/config_validation.py +++ b/esphome/config_validation.py @@ -349,16 +349,10 @@ def icon(value): ) -def device_name(value): - """Validate that a given config value is a valid device name.""" - value = string_strict(value) - if not value: - return value - # if re.match("^[\\w\\-]+:[\\w\\-]+$", value): - # return value - raise Invalid( - 'device name must be string that matches a defined device in "deviced:" section' - ) +def device_id(value): + StringRef = cg.esphome_ns.struct("StringRef") + validator = use_id(StringRef) + return validator(value) def boolean(value): @@ -1880,8 +1874,8 @@ 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): device_name, - + # Optional(CONF_DEVICE_ID): use_id(StringRef), + Optional(CONF_DEVICE_ID): device_id, } ) diff --git a/esphome/const.py b/esphome/const.py index 361d8147bd..55580e5bcd 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -210,7 +210,6 @@ CONF_DELIMITER = "delimiter" CONF_DELTA = "delta" CONF_DEST = "dest" CONF_DEVICE = "device" -CONF_DEVICES = "devices" CONF_DEVICE_CLASS = "device_class" CONF_DEVICE_FACTOR = "device_factor" CONF_DEVICE_ID = "device_id" diff --git a/esphome/core/entity_base.cpp b/esphome/core/entity_base.cpp index 883c23e9f3..15864e793c 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 -std::string EntityBase::get_device_name() const { - if (this->device_name_c_str_ == nullptr) { - return ""; +StringRef EntityBase::get_device_name() const { + if (this->device_name_.empty()) { + return StringRef(""); } - return this->device_name_c_str_; + return this->device_name_; } -void EntityBase::set_device_name(const char *device_name) { this->device_name_c_str_ = device_name; } +void EntityBase::set_device_name(const StringRef *device_name) { this->device_name_ = *device_name; } // 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 342a1fc042..0f6b222efd 100644 --- a/esphome/core/entity_base.h +++ b/esphome/core/entity_base.h @@ -48,8 +48,8 @@ class EntityBase { void set_icon(const char *icon); // Get/set this entity's device name - std::string get_device_name() const; - void set_device_name(const char *icon); + StringRef get_device_name() const; + void set_device_name(const StringRef *device_name); protected: /// The hash_base() function has been deprecated. It is kept in this @@ -65,7 +65,7 @@ class EntityBase { bool internal_{false}; bool disabled_by_default_{false}; EntityCategory entity_category_{ENTITY_CATEGORY_NONE}; - const char *device_name_c_str_{nullptr}; + StringRef device_name_; }; class EntityBase_DeviceClass { // NOLINT(readability-identifier-naming) diff --git a/esphome/cpp_helpers.py b/esphome/cpp_helpers.py index c1b1828d1c..afd951b504 100644 --- a/esphome/cpp_helpers.py +++ b/esphome/cpp_helpers.py @@ -112,8 +112,8 @@ async def setup_entity(var, config): if CONF_ENTITY_CATEGORY in config: add(var.set_entity_category(config[CONF_ENTITY_CATEGORY])) if CONF_DEVICE_ID in config: - # TODO: lookup the device from devices: section and get the real name - add(var.set_device_name(config[CONF_DEVICE_ID])) + parent = await get_variable(config[CONF_DEVICE_ID]) + add(var.set_device_name(parent)) def extract_registry_entry_config(