This commit is contained in:
DanielV 2024-05-02 15:25:41 +12:00 committed by GitHub
commit d516858e79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 101 additions and 0 deletions

View File

@ -267,6 +267,7 @@ message ListEntitiesBinarySensorResponse {
bool disabled_by_default = 7;
string icon = 8;
EntityCategory entity_category = 9;
string device_name = 10;
}
message BinarySensorStateResponse {
option (id) = 21;
@ -300,6 +301,7 @@ message ListEntitiesCoverResponse {
string icon = 10;
EntityCategory entity_category = 11;
bool supports_stop = 12;
string device_name = 13;
}
enum LegacyCoverState {

View File

@ -226,6 +226,7 @@ bool APIConnection::send_binary_sensor_info(binary_sensor::BinarySensor *binary_
msg.disabled_by_default = binary_sensor->is_disabled_by_default();
msg.icon = binary_sensor->get_icon();
msg.entity_category = static_cast<enums::EntityCategory>(binary_sensor->get_entity_category());
msg.device_name = binary_sensor->get_device_name();
return this->send_list_entities_binary_sensor_response(msg);
}
#endif
@ -262,6 +263,7 @@ bool APIConnection::send_cover_info(cover::Cover *cover) {
msg.disabled_by_default = cover->is_disabled_by_default();
msg.icon = cover->get_icon();
msg.entity_category = static_cast<enums::EntityCategory>(cover->get_entity_category());
msg.device_name = cover->get_device_name();
return this->send_list_entities_cover_response(msg);
}
void APIConnection::cover_command(const CoverCommandRequest &msg) {

View File

@ -950,6 +950,10 @@ bool ListEntitiesBinarySensorResponse::decode_length(uint32_t field_id, ProtoLen
this->icon = value.as_string();
return true;
}
case 10: {
this->device_name = value.as_string();
return true;
}
default:
return false;
}
@ -974,6 +978,7 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(7, this->disabled_by_default);
buffer.encode_string(8, this->icon);
buffer.encode_enum<enums::EntityCategory>(9, this->entity_category);
buffer.encode_string(10, this->device_name);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ListEntitiesBinarySensorResponse::dump_to(std::string &out) const {
@ -1015,6 +1020,10 @@ void ListEntitiesBinarySensorResponse::dump_to(std::string &out) const {
out.append(" entity_category: ");
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
out.append("\n");
out.append(" device_name: ");
out.append("'").append(this->device_name).append("'");
out.append("\n");
out.append("}");
}
#endif
@ -1118,6 +1127,10 @@ bool ListEntitiesCoverResponse::decode_length(uint32_t field_id, ProtoLengthDeli
this->icon = value.as_string();
return true;
}
case 13: {
this->device_name = value.as_string();
return true;
}
default:
return false;
}
@ -1145,6 +1158,7 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_string(10, this->icon);
buffer.encode_enum<enums::EntityCategory>(11, this->entity_category);
buffer.encode_bool(12, this->supports_stop);
buffer.encode_string(13, this->device_name);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ListEntitiesCoverResponse::dump_to(std::string &out) const {
@ -1198,6 +1212,10 @@ void ListEntitiesCoverResponse::dump_to(std::string &out) const {
out.append(" supports_stop: ");
out.append(YESNO(this->supports_stop));
out.append("\n");
out.append(" device_name: ");
out.append("'").append(this->device_name).append("'");
out.append("\n");
out.append("}");
}
#endif

View File

@ -386,6 +386,7 @@ class ListEntitiesBinarySensorResponse : public ProtoMessage {
bool disabled_by_default{false};
std::string icon{};
enums::EntityCategory entity_category{};
std::string device_name{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@ -424,6 +425,7 @@ class ListEntitiesCoverResponse : public ProtoMessage {
std::string icon{};
enums::EntityCategory entity_category{};
bool supports_stop{false};
std::string device_name{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;

View File

@ -0,0 +1,27 @@
from esphome import config_validation as cv
from esphome import codegen as cg
from esphome.const import CONF_ID, CONF_NAME
# ns = cg.esphome_ns.namespace("device")
# DeviceClass = ns.Class("Device")
StringRef = cg.esphome_ns.struct("StringRef")
MULTI_CONF = True
CODEOWNERS = ["@dala318"]
CONFIG_SCHEMA = cv.Schema(
{
# cv.Required(CONF_ID): cv.declare_id(DeviceClass),
cv.Required(CONF_ID): cv.declare_id(StringRef),
cv.Required(CONF_NAME): cv.string,
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
cg.new_Pvariable(
config[CONF_ID],
config[CONF_NAME],
)
# cg.add_define("USE_DEVICE_ID")

View File

@ -0,0 +1,16 @@
#pragma once
namespace esphome {
namespace device {
class Device {
public:
void set_name(std::string name) { name_ = name; }
std::string get_name(void) { return name_; }
protected:
std::string name_ = "";
};
} // namespace device
} // namespace esphome

View File

@ -19,6 +19,7 @@ from esphome.const import (
CONF_AVAILABILITY,
CONF_COMMAND_TOPIC,
CONF_COMMAND_RETAIN,
CONF_DEVICE_ID,
CONF_DISABLED_BY_DEFAULT,
CONF_DISCOVERY,
CONF_ENTITY_CATEGORY,
@ -343,6 +344,12 @@ def icon(value):
)
def device_id(value):
StringRef = cg.esphome_ns.struct("StringRef")
validator = use_id(StringRef)
return validator(value)
def boolean(value):
"""Validate the given config option to be a boolean.
@ -1898,6 +1905,7 @@ MQTT_COMMAND_COMPONENT_SCHEMA = MQTT_COMPONENT_SCHEMA.extend(
}
)
# StringRef = cg.esphome_ns.struct("StringRef")
ENTITY_BASE_SCHEMA = Schema(
{
Optional(CONF_NAME): Any(
@ -1913,6 +1921,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): use_id(StringRef),
Optional(CONF_DEVICE_ID): device_id,
}
)

View File

@ -201,6 +201,7 @@ CONF_DEST = "dest"
CONF_DEVICE = "device"
CONF_DEVICE_CLASS = "device_class"
CONF_DEVICE_FACTOR = "device_factor"
CONF_DEVICE_ID = "device_id"
CONF_DIELECTRIC_CONSTANT = "dielectric_constant"
CONF_DIMENSIONS = "dimensions"
CONF_DIO_PIN = "dio_pin"

View File

@ -35,6 +35,15 @@ 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_name() const {
if (this->device_name_.empty()) {
return StringRef("");
}
return this->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_; }
void EntityBase::set_entity_category(EntityCategory entity_category) { this->entity_category_ = entity_category; }

View File

@ -47,6 +47,10 @@ class EntityBase {
std::string get_icon() const;
void set_icon(const char *icon);
// Get/set this entity's device name
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
/// class for now, to prevent external components from not compiling.
@ -61,6 +65,7 @@ class EntityBase {
bool internal_{false};
bool disabled_by_default_{false};
EntityCategory entity_category_{ENTITY_CATEGORY_NONE};
StringRef device_name_;
};
class EntityBase_DeviceClass {

View File

@ -1,6 +1,7 @@
import logging
from esphome.const import (
CONF_DEVICE_ID,
CONF_DISABLED_BY_DEFAULT,
CONF_ENTITY_CATEGORY,
CONF_ICON,
@ -113,6 +114,9 @@ async def setup_entity(var, config):
add(var.set_icon(config[CONF_ICON]))
if CONF_ENTITY_CATEGORY in 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_name(parent))
def extract_registry_entry_config(

View File

@ -56,6 +56,10 @@ packages:
wifi: !include test_packages/test_packages_package_wifi.yaml
pkg_test: !include test_packages/test_packages_package1.yaml
device:
- id: dev_one
name: First device
wifi:
networks:
- ssid: "MySSID"
@ -1844,6 +1848,7 @@ esp32_touch:
binary_sensor:
- platform: gpio
name: "MCP23S08 Pin #1"
device_id: dev_one
pin:
mcp23xxx: mcp23s08_hub
# Use pin number 1