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

Attempt moving it to esphome config section

This commit is contained in:
Daniel Vikström
2025-05-06 10:41:46 +02:00
parent 3915e1f012
commit ff626b428f
6 changed files with 30 additions and 40 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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