ESPHome 2025.5.0
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 root[MQTT_MIN] = traits.get_min_value();
44 root[MQTT_MAX] = traits.get_max_value();
45 root[MQTT_STEP] = traits.get_step();
46 if (!this->number_->traits.get_unit_of_measurement().empty())
48 switch (this->number_->traits.get_mode()) {
50 break;
51 case NUMBER_MODE_BOX:
52 root[MQTT_MODE] = "box";
53 break;
55 root[MQTT_MODE] = "slider";
56 break;
57 }
58 if (!this->number_->traits.get_device_class().empty())
60
61 config.command_topic = true;
62}
64 if (this->number_->has_state()) {
65 return this->publish_state(this->number_->state);
66 } else {
67 return true;
68 }
69}
71 char buffer[64];
72 snprintf(buffer, sizeof(buffer), "%f", value);
73 return this->publish(this->get_state_topic_(), buffer);
74}
75
76} // namespace mqtt
77} // namespace esphome
78
79#endif
80#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
constexpr const char * c_str() const
Definition string_ref.h:68
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
bool has_state() const
Return whether this number has gotten a full state yet.
Definition number.h:52
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:313
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.