ESPHome 2026.1.4
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"
3
4#include "mqtt_const.h"
5
6#ifdef USE_MQTT
7#ifdef USE_NUMBER
8
9namespace esphome::mqtt {
10
11static const char *const TAG = "mqtt.number";
12
13using namespace esphome::number;
14
16
18 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &state) {
20 if (!val.has_value()) {
21 ESP_LOGW(TAG, "Can't convert '%s' to number!", state.c_str());
22 return;
23 }
24 auto call = this->number_->make_call();
26 call.perform();
27 });
28 this->number_->add_on_state_callback([this](float state) { this->publish_state(state); });
29}
30
32 ESP_LOGCONFIG(TAG, "MQTT Number '%s':", this->number_->get_name().c_str());
33 LOG_MQTT_COMPONENT(true, false)
34}
35
37const EntityBase *MQTTNumberComponent::get_entity() const { return this->number_; }
38
40 const auto &traits = number_->traits;
41 // https://www.home-assistant.io/integrations/number.mqtt/
42 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
43 root[MQTT_MIN] = traits.get_min_value();
44 root[MQTT_MAX] = traits.get_max_value();
45 root[MQTT_STEP] = traits.get_step();
46 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
47 const auto unit_of_measurement = this->number_->traits.get_unit_of_measurement_ref();
48 if (!unit_of_measurement.empty()) {
49 root[MQTT_UNIT_OF_MEASUREMENT] = unit_of_measurement;
50 }
51 switch (this->number_->traits.get_mode()) {
53 break;
54 case NUMBER_MODE_BOX:
55 root[MQTT_MODE] = "box";
56 break;
58 root[MQTT_MODE] = "slider";
59 break;
60 }
61 const auto device_class = this->number_->traits.get_device_class_ref();
62 if (!device_class.empty()) {
63 root[MQTT_DEVICE_CLASS] = device_class;
64 }
65 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
66
67 config.command_topic = true;
68}
70 if (this->number_->has_state()) {
71 return this->publish_state(this->number_->state);
72 } else {
73 return true;
74 }
75}
77 char buffer[64];
78 snprintf(buffer, sizeof(buffer), "%f", value);
79 return this->publish(this->get_state_topic_(), buffer);
80}
81
82} // namespace esphome::mqtt
83
84#endif
85#endif // USE_MQTT
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.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
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:41
NumberMode get_mode() const
bool state
Definition fan.h:0
mopeka_std_values val[4]
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:640
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.