1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-30 22:53:59 +00:00

make areas and devices consistant

This commit is contained in:
J. Nick Koston
2025-06-21 17:05:46 +02:00
parent 98de53f60b
commit 8714e80978
8 changed files with 28 additions and 51 deletions

View File

@@ -193,7 +193,7 @@ message AreaInfo {
string name = 2;
}
message SubDeviceInfo {
message DeviceInfo {
uint32 device_id = 1;
string name = 2;
uint32 area_id = 3;
@@ -248,7 +248,7 @@ message DeviceInfoResponse {
// Supports receiving and saving api encryption key
bool api_encryption_supported = 19;
repeated SubDeviceInfo sub_devices = 20;
repeated DeviceInfo devices = 20;
repeated AreaInfo areas = 21;
// Top-level area info to phase out suggested_area

View File

@@ -1620,13 +1620,13 @@ DeviceInfoResponse APIConnection::device_info(const DeviceInfoRequest &msg) {
#ifdef USE_API_NOISE
resp.api_encryption_supported = true;
#endif
#ifdef USE_SUB_DEVICE
for (auto const &sub_device : App.get_sub_devices()) {
SubDeviceInfo sub_device_info;
sub_device_info.device_id = sub_device->get_device_id();
sub_device_info.name = sub_device->get_name();
sub_device_info.area_id = sub_device->get_area_id();
resp.sub_devices.push_back(sub_device_info);
#ifdef USE_DEVICES
for (auto const &device : App.get_devices()) {
DeviceInfo device_info;
device_info.device_id = device->get_device_id();
device_info.name = device->get_name();
device_info.area_id = device->get_area_id();
resp.devices.push_back(device_info);
}
#endif
#ifdef USE_AREAS

View File

@@ -301,7 +301,7 @@ class APIConnection : public APIServerConnection {
response.icon = entity->get_icon();
response.disabled_by_default = entity->is_disabled_by_default();
response.entity_category = static_cast<enums::EntityCategory>(entity->get_entity_category());
#ifdef USE_SUB_DEVICE
#ifdef USE_DEVICES
response.device_id = entity->get_device_id();
#endif
}

View File

@@ -9,8 +9,8 @@
#include "esphome/core/preferences.h"
#include "esphome/core/scheduler.h"
#ifdef USE_SUB_DEVICE
#include "esphome/core/sub_device.h"
#ifdef USE_DEVICES
#include "esphome/core/device.h"
#endif
#ifdef USE_AREAS
#include "esphome/core/area.h"
@@ -114,8 +114,8 @@ class Application {
this->compilation_time_ = compilation_time;
}
#ifdef USE_SUB_DEVICE
void register_sub_device(SubDevice *sub_device) { this->sub_devices_.push_back(sub_device); }
#ifdef USE_DEVICES
void register_device(Device *device) { this->devices_.push_back(device); }
#endif
#ifdef USE_AREAS
void register_area(Area *area) { this->areas_.push_back(area); }
@@ -299,7 +299,7 @@ class Application {
const std::string &get_friendly_name() const { return this->friendly_name_; }
/// Get the area of this Application set by pre_setup().
std::string get_area() const {
const char *get_area() const {
#ifdef USE_AREAS
// If we have areas registered, return the name of the first one (which is the top-level area)
if (!this->areas_.empty() && this->areas_[0] != nullptr) {
@@ -356,8 +356,8 @@ class Application {
uint8_t get_app_state() const { return this->app_state_; }
#ifdef USE_SUB_DEVICE
const std::vector<SubDevice *> &get_sub_devices() { return this->sub_devices_; }
#ifdef USE_DEVICES
const std::vector<Device *> &get_devices() { return this->devices_; }
#endif
#ifdef USE_AREAS
const std::vector<Area *> &get_areas() { return this->areas_; }
@@ -638,8 +638,8 @@ class Application {
uint16_t current_loop_index_{0};
bool in_loop_{false};
#ifdef USE_SUB_DEVICE
std::vector<SubDevice *> sub_devices_{};
#ifdef USE_DEVICES
std::vector<Device *> devices_{};
#endif
#ifdef USE_AREAS
std::vector<Area *> areas_{};

View File

@@ -1,6 +1,5 @@
#pragma once
#include <string>
#include <cstdint>
namespace esphome {
@@ -9,12 +8,12 @@ class Area {
public:
void set_area_id(uint32_t area_id) { area_id_ = area_id; }
uint32_t get_area_id() { return area_id_; }
void set_name(std::string name) { name_ = std::move(name); }
std::string get_name() { return name_; }
void set_name(const char *name) { name_ = name; }
const char *get_name() { return name_; }
protected:
uint32_t area_id_{};
std::string name_ = "";
const char *name_ = "";
};
} // namespace esphome

View File

@@ -58,7 +58,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")
Device = cg.esphome_ns.class_("Device")
Area = cg.esphome_ns.class_("Area")
VALID_INCLUDE_EXTS = {".h", ".hpp", ".tcc", ".ino", ".cpp", ".c"}
@@ -197,7 +197,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_SUB_DEVICES, default=[]): cv.ensure_list(
cv.Schema(
{
cv.GenerateID(CONF_ID): cv.declare_id(SubDevice),
cv.GenerateID(CONF_ID): cv.declare_id(Device),
cv.Required(CONF_NAME): cv.string,
cv.Optional(CONF_AREA_ID): cv.use_id(Area),
}
@@ -507,5 +507,5 @@ async def to_code(config):
# Get the area variable and use its area_id
area = await cg.get_variable(dev_conf[CONF_AREA_ID])
cg.add(dev.set_area_id(area.get_area_id()))
cg.add(cg.App.register_sub_device(dev))
cg.add_define("USE_SUB_DEVICE")
cg.add(cg.App.register_device(dev))
cg.add_define("USE_DEVICES")

View File

@@ -51,7 +51,7 @@ class EntityBase {
std::string get_icon() const;
void set_icon(const char *icon);
#ifdef USE_SUB_DEVICE
#ifdef USE_DEVICES
// Get/set this entity's device id
uint32_t get_device_id() const { return this->device_id_; }
void set_device_id(const uint32_t device_id) { this->device_id_ = device_id; }
@@ -73,7 +73,7 @@ class EntityBase {
const char *object_id_c_str_{nullptr};
const char *icon_c_str_{nullptr};
uint32_t object_id_hash_{};
#ifdef USE_SUB_DEVICE
#ifdef USE_DEVICES
uint32_t device_id_{};
#endif

View File

@@ -1,22 +0,0 @@
#pragma once
#include "esphome/core/string_ref.h"
namespace esphome {
class SubDevice {
public:
void set_device_id(uint32_t device_id) { device_id_ = device_id; }
uint32_t get_device_id() { return device_id_; }
void set_name(std::string name) { name_ = std::move(name); }
std::string get_name() { return name_; }
void set_area_id(uint32_t area_id) { area_id_ = area_id; }
uint32_t get_area_id() { return area_id_; }
protected:
uint32_t device_id_{};
uint32_t area_id_{};
std::string name_ = "";
};
} // namespace esphome