EntityBase Refactor (#2418)

* Renamed Nameable to EntityBase (cpp)

* Renamed NAMEABLE_SCHEMA to ENTITY_BASE_SCHEMA (Python)

* Renamed cg.Nameable to cg.EntityBase (Python)

* Remove redundant use of CONF_NAME from esp32_touch

* Remove redundant use of CONF_NAME from mcp3008

* Updated test

* Moved EntityBase from Component.h and Component.cpp

* Added icon property to EntityBase

* Added CONF_ICON to ENTITY_BASE_SCHEMA and added setup_entity function to cpp_helpers

* Added MQTT component getters for icon and disabled_by_default

* Lint

* Removed icon field from MQTT components

* Code generation now uses setup_entity to setENTITY_BASE_SCHEMA fields

* Removed unused import

* Added cstdint include

* Optimisation: don't set icon if it is empty

* Remove icon from NumberTraits and SelectTraits

* Removed unused import

* Integration and Total Daily Energy sensors now inherit icons from their parents during code generation

* Minor comment correction

* Removed redundant icon-handling code from sensor, switch, and text_sensor

* Update esphome/components/tsl2591/tsl2591.h

Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>

* Added icon property to binary sensor, climate, cover, and fan component tests

* Added icons for Binary Sensor, Climate, Cover, Fan, and Light  to API

* Consolidated EntityBase fields in MQTT components

Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
Paul Monigatti
2021-10-10 21:37:05 +13:00
committed by GitHub
parent 92b85f98e8
commit 471b82f727
83 changed files with 395 additions and 351 deletions
+1 -1
View File
@@ -67,7 +67,7 @@ from esphome.cpp_types import ( # noqa
NAN,
esphome_ns,
App,
Nameable,
EntityBase,
Component,
ComponentPtr,
PollingComponent,
+5
View File
@@ -215,6 +215,7 @@ message ListEntitiesBinarySensorResponse {
string device_class = 5;
bool is_status_binary_sensor = 6;
bool disabled_by_default = 7;
string icon = 8;
}
message BinarySensorStateResponse {
option (id) = 21;
@@ -245,6 +246,7 @@ message ListEntitiesCoverResponse {
bool supports_tilt = 7;
string device_class = 8;
bool disabled_by_default = 9;
string icon = 10;
}
enum LegacyCoverState {
@@ -313,6 +315,7 @@ message ListEntitiesFanResponse {
bool supports_direction = 7;
int32 supported_speed_count = 8;
bool disabled_by_default = 9;
string icon = 10;
}
enum FanSpeed {
FAN_SPEED_LOW = 0;
@@ -388,6 +391,7 @@ message ListEntitiesLightResponse {
float max_mireds = 10;
repeated string effects = 11;
bool disabled_by_default = 13;
string icon = 14;
}
message LightStateResponse {
option (id) = 24;
@@ -790,6 +794,7 @@ message ListEntitiesClimateResponse {
repeated ClimatePreset supported_presets = 16;
repeated string supported_custom_presets = 17;
bool disabled_by_default = 18;
string icon = 19;
}
message ClimateStateResponse {
option (id) = 47;
+10 -4
View File
@@ -1,4 +1,5 @@
#include "api_connection.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/log.h"
#include "esphome/components/network/util.h"
#include "esphome/core/version.h"
@@ -143,8 +144,8 @@ void APIConnection::loop() {
}
}
std::string get_default_unique_id(const std::string &component_type, Nameable *nameable) {
return App.get_name() + component_type + nameable->get_object_id();
std::string get_default_unique_id(const std::string &component_type, EntityBase *entity) {
return App.get_name() + component_type + entity->get_object_id();
}
DisconnectResponse APIConnection::disconnect(const DisconnectRequest &msg) {
@@ -180,6 +181,7 @@ bool APIConnection::send_binary_sensor_info(binary_sensor::BinarySensor *binary_
msg.device_class = binary_sensor->get_device_class();
msg.is_status_binary_sensor = binary_sensor->is_status_binary_sensor();
msg.disabled_by_default = binary_sensor->is_disabled_by_default();
msg.icon = binary_sensor->get_icon();
return this->send_list_entities_binary_sensor_response(msg);
}
#endif
@@ -212,6 +214,7 @@ bool APIConnection::send_cover_info(cover::Cover *cover) {
msg.supports_tilt = traits.get_supports_tilt();
msg.device_class = cover->get_device_class();
msg.disabled_by_default = cover->is_disabled_by_default();
msg.icon = cover->get_icon();
return this->send_list_entities_cover_response(msg);
}
void APIConnection::cover_command(const CoverCommandRequest &msg) {
@@ -277,6 +280,7 @@ bool APIConnection::send_fan_info(fan::FanState *fan) {
msg.supports_direction = traits.supports_direction();
msg.supported_speed_count = traits.supported_speed_count();
msg.disabled_by_default = fan->is_disabled_by_default();
msg.icon = fan->get_icon();
return this->send_list_entities_fan_response(msg);
}
void APIConnection::fan_command(const FanCommandRequest &msg) {
@@ -339,6 +343,7 @@ bool APIConnection::send_light_info(light::LightState *light) {
msg.unique_id = get_default_unique_id("light", light);
msg.disabled_by_default = light->is_disabled_by_default();
msg.icon = light->get_icon();
for (auto mode : traits.get_supported_color_modes())
msg.supported_color_modes.push_back(static_cast<enums::ColorMode>(mode));
@@ -529,6 +534,7 @@ bool APIConnection::send_climate_info(climate::Climate *climate) {
msg.unique_id = get_default_unique_id("climate", climate);
msg.disabled_by_default = climate->is_disabled_by_default();
msg.icon = climate->get_icon();
msg.supports_current_temperature = traits.get_supports_current_temperature();
msg.supports_two_point_target_temperature = traits.get_supports_two_point_target_temperature();
@@ -601,7 +607,7 @@ bool APIConnection::send_number_info(number::Number *number) {
msg.object_id = number->get_object_id();
msg.name = number->get_name();
msg.unique_id = get_default_unique_id("number", number);
msg.icon = number->traits.get_icon();
msg.icon = number->get_icon();
msg.disabled_by_default = number->is_disabled_by_default();
msg.min_value = number->traits.get_min_value();
@@ -638,7 +644,7 @@ bool APIConnection::send_select_info(select::Select *select) {
msg.object_id = select->get_object_id();
msg.name = select->get_name();
msg.unique_id = get_default_unique_id("select", select);
msg.icon = select->traits.get_icon();
msg.icon = select->get_icon();
msg.disabled_by_default = select->is_disabled_by_default();
for (const auto &option : select->traits.get_options())
+45 -1
View File
@@ -2,7 +2,6 @@
// See scripts/api_protobuf/api_protobuf.py
#include "api_pb2.h"
#include "esphome/core/log.h"
#include <cstdio>
namespace esphome {
namespace api {
@@ -532,6 +531,10 @@ bool ListEntitiesBinarySensorResponse::decode_length(uint32_t field_id, ProtoLen
this->device_class = value.as_string();
return true;
}
case 8: {
this->icon = value.as_string();
return true;
}
default:
return false;
}
@@ -554,6 +557,7 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_string(5, this->device_class);
buffer.encode_bool(6, this->is_status_binary_sensor);
buffer.encode_bool(7, this->disabled_by_default);
buffer.encode_string(8, this->icon);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ListEntitiesBinarySensorResponse::dump_to(std::string &out) const {
@@ -587,6 +591,10 @@ void ListEntitiesBinarySensorResponse::dump_to(std::string &out) const {
out.append(" disabled_by_default: ");
out.append(YESNO(this->disabled_by_default));
out.append("\n");
out.append(" icon: ");
out.append("'").append(this->icon).append("'");
out.append("\n");
out.append("}");
}
#endif
@@ -678,6 +686,10 @@ bool ListEntitiesCoverResponse::decode_length(uint32_t field_id, ProtoLengthDeli
this->device_class = value.as_string();
return true;
}
case 10: {
this->icon = value.as_string();
return true;
}
default:
return false;
}
@@ -702,6 +714,7 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(7, this->supports_tilt);
buffer.encode_string(8, this->device_class);
buffer.encode_bool(9, this->disabled_by_default);
buffer.encode_string(10, this->icon);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ListEntitiesCoverResponse::dump_to(std::string &out) const {
@@ -743,6 +756,10 @@ void ListEntitiesCoverResponse::dump_to(std::string &out) const {
out.append(" disabled_by_default: ");
out.append(YESNO(this->disabled_by_default));
out.append("\n");
out.append(" icon: ");
out.append("'").append(this->icon).append("'");
out.append("\n");
out.append("}");
}
#endif
@@ -949,6 +966,10 @@ bool ListEntitiesFanResponse::decode_length(uint32_t field_id, ProtoLengthDelimi
this->unique_id = value.as_string();
return true;
}
case 10: {
this->icon = value.as_string();
return true;
}
default:
return false;
}
@@ -973,6 +994,7 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(7, this->supports_direction);
buffer.encode_int32(8, this->supported_speed_count);
buffer.encode_bool(9, this->disabled_by_default);
buffer.encode_string(10, this->icon);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ListEntitiesFanResponse::dump_to(std::string &out) const {
@@ -1015,6 +1037,10 @@ void ListEntitiesFanResponse::dump_to(std::string &out) const {
out.append(" disabled_by_default: ");
out.append(YESNO(this->disabled_by_default));
out.append("\n");
out.append(" icon: ");
out.append("'").append(this->icon).append("'");
out.append("\n");
out.append("}");
}
#endif
@@ -1263,6 +1289,10 @@ bool ListEntitiesLightResponse::decode_length(uint32_t field_id, ProtoLengthDeli
this->effects.push_back(value.as_string());
return true;
}
case 14: {
this->icon = value.as_string();
return true;
}
default:
return false;
}
@@ -1303,6 +1333,7 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_string(11, it, true);
}
buffer.encode_bool(13, this->disabled_by_default);
buffer.encode_string(14, this->icon);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ListEntitiesLightResponse::dump_to(std::string &out) const {
@@ -1366,6 +1397,10 @@ void ListEntitiesLightResponse::dump_to(std::string &out) const {
out.append(" disabled_by_default: ");
out.append(YESNO(this->disabled_by_default));
out.append("\n");
out.append(" icon: ");
out.append("'").append(this->icon).append("'");
out.append("\n");
out.append("}");
}
#endif
@@ -3073,6 +3108,10 @@ bool ListEntitiesClimateResponse::decode_length(uint32_t field_id, ProtoLengthDe
this->supported_custom_presets.push_back(value.as_string());
return true;
}
case 19: {
this->icon = value.as_string();
return true;
}
default:
return false;
}
@@ -3130,6 +3169,7 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_string(17, it, true);
}
buffer.encode_bool(18, this->disabled_by_default);
buffer.encode_string(19, this->icon);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ListEntitiesClimateResponse::dump_to(std::string &out) const {
@@ -3222,6 +3262,10 @@ void ListEntitiesClimateResponse::dump_to(std::string &out) const {
out.append(" disabled_by_default: ");
out.append(YESNO(this->disabled_by_default));
out.append("\n");
out.append(" icon: ");
out.append("'").append(this->icon).append("'");
out.append("\n");
out.append("}");
}
#endif
+5
View File
@@ -269,6 +269,7 @@ class ListEntitiesBinarySensorResponse : public ProtoMessage {
std::string device_class{};
bool is_status_binary_sensor{false};
bool disabled_by_default{false};
std::string icon{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@@ -304,6 +305,7 @@ class ListEntitiesCoverResponse : public ProtoMessage {
bool supports_tilt{false};
std::string device_class{};
bool disabled_by_default{false};
std::string icon{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@@ -360,6 +362,7 @@ class ListEntitiesFanResponse : public ProtoMessage {
bool supports_direction{false};
int32_t supported_speed_count{0};
bool disabled_by_default{false};
std::string icon{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@@ -424,6 +427,7 @@ class ListEntitiesLightResponse : public ProtoMessage {
float max_mireds{0.0f};
std::vector<std::string> effects{};
bool disabled_by_default{false};
std::string icon{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@@ -856,6 +860,7 @@ class ListEntitiesClimateResponse : public ProtoMessage {
std::vector<enums::ClimatePreset> supported_presets{};
std::vector<std::string> supported_custom_presets{};
bool disabled_by_default{false};
std::string icon{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
+5 -8
View File
@@ -1,15 +1,14 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.cpp_helpers import setup_entity
from esphome import automation, core
from esphome.automation import Condition, maybe_simple_id
from esphome.components import mqtt
from esphome.const import (
CONF_DELAY,
CONF_DEVICE_CLASS,
CONF_DISABLED_BY_DEFAULT,
CONF_FILTERS,
CONF_ID,
CONF_INTERNAL,
CONF_INVALID_COOLDOWN,
CONF_INVERTED,
CONF_MAX_LENGTH,
@@ -88,7 +87,7 @@ DEVICE_CLASSES = [
IS_PLATFORM_COMPONENT = True
binary_sensor_ns = cg.esphome_ns.namespace("binary_sensor")
BinarySensor = binary_sensor_ns.class_("BinarySensor", cg.Nameable)
BinarySensor = binary_sensor_ns.class_("BinarySensor", cg.EntityBase)
BinarySensorInitiallyOff = binary_sensor_ns.class_(
"BinarySensorInitiallyOff", BinarySensor
)
@@ -314,7 +313,7 @@ def validate_multi_click_timing(value):
device_class = cv.one_of(*DEVICE_CLASSES, lower=True, space="_")
BINARY_SENSOR_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMPONENT_SCHEMA).extend(
BINARY_SENSOR_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMPONENT_SCHEMA).extend(
{
cv.GenerateID(): cv.declare_id(BinarySensor),
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(
@@ -375,10 +374,8 @@ BINARY_SENSOR_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMPONENT_SCHEMA).exten
async def setup_binary_sensor_core_(var, config):
cg.add(var.set_name(config[CONF_NAME]))
cg.add(var.set_disabled_by_default(config[CONF_DISABLED_BY_DEFAULT]))
if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL]))
await setup_entity(var, config)
if CONF_DEVICE_CLASS in config:
cg.add(var.set_device_class(config[CONF_DEVICE_CLASS]))
if CONF_INVERTED in config:
@@ -42,7 +42,7 @@ void BinarySensor::send_state_internal(bool state, bool is_initial) {
}
}
std::string BinarySensor::device_class() { return ""; }
BinarySensor::BinarySensor(const std::string &name) : Nameable(name), state(false) {}
BinarySensor::BinarySensor(const std::string &name) : EntityBase(name), state(false) {}
BinarySensor::BinarySensor() : BinarySensor("") {}
void BinarySensor::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }
std::string BinarySensor::get_device_class() {
@@ -1,6 +1,7 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h"
#include "esphome/components/binary_sensor/filter.h"
@@ -22,7 +23,7 @@ namespace binary_sensor {
* The sub classes should notify the front-end of new states via the publish_state() method which
* handles inverted inputs for you.
*/
class BinarySensor : public Nameable {
class BinarySensor : public EntityBase {
public:
explicit BinarySensor();
/** Construct a binary sensor with the specified name
+5 -9
View File
@@ -1,14 +1,13 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.cpp_helpers import setup_entity
from esphome import automation
from esphome.components import mqtt
from esphome.const import (
CONF_AWAY,
CONF_CUSTOM_FAN_MODE,
CONF_CUSTOM_PRESET,
CONF_DISABLED_BY_DEFAULT,
CONF_ID,
CONF_INTERNAL,
CONF_MAX_TEMPERATURE,
CONF_MIN_TEMPERATURE,
CONF_MODE,
@@ -19,7 +18,6 @@ from esphome.const import (
CONF_TEMPERATURE_STEP,
CONF_VISUAL,
CONF_MQTT_ID,
CONF_NAME,
CONF_FAN_MODE,
CONF_SWING_MODE,
)
@@ -30,7 +28,7 @@ IS_PLATFORM_COMPONENT = True
CODEOWNERS = ["@esphome/core"]
climate_ns = cg.esphome_ns.namespace("climate")
Climate = climate_ns.class_("Climate", cg.Nameable)
Climate = climate_ns.class_("Climate", cg.EntityBase)
ClimateCall = climate_ns.class_("ClimateCall")
ClimateTraits = climate_ns.class_("ClimateTraits")
@@ -88,7 +86,7 @@ validate_climate_swing_mode = cv.enum(CLIMATE_SWING_MODES, upper=True)
# Actions
ControlAction = climate_ns.class_("ControlAction", automation.Action)
CLIMATE_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
CLIMATE_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
{
cv.GenerateID(): cv.declare_id(Climate),
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTClimateComponent),
@@ -105,10 +103,8 @@ CLIMATE_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).ext
async def setup_climate_core_(var, config):
cg.add(var.set_name(config[CONF_NAME]))
cg.add(var.set_disabled_by_default(config[CONF_DISABLED_BY_DEFAULT]))
if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL]))
await setup_entity(var, config)
visual = config[CONF_VISUAL]
if CONF_MIN_TEMPERATURE in visual:
cg.add(var.set_visual_min_temperature_override(visual[CONF_MIN_TEMPERATURE]))
+1 -1
View File
@@ -440,7 +440,7 @@ void Climate::set_visual_max_temperature_override(float visual_max_temperature_o
void Climate::set_visual_temperature_step_override(float visual_temperature_step_override) {
this->visual_temperature_step_override_ = visual_temperature_step_override;
}
Climate::Climate(const std::string &name) : Nameable(name) {}
Climate::Climate(const std::string &name) : EntityBase(name) {}
Climate::Climate() : Climate("") {}
ClimateCall Climate::make_call() { return ClimateCall(this); }
+2 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h"
#include "esphome/core/preferences.h"
#include "esphome/core/log.h"
@@ -163,7 +164,7 @@ struct ClimateDeviceRestoreState {
* mode etc). These are read-only for the user and rw for integrations. The reason these are public
* is for simple access to them from lambdas `if (id(my_climate).mode == climate::CLIMATE_MODE_HEAT_COOL) ...`
*/
class Climate : public Nameable {
class Climate : public EntityBase {
public:
/// Construct a climate device with empty name (will be set later).
Climate();
+5 -9
View File
@@ -4,18 +4,16 @@ from esphome import automation
from esphome.automation import maybe_simple_id, Condition
from esphome.components import mqtt
from esphome.const import (
CONF_DISABLED_BY_DEFAULT,
CONF_ID,
CONF_INTERNAL,
CONF_DEVICE_CLASS,
CONF_STATE,
CONF_POSITION,
CONF_TILT,
CONF_STOP,
CONF_MQTT_ID,
CONF_NAME,
)
from esphome.core import CORE, coroutine_with_priority
from esphome.cpp_helpers import setup_entity
IS_PLATFORM_COMPONENT = True
@@ -36,7 +34,7 @@ DEVICE_CLASSES = [
cover_ns = cg.esphome_ns.namespace("cover")
Cover = cover_ns.class_("Cover", cg.Nameable)
Cover = cover_ns.class_("Cover", cg.EntityBase)
COVER_OPEN = cover_ns.COVER_OPEN
COVER_CLOSED = cover_ns.COVER_CLOSED
@@ -65,7 +63,7 @@ CoverPublishAction = cover_ns.class_("CoverPublishAction", automation.Action)
CoverIsOpenCondition = cover_ns.class_("CoverIsOpenCondition", Condition)
CoverIsClosedCondition = cover_ns.class_("CoverIsClosedCondition", Condition)
COVER_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
COVER_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
{
cv.GenerateID(): cv.declare_id(Cover),
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTCoverComponent),
@@ -76,10 +74,8 @@ COVER_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).exten
async def setup_cover_core_(var, config):
cg.add(var.set_name(config[CONF_NAME]))
cg.add(var.set_disabled_by_default(config[CONF_DISABLED_BY_DEFAULT]))
if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL]))
await setup_entity(var, config)
if CONF_DEVICE_CLASS in config:
cg.add(var.set_device_class(config[CONF_DEVICE_CLASS]))
+1 -1
View File
@@ -31,7 +31,7 @@ const char *cover_operation_to_str(CoverOperation op) {
}
}
Cover::Cover(const std::string &name) : Nameable(name), position{COVER_OPEN} {}
Cover::Cover(const std::string &name) : EntityBase(name), position{COVER_OPEN} {}
uint32_t Cover::hash_base() { return 1727367479UL; }
+2 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h"
#include "esphome/core/preferences.h"
#include "cover_traits.h"
@@ -107,7 +108,7 @@ const char *cover_operation_to_str(CoverOperation op);
* to control all values of the cover. Also implement get_traits() to return what operations
* the cover supports.
*/
class Cover : public Nameable {
class Cover : public EntityBase {
public:
explicit Cover();
explicit Cover(const std::string &name);
+1 -1
View File
@@ -21,7 +21,7 @@ from esphome.components.esp32 import add_idf_sdkconfig_option
DEPENDENCIES = ["esp32", "api"]
esp32_camera_ns = cg.esphome_ns.namespace("esp32_camera")
ESP32Camera = esp32_camera_ns.class_("ESP32Camera", cg.PollingComponent, cg.Nameable)
ESP32Camera = esp32_camera_ns.class_("ESP32Camera", cg.PollingComponent, cg.EntityBase)
ESP32CameraFrameSize = esp32_camera_ns.enum("ESP32CameraFrameSize")
FRAME_SIZES = {
"160X120": ESP32CameraFrameSize.ESP32_CAMERA_SIZE_160X120,
@@ -172,7 +172,7 @@ void ESP32Camera::framebuffer_task(void *pv) {
esp_camera_fb_return(framebuffer);
}
}
ESP32Camera::ESP32Camera(const std::string &name) : Nameable(name) {
ESP32Camera::ESP32Camera(const std::string &name) : EntityBase(name) {
this->config_.pin_pwdn = -1;
this->config_.pin_reset = -1;
this->config_.pin_xclk = -1;
@@ -3,6 +3,7 @@
#ifdef USE_ESP32
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h"
#include <esp_camera.h>
#include <freertos/FreeRTOS.h>
@@ -50,7 +51,7 @@ enum ESP32CameraFrameSize {
ESP32_CAMERA_SIZE_1600X1200, // UXGA
};
class ESP32Camera : public Component, public Nameable {
class ESP32Camera : public Component, public EntityBase {
public:
ESP32Camera(const std::string &name);
void set_data_pins(std::array<uint8_t, 8> pins);
@@ -2,7 +2,6 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import (
CONF_NAME,
CONF_PIN,
CONF_THRESHOLD,
CONF_ID,
@@ -55,7 +54,6 @@ async def to_code(config):
hub = await cg.get_variable(config[CONF_ESP32_TOUCH_ID])
var = cg.new_Pvariable(
config[CONF_ID],
config[CONF_NAME],
TOUCH_PADS[config[CONF_PIN]],
config[CONF_THRESHOLD],
config[CONF_WAKEUP_THRESHOLD],
@@ -159,9 +159,8 @@ void ESP32TouchComponent::on_shutdown() {
}
}
ESP32TouchBinarySensor::ESP32TouchBinarySensor(const std::string &name, touch_pad_t touch_pad, uint16_t threshold,
uint16_t wakeup_threshold)
: BinarySensor(name), touch_pad_(touch_pad), threshold_(threshold), wakeup_threshold_(wakeup_threshold) {}
ESP32TouchBinarySensor::ESP32TouchBinarySensor(touch_pad_t touch_pad, uint16_t threshold, uint16_t wakeup_threshold)
: BinarySensor(), touch_pad_(touch_pad), threshold_(threshold), wakeup_threshold_(wakeup_threshold) {}
} // namespace esp32_touch
} // namespace esphome
+1 -1
View File
@@ -64,7 +64,7 @@ class ESP32TouchComponent : public Component {
/// Simple helper class to expose a touch pad value as a binary sensor.
class ESP32TouchBinarySensor : public binary_sensor::BinarySensor {
public:
ESP32TouchBinarySensor(const std::string &name, touch_pad_t touch_pad, uint16_t threshold, uint16_t wakeup_threshold);
ESP32TouchBinarySensor(touch_pad_t touch_pad, uint16_t threshold, uint16_t wakeup_threshold);
touch_pad_t get_touch_pad() const { return touch_pad_; }
uint16_t get_threshold() const { return threshold_; }
+4 -9
View File
@@ -4,9 +4,7 @@ from esphome import automation
from esphome.automation import maybe_simple_id
from esphome.components import mqtt
from esphome.const import (
CONF_DISABLED_BY_DEFAULT,
CONF_ID,
CONF_INTERNAL,
CONF_MQTT_ID,
CONF_OSCILLATING,
CONF_OSCILLATION_COMMAND_TOPIC,
@@ -16,7 +14,6 @@ from esphome.const import (
CONF_SPEED_LEVEL_STATE_TOPIC,
CONF_SPEED_COMMAND_TOPIC,
CONF_SPEED_STATE_TOPIC,
CONF_NAME,
CONF_ON_SPEED_SET,
CONF_ON_TURN_OFF,
CONF_ON_TURN_ON,
@@ -24,11 +21,12 @@ from esphome.const import (
CONF_DIRECTION,
)
from esphome.core import CORE, coroutine_with_priority
from esphome.cpp_helpers import setup_entity
IS_PLATFORM_COMPONENT = True
fan_ns = cg.esphome_ns.namespace("fan")
FanState = fan_ns.class_("FanState", cg.Nameable, cg.Component)
FanState = fan_ns.class_("FanState", cg.EntityBase, cg.Component)
MakeFan = cg.Application.struct("MakeFan")
FanDirection = fan_ns.enum("FanDirection")
@@ -50,7 +48,7 @@ FanSpeedSetTrigger = fan_ns.class_("FanSpeedSetTrigger", automation.Trigger.temp
FanIsOnCondition = fan_ns.class_("FanIsOnCondition", automation.Condition.template())
FanIsOffCondition = fan_ns.class_("FanIsOffCondition", automation.Condition.template())
FAN_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
FAN_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
{
cv.GenerateID(): cv.declare_id(FanState),
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTFanComponent),
@@ -92,10 +90,7 @@ FAN_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
async def setup_fan_core_(var, config):
cg.add(var.set_name(config[CONF_NAME]))
cg.add(var.set_disabled_by_default(config[CONF_DISABLED_BY_DEFAULT]))
if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL]))
await setup_entity(var, config)
if CONF_MQTT_ID in config:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
+1 -1
View File
@@ -12,7 +12,7 @@ void FanState::set_traits(const FanTraits &traits) { this->traits_ = traits; }
void FanState::add_on_state_callback(std::function<void()> &&callback) {
this->state_callback_.add(std::move(callback));
}
FanState::FanState(const std::string &name) : Nameable(name) {}
FanState::FanState(const std::string &name) : EntityBase(name) {}
FanStateCall FanState::turn_on() { return this->make_call().set_state(true); }
FanStateCall FanState::turn_off() { return this->make_call().set_state(false); }
+2 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h"
#include "esphome/core/preferences.h"
#include "esphome/core/log.h"
@@ -66,7 +67,7 @@ class FanStateCall {
optional<FanDirection> direction_{};
};
class FanState : public Nameable, public Component {
class FanState : public EntityBase, public Component {
public:
FanState() = default;
/// Construct the fan state with name.
@@ -64,7 +64,6 @@ class IntegrationSensor : public sensor::Sensor, public Component {
this->rtc_.save(&result_f);
}
std::string unit_of_measurement() override;
std::string icon() override { return this->sensor_->get_icon(); }
int8_t accuracy_decimals() override { return this->sensor_->get_accuracy_decimals() + 2; }
sensor::Sensor *sensor_;
+15 -2
View File
@@ -2,7 +2,8 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.components import sensor
from esphome.const import CONF_ID, CONF_SENSOR, CONF_RESTORE
from esphome.const import CONF_ICON, CONF_ID, CONF_SENSOR, CONF_RESTORE
from esphome.core.entity_helpers import inherit_property_from
integration_ns = cg.esphome_ns.namespace("integration")
IntegrationSensor = integration_ns.class_(
@@ -29,7 +30,6 @@ CONF_TIME_UNIT = "time_unit"
CONF_INTEGRATION_METHOD = "integration_method"
CONF_MIN_SAVE_INTERVAL = "min_save_interval"
CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(IntegrationSensor),
@@ -46,6 +46,19 @@ CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(
).extend(cv.COMPONENT_SCHEMA)
FINAL_VALIDATE_SCHEMA = cv.All(
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(IntegrationSensor),
cv.Optional(CONF_ICON): cv.icon,
cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor),
},
extra=cv.ALLOW_EXTRA,
),
inherit_property_from(CONF_ICON, CONF_SENSOR),
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
+6 -8
View File
@@ -5,13 +5,10 @@ from esphome.components import mqtt, power_supply
from esphome.const import (
CONF_COLOR_CORRECT,
CONF_DEFAULT_TRANSITION_LENGTH,
CONF_DISABLED_BY_DEFAULT,
CONF_EFFECTS,
CONF_FLASH_TRANSITION_LENGTH,
CONF_GAMMA_CORRECT,
CONF_ID,
CONF_INTERNAL,
CONF_NAME,
CONF_MQTT_ID,
CONF_POWER_SUPPLY,
CONF_RESTORE_MODE,
@@ -22,6 +19,7 @@ from esphome.const import (
CONF_WARM_WHITE_COLOR_TEMPERATURE,
)
from esphome.core import coroutine_with_priority
from esphome.cpp_helpers import setup_entity
from .automation import light_control_to_code # noqa
from .effects import (
validate_effects,
@@ -54,7 +52,7 @@ RESTORE_MODES = {
"RESTORE_INVERTED_DEFAULT_ON": LightRestoreMode.LIGHT_RESTORE_INVERTED_DEFAULT_ON,
}
LIGHT_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
LIGHT_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
{
cv.GenerateID(): cv.declare_id(LightState),
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTJSONLightComponent),
@@ -126,10 +124,10 @@ def validate_color_temperature_channels(value):
async def setup_light_core_(light_var, output_var, config):
cg.add(light_var.set_disabled_by_default(config[CONF_DISABLED_BY_DEFAULT]))
await setup_entity(light_var, config)
cg.add(light_var.set_restore_mode(config[CONF_RESTORE_MODE]))
if CONF_INTERNAL in config:
cg.add(light_var.set_internal(config[CONF_INTERNAL]))
if CONF_DEFAULT_TRANSITION_LENGTH in config:
cg.add(
light_var.set_default_transition_length(
@@ -167,7 +165,7 @@ async def setup_light_core_(light_var, output_var, config):
async def register_light(output_var, config):
light_var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME], output_var)
light_var = cg.new_Pvariable(config[CONF_ID], output_var)
cg.add(cg.App.register_light(light_var))
await cg.register_component(light_var, config)
await setup_light_core_(light_var, output_var, config)
+2 -1
View File
@@ -8,7 +8,8 @@ namespace light {
static const char *const TAG = "light";
LightState::LightState(const std::string &name, LightOutput *output) : Nameable(name), output_(output) {}
LightState::LightState(const std::string &name, LightOutput *output) : EntityBase(name), output_(output) {}
LightState::LightState(LightOutput *output) : output_(output) {}
LightTraits LightState::get_traits() { return this->output_->get_traits(); }
LightCall LightState::turn_on() { return this->make_call().set_state(true); }
+4 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/optional.h"
#include "esphome/core/preferences.h"
#include "light_call.h"
@@ -26,11 +27,13 @@ enum LightRestoreMode {
/** This class represents the communication layer between the front-end MQTT layer and the
* hardware output layer.
*/
class LightState : public Nameable, public Component {
class LightState : public EntityBase, public Component {
public:
/// Construct this LightState using the provided traits and name.
LightState(const std::string &name, LightOutput *output);
LightState(LightOutput *output);
LightTraits get_traits();
/// Make a light state call
+1 -1
View File
@@ -3,7 +3,7 @@ from esphome import automation
# Base
light_ns = cg.esphome_ns.namespace("light")
LightState = light_ns.class_("LightState", cg.Nameable, cg.Component)
LightState = light_ns.class_("LightState", cg.EntityBase, cg.Component)
# Fake class for addressable lights
AddressableLightState = light_ns.class_("LightState", LightState)
LightOutput = light_ns.class_("LightOutput")
+2 -4
View File
@@ -37,10 +37,8 @@ float MCP3008::read_data(uint8_t pin) {
return data / 1023.0f;
}
MCP3008Sensor::MCP3008Sensor(MCP3008 *parent, const std::string &name, uint8_t pin, float reference_voltage)
: PollingComponent(1000), parent_(parent), pin_(pin), reference_voltage_(reference_voltage) {
this->set_name(name);
}
MCP3008Sensor::MCP3008Sensor(MCP3008 *parent, uint8_t pin, float reference_voltage)
: PollingComponent(1000), parent_(parent), pin_(pin), reference_voltage_(reference_voltage) {}
float MCP3008Sensor::get_setup_priority() const { return setup_priority::DATA; }
+1 -1
View File
@@ -26,7 +26,7 @@ class MCP3008 : public Component,
class MCP3008Sensor : public PollingComponent, public sensor::Sensor, public voltage_sampler::VoltageSampler {
public:
MCP3008Sensor(MCP3008 *parent, const std::string &name, uint8_t pin, float reference_voltage);
MCP3008Sensor(MCP3008 *parent, uint8_t pin, float reference_voltage);
void set_reference_voltage(float reference_voltage) { reference_voltage_ = reference_voltage; }
void setup() override;
+1 -2
View File
@@ -1,7 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, voltage_sampler
from esphome.const import CONF_ID, CONF_NUMBER, CONF_NAME
from esphome.const import CONF_ID, CONF_NUMBER
from . import mcp3008_ns, MCP3008
AUTO_LOAD = ["voltage_sampler"]
@@ -29,7 +29,6 @@ async def to_code(config):
var = cg.new_Pvariable(
config[CONF_ID],
parent,
config[CONF_NAME],
config[CONF_NUMBER],
config[CONF_REFERENCE_VOLTAGE],
)
@@ -10,6 +10,7 @@ namespace mqtt {
static const char *const TAG = "mqtt.binary_sensor";
std::string MQTTBinarySensorComponent::component_type() const { return "binary_sensor"; }
const EntityBase *MQTTBinarySensorComponent::get_entity() const { return this->binary_sensor_; }
void MQTTBinarySensorComponent::setup() {
this->binary_sensor_->add_on_state_callback([this](bool state) { this->publish_state(state); });
@@ -25,7 +26,6 @@ MQTTBinarySensorComponent::MQTTBinarySensorComponent(binary_sensor::BinarySensor
this->set_custom_state_topic(mqtt::global_mqtt_client->get_availability().topic);
}
}
std::string MQTTBinarySensorComponent::friendly_name() const { return this->binary_sensor_->get_name(); }
void MQTTBinarySensorComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) {
if (!this->binary_sensor_->get_device_class().empty())
@@ -43,7 +43,6 @@ bool MQTTBinarySensorComponent::send_initial_state() {
return true;
}
}
bool MQTTBinarySensorComponent::is_internal() { return this->binary_sensor_->is_internal(); }
bool MQTTBinarySensorComponent::publish_state(bool state) {
if (this->binary_sensor_->is_status_binary_sensor())
return true;
+1 -2
View File
@@ -28,11 +28,10 @@ class MQTTBinarySensorComponent : public mqtt::MQTTComponent {
bool send_initial_state() override;
bool publish_state(bool state);
bool is_internal() override;
protected:
std::string friendly_name() const override;
std::string component_type() const override;
const EntityBase *get_entity() const override;
binary_sensor::BinarySensor *binary_sensor_;
};
+2 -2
View File
@@ -212,9 +212,9 @@ void MQTTClimateComponent::setup() {
}
MQTTClimateComponent::MQTTClimateComponent(Climate *device) : device_(device) {}
bool MQTTClimateComponent::send_initial_state() { return this->publish_state_(); }
bool MQTTClimateComponent::is_internal() { return this->device_->is_internal(); }
std::string MQTTClimateComponent::component_type() const { return "climate"; }
std::string MQTTClimateComponent::friendly_name() const { return this->device_->get_name(); }
const EntityBase *MQTTClimateComponent::get_entity() const { return this->device_; }
bool MQTTClimateComponent::publish_state_() {
auto traits = this->device_->get_traits();
// mode
+1 -2
View File
@@ -16,7 +16,6 @@ class MQTTClimateComponent : public mqtt::MQTTComponent {
MQTTClimateComponent(climate::Climate *device);
void send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) override;
bool send_initial_state() override;
bool is_internal() override;
std::string component_type() const override;
void setup() override;
@@ -38,7 +37,7 @@ class MQTTClimateComponent : public mqtt::MQTTComponent {
MQTT_COMPONENT_CUSTOM_TOPIC(swing_mode, command)
protected:
std::string friendly_name() const override;
const EntityBase *get_entity() const override;
bool publish_state_();
+13 -2
View File
@@ -68,8 +68,13 @@ bool MQTTComponent::send_discovery_() {
this->send_discovery(root, config);
std::string name = this->friendly_name();
root["name"] = name;
// Fields from EntityBase
root["name"] = this->friendly_name();
if (this->is_disabled_by_default())
root["enabled_by_default"] = false;
if (!this->get_icon().empty())
root["icon"] = this->get_icon();
if (config.state_topic)
root["state_topic"] = this->get_state_topic_();
if (config.command_topic)
@@ -199,6 +204,12 @@ void MQTTComponent::schedule_resend_state() { this->resend_state_ = true; }
std::string MQTTComponent::unique_id() { return ""; }
bool MQTTComponent::is_connected_() const { return global_mqtt_client->is_connected(); }
// Pull these properties from EntityBase if not overridden
std::string MQTTComponent::friendly_name() const { return this->get_entity()->get_name(); }
std::string MQTTComponent::get_icon() const { return this->get_entity()->get_icon(); }
bool MQTTComponent::is_disabled_by_default() const { return this->get_entity()->is_disabled_by_default(); }
bool MQTTComponent::is_internal() { return this->get_entity()->is_internal(); }
} // namespace mqtt
} // namespace esphome
+15 -3
View File
@@ -7,6 +7,7 @@
#include <memory>
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "mqtt_client.h"
namespace esphome {
@@ -73,7 +74,7 @@ class MQTTComponent : public Component {
virtual bool send_initial_state() = 0;
virtual bool is_internal() = 0;
virtual bool is_internal();
/// Set whether state message should be retained.
void set_retain(bool retain);
@@ -148,8 +149,10 @@ class MQTTComponent : public Component {
*/
std::string get_default_topic_for_(const std::string &suffix) const;
/// Get the friendly name of this MQTT component.
virtual std::string friendly_name() const = 0;
/**
* Gets the Entity served by this MQTT component.
*/
virtual const EntityBase *get_entity() const = 0;
/** A unique ID for this MQTT component, empty for no unique id. See unique ID requirements:
* https://developers.home-assistant.io/docs/en/entity_registry_index.html#unique-id-requirements
@@ -158,6 +161,15 @@ class MQTTComponent : public Component {
*/
virtual std::string unique_id();
/// Get the friendly name of this MQTT component.
virtual std::string friendly_name() const;
/// Get the icon field of this component
virtual std::string get_icon() const;
/// Get whether the underlying Entity is disabled by default
virtual bool is_disabled_by_default() const;
/// Get the MQTT topic that new states will be shared to.
const std::string get_state_topic_() const;
+2 -2
View File
@@ -84,9 +84,9 @@ void MQTTCoverComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryCon
}
std::string MQTTCoverComponent::component_type() const { return "cover"; }
std::string MQTTCoverComponent::friendly_name() const { return this->cover_->get_name(); }
const EntityBase *MQTTCoverComponent::get_entity() const { return this->cover_; }
bool MQTTCoverComponent::send_initial_state() { return this->publish_state(); }
bool MQTTCoverComponent::is_internal() { return this->cover_->is_internal(); }
bool MQTTCoverComponent::publish_state() {
auto traits = this->cover_->get_traits();
bool success = true;
+1 -2
View File
@@ -24,7 +24,6 @@ class MQTTCoverComponent : public mqtt::MQTTComponent {
MQTT_COMPONENT_CUSTOM_TOPIC(tilt, state)
bool send_initial_state() override;
bool is_internal() override;
bool publish_state();
@@ -32,7 +31,7 @@ class MQTTCoverComponent : public mqtt::MQTTComponent {
protected:
std::string component_type() const override;
std::string friendly_name() const override;
const EntityBase *get_entity() const override;
cover::Cover *cover_;
};
+2 -2
View File
@@ -16,6 +16,7 @@ MQTTFanComponent::MQTTFanComponent(FanState *state) : MQTTComponent(), state_(st
FanState *MQTTFanComponent::get_state() const { return this->state_; }
std::string MQTTFanComponent::component_type() const { return "fan"; }
const EntityBase *MQTTFanComponent::get_entity() const { return this->state_; }
void MQTTFanComponent::setup() {
this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
@@ -113,7 +114,7 @@ void MQTTFanComponent::dump_config() {
}
bool MQTTFanComponent::send_initial_state() { return this->publish_state(); }
std::string MQTTFanComponent::friendly_name() const { return this->state_->get_name(); }
void MQTTFanComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) {
if (this->state_->get_traits().supports_oscillation()) {
root["oscillation_command_topic"] = this->get_oscillation_command_topic();
@@ -126,7 +127,6 @@ void MQTTFanComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfi
root["speed_state_topic"] = this->get_speed_state_topic();
}
}
bool MQTTFanComponent::is_internal() { return this->state_->is_internal(); }
bool MQTTFanComponent::publish_state() {
const char *state_s = this->state_->state ? "ON" : "OFF";
ESP_LOGD(TAG, "'%s' Sending state %s.", this->state_->get_name().c_str(), state_s);
+1 -3
View File
@@ -39,10 +39,8 @@ class MQTTFanComponent : public mqtt::MQTTComponent {
fan::FanState *get_state() const;
bool is_internal() override;
protected:
std::string friendly_name() const override;
const EntityBase *get_entity() const override;
fan::FanState *state_;
};
+2 -2
View File
@@ -13,6 +13,7 @@ static const char *const TAG = "mqtt.light";
using namespace esphome::light;
std::string MQTTJSONLightComponent::component_type() const { return "light"; }
const EntityBase *MQTTJSONLightComponent::get_entity() const { return this->state_; }
void MQTTJSONLightComponent::setup() {
this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject &root) {
@@ -32,7 +33,7 @@ bool MQTTJSONLightComponent::publish_state_() {
[this](JsonObject &root) { LightJSONSchema::dump_json(*this->state_, root); });
}
LightState *MQTTJSONLightComponent::get_state() const { return this->state_; }
std::string MQTTJSONLightComponent::friendly_name() const { return this->state_->get_name(); }
void MQTTJSONLightComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) {
root["schema"] = "json";
auto traits = this->state_->get_traits();
@@ -70,7 +71,6 @@ void MQTTJSONLightComponent::send_discovery(JsonObject &root, mqtt::SendDiscover
}
}
bool MQTTJSONLightComponent::send_initial_state() { return this->publish_state_(); }
bool MQTTJSONLightComponent::is_internal() { return this->state_->is_internal(); }
void MQTTJSONLightComponent::dump_config() {
ESP_LOGCONFIG(TAG, "MQTT Light '%s':", this->state_->get_name().c_str());
LOG_MQTT_COMPONENT(true, true)
+1 -3
View File
@@ -25,11 +25,9 @@ class MQTTJSONLightComponent : public mqtt::MQTTComponent {
bool send_initial_state() override;
bool is_internal() override;
protected:
std::string friendly_name() const override;
std::string component_type() const override;
const EntityBase *get_entity() const override;
bool publish_state_();
+1 -4
View File
@@ -33,13 +33,11 @@ void MQTTNumberComponent::dump_config() {
}
std::string MQTTNumberComponent::component_type() const { return "number"; }
const EntityBase *MQTTNumberComponent::get_entity() const { return this->number_; }
std::string MQTTNumberComponent::friendly_name() const { return this->number_->get_name(); }
void MQTTNumberComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) {
const auto &traits = number_->traits;
// https://www.home-assistant.io/integrations/number.mqtt/
if (!traits.get_icon().empty())
root["icon"] = traits.get_icon();
root["min"] = traits.get_min_value();
root["max"] = traits.get_max_value();
root["step"] = traits.get_step();
@@ -53,7 +51,6 @@ bool MQTTNumberComponent::send_initial_state() {
return true;
}
}
bool MQTTNumberComponent::is_internal() { return this->number_->is_internal(); }
bool MQTTNumberComponent::publish_state(float value) {
char buffer[64];
snprintf(buffer, sizeof(buffer), "%f", value);
+1 -3
View File
@@ -28,15 +28,13 @@ class MQTTNumberComponent : public mqtt::MQTTComponent {
void send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) override;
bool send_initial_state() override;
bool is_internal() override;
bool publish_state(float value);
protected:
/// Override for MQTTComponent, returns "number".
std::string component_type() const override;
std::string friendly_name() const override;
const EntityBase *get_entity() const override;
number::Number *number_;
};
+1 -4
View File
@@ -28,13 +28,11 @@ void MQTTSelectComponent::dump_config() {
}
std::string MQTTSelectComponent::component_type() const { return "select"; }
const EntityBase *MQTTSelectComponent::get_entity() const { return this->select_; }
std::string MQTTSelectComponent::friendly_name() const { return this->select_->get_name(); }
void MQTTSelectComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) {
const auto &traits = select_->traits;
// https://www.home-assistant.io/integrations/select.mqtt/
if (!traits.get_icon().empty())
root["icon"] = traits.get_icon();
JsonArray &options = root.createNestedArray("options");
for (const auto &option : traits.get_options())
options.add(option);
@@ -48,7 +46,6 @@ bool MQTTSelectComponent::send_initial_state() {
return true;
}
}
bool MQTTSelectComponent::is_internal() { return this->select_->is_internal(); }
bool MQTTSelectComponent::publish_state(const std::string &value) {
return this->publish(this->get_state_topic_(), value);
}
+1 -3
View File
@@ -28,15 +28,13 @@ class MQTTSelectComponent : public mqtt::MQTTComponent {
void send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) override;
bool send_initial_state() override;
bool is_internal() override;
bool publish_state(const std::string &value);
protected:
/// Override for MQTTComponent, returns "select".
std::string component_type() const override;
std::string friendly_name() const override;
const EntityBase *get_entity() const override;
select::Select *select_;
};
+2 -5
View File
@@ -30,6 +30,7 @@ void MQTTSensorComponent::dump_config() {
}
std::string MQTTSensorComponent::component_type() const { return "sensor"; }
const EntityBase *MQTTSensorComponent::get_entity() const { return this->sensor_; }
uint32_t MQTTSensorComponent::get_expire_after() const {
if (this->expire_after_.has_value())
@@ -38,7 +39,7 @@ uint32_t MQTTSensorComponent::get_expire_after() const {
}
void MQTTSensorComponent::set_expire_after(uint32_t expire_after) { this->expire_after_ = expire_after; }
void MQTTSensorComponent::disable_expire_after() { this->expire_after_ = 0; }
std::string MQTTSensorComponent::friendly_name() const { return this->sensor_->get_name(); }
void MQTTSensorComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) {
if (!this->sensor_->get_device_class().empty())
root["device_class"] = this->sensor_->get_device_class();
@@ -49,9 +50,6 @@ void MQTTSensorComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryCo
if (this->get_expire_after() > 0)
root["expire_after"] = this->get_expire_after() / 1000;
if (!this->sensor_->get_icon().empty())
root["icon"] = this->sensor_->get_icon();
if (this->sensor_->get_force_update())
root["force_update"] = true;
@@ -67,7 +65,6 @@ bool MQTTSensorComponent::send_initial_state() {
return true;
}
}
bool MQTTSensorComponent::is_internal() { return this->sensor_->is_internal(); }
bool MQTTSensorComponent::publish_state(float value) {
int8_t accuracy = this->sensor_->get_accuracy_decimals();
return this->publish(this->get_state_topic_(), value_accuracy_to_string(value, accuracy));
+1 -4
View File
@@ -41,14 +41,11 @@ class MQTTSensorComponent : public mqtt::MQTTComponent {
bool publish_state(float value);
bool send_initial_state() override;
bool is_internal() override;
protected:
/// Override for MQTTComponent, returns "sensor".
std::string component_type() const override;
std::string friendly_name() const override;
const EntityBase *get_entity() const override;
std::string unique_id() override;
sensor::Sensor *sensor_;

Some files were not shown because too many files have changed in this diff Show More