ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
mqtt_number.cpp
Go to the documentation of this file.
1#include "mqtt_number.h"
2#include "esphome/core/log.h"
4
5#include "mqtt_const.h"
6
7#ifdef USE_MQTT
8#ifdef USE_NUMBER
9
10namespace esphome::mqtt {
11
12static const char *const TAG = "mqtt.number";
13
14using namespace esphome::number;
15
16// Number mode MQTT strings indexed by NumberMode enum: AUTO(0) is skipped, BOX(1), SLIDER(2)
17PROGMEM_STRING_TABLE(NumberMqttModeStrings, "", "box", "slider");
18
20
22 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &state) {
24 if (!val.has_value()) {
25 ESP_LOGW(TAG, "Can't convert '%s' to number!", state.c_str());
26 return;
27 }
28 auto call = this->number_->make_call();
30 call.perform();
31 });
32 this->number_->add_on_state_callback([this](float state) { this->publish_state(state); });
33}
34
36 ESP_LOGCONFIG(TAG, "MQTT Number '%s':", this->number_->get_name().c_str());
37 LOG_MQTT_COMPONENT(true, false);
38}
39
41const EntityBase *MQTTNumberComponent::get_entity() const { return this->number_; }
42
44 const auto &traits = number_->traits;
45 // https://www.home-assistant.io/integrations/number.mqtt/
46 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
47 root[MQTT_MIN] = traits.get_min_value();
48 root[MQTT_MAX] = traits.get_max_value();
49 root[MQTT_STEP] = traits.get_step();
50 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
51 const auto unit_of_measurement = this->number_->traits.get_unit_of_measurement_ref();
52 if (!unit_of_measurement.empty()) {
53 root[MQTT_UNIT_OF_MEASUREMENT] = unit_of_measurement;
54 }
55 const auto mode = this->number_->traits.get_mode();
56 if (mode != NUMBER_MODE_AUTO) {
57 root[MQTT_MODE] =
58 NumberMqttModeStrings::get_progmem_str(static_cast<uint8_t>(mode), static_cast<uint8_t>(NUMBER_MODE_BOX));
59 }
60 const auto device_class = this->number_->traits.get_device_class_ref();
61 if (!device_class.empty()) {
62 root[MQTT_DEVICE_CLASS] = device_class;
63 }
64 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
65
66 config.command_topic = true;
67}
69 if (this->number_->has_state()) {
70 return this->publish_state(this->number_->state);
71 } else {
72 return true;
73 }
74}
76 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
77 char buffer[64];
78 size_t len = buf_append_printf(buffer, sizeof(buffer), 0, "%f", value);
79 return this->publish(this->get_state_topic_to_(topic_buf), buffer, len);
80}
81
82} // namespace esphome::mqtt
83
84#endif
85#endif // USE_MQTT
BedjetMode mode
BedJet operating mode.
StringRef get_device_class_ref() const
Get the device class as StringRef.
StringRef get_unit_of_measurement_ref() const
Get the unit of measurement as StringRef.
const StringRef & get_name() const
bool has_state() const
constexpr const char * c_str() const
Definition string_ref.h:73
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
StringRef get_state_topic_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf) const
Get the MQTT state topic into a buffer (no heap allocation for non-lambda custom topics).
std::string get_command_topic_() const
Get the MQTT topic for listening to commands (allocates std::string).
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
MQTTNumberComponent(number::Number *number)
Construct this MQTTNumberComponent instance with the provided friendly_name and number.
void setup() override
Override setup.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
NumberCall & set_value(float value)
Base-class for all numbers.
Definition number.h:29
NumberCall make_call()
Definition number.h:35
NumberTraits traits
Definition number.h:39
void add_on_state_callback(std::function< void(float)> &&callback)
Definition number.cpp:32
NumberMode get_mode() const
bool state
Definition fan.h:2
mopeka_std_values val[4]
PROGMEM_STRING_TABLE(AlarmControlPanelStateStrings, "DISARMED", "ARMED_HOME", "ARMED_AWAY", "ARMED_NIGHT", "ARMED_VACATION", "ARMED_CUSTOM_BYPASS", "PENDING", "ARMING", "DISARMING", "TRIGGERED", "UNKNOWN")
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
std::string size_t len
Definition helpers.h:817
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:910
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.