ESPHome 2026.11.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
12ESPHOME_LOG_TAG(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->command_topic_source_(), [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
35void MQTTNumberComponent::dump_config() { LOG_MQTT_COMPONENT(true, false); }
36
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 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
48 const auto unit_of_measurement = this->number_->get_unit_of_measurement_ref();
49 if (!unit_of_measurement.empty()) {
50 root[MQTT_UNIT_OF_MEASUREMENT] = unit_of_measurement;
51 }
52 const auto mode = this->number_->traits.get_mode();
53 if (mode != NUMBER_MODE_AUTO) {
54 root[MQTT_MODE] =
55 NumberMqttModeStrings::get_progmem_str(static_cast<uint8_t>(mode), static_cast<uint8_t>(NUMBER_MODE_BOX));
56 }
57 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
58
59 config.command_topic = true;
60}
62 if (this->number_->has_state()) {
63 return this->publish_state(this->number_->state);
64 } else {
65 return true;
66 }
67}
69 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
70 char buffer[64];
71 size_t len = buf_append_printf(buffer, sizeof(buffer), 0, "%f", value);
72 return this->publish(this->get_state_topic_to_(topic_buf), buffer, len);
73}
74
75} // namespace esphome::mqtt
76
77#endif
78#endif // USE_MQTT
BedjetMode mode
BedJet operating mode.
StringRef get_unit_of_measurement_ref() const
bool has_state() const
void subscribe(T &&topic, F &&callback, uint8_t qos=0)
Subscribe to a MQTT topic.
MQTTTopicSource command_topic_source_() const
The command topic as a subscription source. A lambda topic is read once and copied,...
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).
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
void add_on_state_callback(F &&callback)
Definition number.h:37
NumberTraits traits
Definition number.h:41
NumberMode get_mode() const
bool state
Definition fan.h:2
uint32_t buffer
Definition gif.cpp:50
mopeka_std_values val[3]
PROGMEM_STRING_TABLE(AlarmControlPanelStateStrings, "DISARMED", "ARMED_HOME", "ARMED_AWAY", "ARMED_NIGHT", "ARMED_VACATION", "ARMED_CUSTOM_BYPASS", "PENDING", "ARMING", "DISARMING", "TRIGGERED", "UNKNOWN")
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
const void size_t len
Definition hal.h:68
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1352
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.