ESPHome  2024.11.1
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 
9 namespace esphome {
10 namespace mqtt {
11 
12 static const char *const TAG = "mqtt.number";
13 
14 using namespace esphome::number;
15 
16 MQTTNumberComponent::MQTTNumberComponent(Number *number) : number_(number) {}
17 
19  this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &state) {
20  auto val = parse_number<float>(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();
26  call.set_value(*val);
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 
37 std::string MQTTNumberComponent::component_type() const { return "number"; }
38 const 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()) {
49  case NUMBER_MODE_AUTO:
50  break;
51  case NUMBER_MODE_BOX:
52  root[MQTT_MODE] = "box";
53  break;
54  case NUMBER_MODE_SLIDER:
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
constexpr const char *const MQTT_MIN
Definition: mqtt_const.h:110
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
std::string get_device_class()
Get the device class, using the manual override if set.
Definition: entity_base.cpp:78
void setup() override
Override setup.
Definition: mqtt_number.cpp:18
mopeka_std_values val[4]
bool command_topic
If the command topic should be included. Default to true.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition: mqtt_number.cpp:40
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
constexpr const char *const MQTT_STEP
Definition: mqtt_const.h:224
MQTTNumberComponent(number::Number *number)
Construct this MQTTNumberComponent instance with the provided friendly_name and number.
Definition: mqtt_number.cpp:16
Base-class for all numbers.
Definition: number.h:39
constexpr const char *const MQTT_UNIT_OF_MEASUREMENT
Definition: mqtt_const.h:262
Simple Helper struct used for Home Assistant MQTT send_discovery().
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
Definition: entity_base.cpp:87
std::string component_type() const override
Override for MQTTComponent, returns "number".
Definition: mqtt_number.cpp:37
constexpr const char *const MQTT_MAX
Definition: mqtt_const.h:106
constexpr const char *const MQTT_MODE
Definition: mqtt_const.h:114
NumberTraits traits
Definition: number.h:49
constexpr const char * c_str() const
Definition: string_ref.h:68
NumberCall make_call()
Definition: number.h:45
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
NumberMode get_mode() const
Definition: number_traits.h:29
constexpr const char *const MQTT_DEVICE_CLASS
Definition: mqtt_const.h:58
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
const EntityBase * get_entity() const override
Definition: mqtt_number.cpp:38
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool state
Definition: fan.h:34