1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-14 09:12:19 +01:00

Some basic chain working

This commit is contained in:
Daniel Vikström
2024-04-06 00:03:26 +02:00
parent 1bd8985dff
commit a8b76c617c
7 changed files with 44 additions and 41 deletions

View File

@@ -2,34 +2,30 @@ from esphome import config_validation as cv
from esphome import codegen as cg from esphome import codegen as cg
from esphome.const import CONF_ID, CONF_NAME 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 MULTI_CONF = True
CONFIG_SCHEMA = cv.Schema( 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.Required(CONF_NAME): cv.string,
# cv.Exclusive(CONF_RED, "red"): cv.percentage, }
# cv.Exclusive(CONF_RED_INT, "red"): cv.uint8_t, ).extend(cv.COMPONENT_SCHEMA)
# 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)
async def to_code(config): async def to_code(config):
# paren = await cg.get_variable(config[CONF_WEB_SERVER_BASE_ID]) # cg.new_variable(
# var = cg.new_Pvariable(config[CONF_ID], paren) # config[CONF_ID],
# await cg.register_component(var, config) # config[CONF_NAME],
# cg.add_define("USE_CAPTIVE_PORTAL") # )
cg.new_Pvariable(
cg.new_variable(
config[CONF_ID], config[CONF_ID],
cg.new_Pvariable(config[CONF_NAME]), config[CONF_NAME],
) )
# cg.add_define("USE_DEVICE_ID") # cg.add_define("USE_DEVICE_ID")

View File

@@ -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

View File

@@ -349,16 +349,10 @@ def icon(value):
) )
def device_name(value): def device_id(value):
"""Validate that a given config value is a valid device name.""" StringRef = cg.esphome_ns.struct("StringRef")
value = string_strict(value) validator = use_id(StringRef)
if not value: return validator(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 boolean(value): def boolean(value):
@@ -1880,8 +1874,8 @@ ENTITY_BASE_SCHEMA = Schema(
Optional(CONF_DISABLED_BY_DEFAULT, default=False): boolean, Optional(CONF_DISABLED_BY_DEFAULT, default=False): boolean,
Optional(CONF_ICON): icon, Optional(CONF_ICON): icon,
Optional(CONF_ENTITY_CATEGORY): entity_category, 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,
} }
) )

View File

@@ -210,7 +210,6 @@ CONF_DELIMITER = "delimiter"
CONF_DELTA = "delta" CONF_DELTA = "delta"
CONF_DEST = "dest" CONF_DEST = "dest"
CONF_DEVICE = "device" CONF_DEVICE = "device"
CONF_DEVICES = "devices"
CONF_DEVICE_CLASS = "device_class" CONF_DEVICE_CLASS = "device_class"
CONF_DEVICE_FACTOR = "device_factor" CONF_DEVICE_FACTOR = "device_factor"
CONF_DEVICE_ID = "device_id" CONF_DEVICE_ID = "device_id"

View File

@@ -36,13 +36,13 @@ std::string EntityBase::get_icon() const {
void EntityBase::set_icon(const char *icon) { this->icon_c_str_ = icon; } void EntityBase::set_icon(const char *icon) { this->icon_c_str_ = icon; }
// Entity Device Name // Entity Device Name
std::string EntityBase::get_device_name() const { StringRef EntityBase::get_device_name() const {
if (this->device_name_c_str_ == nullptr) { if (this->device_name_.empty()) {
return ""; 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 // Entity Category
EntityCategory EntityBase::get_entity_category() const { return this->entity_category_; } EntityCategory EntityBase::get_entity_category() const { return this->entity_category_; }

View File

@@ -48,8 +48,8 @@ class EntityBase {
void set_icon(const char *icon); void set_icon(const char *icon);
// Get/set this entity's device name // Get/set this entity's device name
std::string get_device_name() const; StringRef get_device_name() const;
void set_device_name(const char *icon); void set_device_name(const StringRef *device_name);
protected: protected:
/// The hash_base() function has been deprecated. It is kept in this /// The hash_base() function has been deprecated. It is kept in this
@@ -65,7 +65,7 @@ class EntityBase {
bool internal_{false}; bool internal_{false};
bool disabled_by_default_{false}; bool disabled_by_default_{false};
EntityCategory entity_category_{ENTITY_CATEGORY_NONE}; EntityCategory entity_category_{ENTITY_CATEGORY_NONE};
const char *device_name_c_str_{nullptr}; StringRef device_name_;
}; };
class EntityBase_DeviceClass { // NOLINT(readability-identifier-naming) class EntityBase_DeviceClass { // NOLINT(readability-identifier-naming)

View File

@@ -112,8 +112,8 @@ async def setup_entity(var, config):
if CONF_ENTITY_CATEGORY in config: if CONF_ENTITY_CATEGORY in config:
add(var.set_entity_category(config[CONF_ENTITY_CATEGORY])) add(var.set_entity_category(config[CONF_ENTITY_CATEGORY]))
if CONF_DEVICE_ID in config: if CONF_DEVICE_ID in config:
# TODO: lookup the device from devices: section and get the real name parent = await get_variable(config[CONF_DEVICE_ID])
add(var.set_device_name(config[CONF_DEVICE_ID])) add(var.set_device_name(parent))
def extract_registry_entry_config( def extract_registry_entry_config(