ESPHome 2025.9.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"
3
4#include "mqtt_const.h"
5
6#ifdef USE_MQTT
7#ifdef USE_NUMBER
8
9namespace esphome {
10namespace mqtt {
11
12static const char *const TAG = "mqtt.number";
13
14using namespace esphome::number;
15
17
19 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &state) {
21 if (!val.has_value()) {
22 ESP_LOGW(TAG, "Can't convert '%s' to number!", state.c_str());
23 return;
24 }
25 auto call = this->number_->make_call();
27 call.perform();
28 });
29 this->number_->add_on_state_callback([this](float state) { this->publish_state(state); });
30}
31
33 ESP_LOGCONFIG(TAG, "MQTT Number '%s':", this->number_->get_name().c_str());
34 LOG_MQTT_COMPONENT(true, false)
35}
36
37std::string MQTTNumberComponent::component_type() const { return "number"; }
38const EntityBase *MQTTNumberComponent::get_entity() const { return this->number_; }
39
41 const auto &traits = number_->traits;
42 // https://www.home-assistant.io/integrations/number.mqtt/
43 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
44 root[MQTT_MIN] = traits.get_min_value();
45 root[MQTT_MAX] = traits.get_max_value();
46 root[MQTT_STEP] = traits.get_step();
47 if (!this->number_->traits.get_unit_of_measurement().empty())
49 switch (this->number_->traits.get_mode()) {
51 break;
52 case NUMBER_MODE_BOX:
53 root[MQTT_MODE] = "box";
54 break;
56 root[MQTT_MODE] = "slider";
57 break;
58 }
59 if (!this->number_->traits.get_device_class().empty())
61
62 config.command_topic = true;
63}
65 if (this->number_->has_state()) {
66 return this->publish_state(this->number_->state);
67 } else {
68 return true;
69 }
70}
72 char buffer[64];
73 snprintf(buffer, sizeof(buffer), "%f", value);
74 return this->publish(this->get_state_topic_(), buffer);
75}
76
77} // namespace mqtt
78} // namespace esphome
79
80#endif
81#endif // USE_MQTT
std::string get_device_class()
Get the device class, using the manual override if set.
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
const StringRef & get_name() const
bool has_state() const
Definition entity_base.h:78
constexpr const char * c_str() const
Definition string_ref.h:69
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.
const EntityBase * get_entity() const override
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
std::string component_type() const override
Override for MQTTComponent, returns "number".
NumberCall & set_value(float value)
Base-class for all numbers.
Definition number.h:39
NumberCall make_call()
Definition number.h:45
NumberTraits traits
Definition number.h:49
void add_on_state_callback(std::function< void(float)> &&callback)
Definition number.cpp:16
NumberMode get_mode() const
bool state
Definition fan.h:0
mopeka_std_values val[4]
constexpr const char *const MQTT_MODE
Definition mqtt_const.h:116
constexpr const char *const MQTT_UNIT_OF_MEASUREMENT
Definition mqtt_const.h:264
constexpr const char *const MQTT_MIN
Definition mqtt_const.h:112
constexpr const char *const MQTT_STEP
Definition mqtt_const.h:226
constexpr const char *const MQTT_DEVICE_CLASS
Definition mqtt_const.h:58
constexpr const char *const MQTT_MAX
Definition mqtt_const.h:108
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:291
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.