mirror of
https://github.com/esphome/esphome.git
synced 2025-09-28 08:02:23 +01:00
Event entity support (#6451)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -119,6 +119,7 @@ MQTTTextComponent = mqtt_ns.class_("MQTTTextComponent", MQTTComponent)
|
||||
MQTTSelectComponent = mqtt_ns.class_("MQTTSelectComponent", MQTTComponent)
|
||||
MQTTButtonComponent = mqtt_ns.class_("MQTTButtonComponent", MQTTComponent)
|
||||
MQTTLockComponent = mqtt_ns.class_("MQTTLockComponent", MQTTComponent)
|
||||
MQTTEventComponent = mqtt_ns.class_("MQTTEventComponent", MQTTComponent)
|
||||
MQTTValveComponent = mqtt_ns.class_("MQTTValveComponent", MQTTComponent)
|
||||
|
||||
MQTTDiscoveryUniqueIdGenerator = mqtt_ns.enum("MQTTDiscoveryUniqueIdGenerator")
|
||||
|
@@ -73,6 +73,8 @@ constexpr const char *const MQTT_ENABLED_BY_DEFAULT = "en";
|
||||
constexpr const char *const MQTT_ENTITY_CATEGORY = "ent_cat";
|
||||
constexpr const char *const MQTT_ERROR_TEMPLATE = "err_tpl";
|
||||
constexpr const char *const MQTT_ERROR_TOPIC = "err_t";
|
||||
constexpr const char *const MQTT_EVENT_TYPE = "event_type";
|
||||
constexpr const char *const MQTT_EVENT_TYPES = "evt_typ";
|
||||
constexpr const char *const MQTT_EXPIRE_AFTER = "exp_aft";
|
||||
constexpr const char *const MQTT_FAN_MODE_COMMAND_TEMPLATE = "fan_mode_cmd_tpl";
|
||||
constexpr const char *const MQTT_FAN_MODE_COMMAND_TOPIC = "fan_mode_cmd_t";
|
||||
@@ -330,6 +332,8 @@ constexpr const char *const MQTT_ENABLED_BY_DEFAULT = "enabled_by_default";
|
||||
constexpr const char *const MQTT_ENTITY_CATEGORY = "entity_category";
|
||||
constexpr const char *const MQTT_ERROR_TEMPLATE = "error_template";
|
||||
constexpr const char *const MQTT_ERROR_TOPIC = "error_topic";
|
||||
constexpr const char *const MQTT_EVENT_TYPE = "event_type";
|
||||
constexpr const char *const MQTT_EVENT_TYPES = "event_types";
|
||||
constexpr const char *const MQTT_EXPIRE_AFTER = "expire_after";
|
||||
constexpr const char *const MQTT_FAN_MODE_COMMAND_TEMPLATE = "fan_mode_command_template";
|
||||
constexpr const char *const MQTT_FAN_MODE_COMMAND_TOPIC = "fan_mode_command_topic";
|
||||
|
54
esphome/components/mqtt/mqtt_event.cpp
Normal file
54
esphome/components/mqtt/mqtt_event.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "mqtt_event.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include "mqtt_const.h"
|
||||
|
||||
#ifdef USE_MQTT
|
||||
#ifdef USE_EVENT
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt {
|
||||
|
||||
static const char *const TAG = "mqtt.event";
|
||||
|
||||
using namespace esphome::event;
|
||||
|
||||
MQTTEventComponent::MQTTEventComponent(event::Event *event) : event_(event) {}
|
||||
|
||||
void MQTTEventComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) {
|
||||
JsonArray event_types = root.createNestedArray(MQTT_EVENT_TYPES);
|
||||
for (const auto &event_type : this->event_->get_event_types())
|
||||
event_types.add(event_type);
|
||||
|
||||
if (!this->event_->get_device_class().empty())
|
||||
root[MQTT_DEVICE_CLASS] = this->event_->get_device_class();
|
||||
|
||||
config.command_topic = false;
|
||||
}
|
||||
|
||||
void MQTTEventComponent::setup() {
|
||||
this->event_->add_on_event_callback([this](const std::string &event_type) { this->publish_event_(event_type); });
|
||||
}
|
||||
|
||||
void MQTTEventComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "MQTT Event '%s': ", this->event_->get_name().c_str());
|
||||
ESP_LOGCONFIG(TAG, "Event Types: ");
|
||||
for (const auto &event_type : this->event_->get_event_types()) {
|
||||
ESP_LOGCONFIG(TAG, "- %s", event_type.c_str());
|
||||
}
|
||||
LOG_MQTT_COMPONENT(true, true);
|
||||
}
|
||||
|
||||
bool MQTTEventComponent::publish_event_(const std::string &event_type) {
|
||||
return this->publish_json(this->get_state_topic_(),
|
||||
[event_type](JsonObject root) { root[MQTT_EVENT_TYPE] = event_type; });
|
||||
}
|
||||
|
||||
std::string MQTTEventComponent::component_type() const { return "event"; }
|
||||
const EntityBase *MQTTEventComponent::get_entity() const { return this->event_; }
|
||||
|
||||
} // namespace mqtt
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
||||
#endif // USE_MQTT
|
39
esphome/components/mqtt/mqtt_event.h
Normal file
39
esphome/components/mqtt/mqtt_event.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#ifdef USE_MQTT
|
||||
#ifdef USE_EVENT
|
||||
|
||||
#include "esphome/components/event/event.h"
|
||||
#include "mqtt_component.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt {
|
||||
|
||||
class MQTTEventComponent : public mqtt::MQTTComponent {
|
||||
public:
|
||||
explicit MQTTEventComponent(event::Event *event);
|
||||
|
||||
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override;
|
||||
|
||||
void setup() override;
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
/// Events do not send a state so just return true.
|
||||
bool send_initial_state() override { return true; }
|
||||
|
||||
protected:
|
||||
bool publish_event_(const std::string &event_type);
|
||||
std::string component_type() const override;
|
||||
const EntityBase *get_entity() const override;
|
||||
|
||||
event::Event *event_;
|
||||
};
|
||||
|
||||
} // namespace mqtt
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
||||
#endif // USE_MQTT
|
Reference in New Issue
Block a user