ESPHome  2022.5.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 
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 buffer[64];
70  snprintf(buffer, sizeof(buffer), "%f", value);
71  return this->publish(this->get_state_topic_(), buffer);
72 }
73 
74 } // namespace mqtt
75 } // namespace esphome
76 
77 #endif
78 #endif // USE_MQTT
constexpr const char *const MQTT_MIN
Definition: mqtt_const.h:97
bool has_state() const
Return whether this number has gotten a full state yet.
Definition: number.h:42
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
void add_on_state_callback(std::function< void(float)> &&callback)
Definition: number.cpp:16
void setup() override
Override setup.
Definition: mqtt_number.cpp:18
const std::string & get_name() const
Definition: entity_base.cpp:11
constexpr const char *const MQTT_MODE
Definition: mqtt_const.h:521
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:211
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:29
constexpr const char *const MQTT_UNIT_OF_MEASUREMENT
Definition: mqtt_const.h:244
Simple Helper struct used for Home Assistant MQTT send_discovery().
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:96
NumberTraits traits
Definition: number.h:39
NumberCall make_call()
Definition: number.h:35
Definition: a4988.cpp:4
uint32_t val
Definition: datatypes.h:79
NumberMode get_mode() const
Definition: number_traits.h:33
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.
bool state
Definition: fan.h:41