ESPHome  2024.12.2
homeassistant_number.cpp
Go to the documentation of this file.
1 #include "homeassistant_number.h"
2 
5 #include "esphome/core/log.h"
6 
7 namespace esphome {
8 namespace homeassistant {
9 
10 static const char *const TAG = "homeassistant.number";
11 
12 void HomeassistantNumber::state_changed_(const std::string &state) {
13  auto number_value = parse_number<float>(state);
14  if (!number_value.has_value()) {
15  ESP_LOGW(TAG, "'%s': Can't convert '%s' to number!", this->entity_id_.c_str(), state.c_str());
16  this->publish_state(NAN);
17  return;
18  }
19  if (this->state == number_value.value()) {
20  return;
21  }
22  ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_.c_str(), state.c_str());
23  this->publish_state(number_value.value());
24 }
25 
26 void HomeassistantNumber::min_retrieved_(const std::string &min) {
27  auto min_value = parse_number<float>(min);
28  if (!min_value.has_value()) {
29  ESP_LOGE(TAG, "'%s': Can't convert 'min' value '%s' to number!", this->entity_id_.c_str(), min.c_str());
30  return;
31  }
32  ESP_LOGD(TAG, "'%s': Min retrieved: %s", get_name().c_str(), min.c_str());
33  this->traits.set_min_value(min_value.value());
34 }
35 
36 void HomeassistantNumber::max_retrieved_(const std::string &max) {
37  auto max_value = parse_number<float>(max);
38  if (!max_value.has_value()) {
39  ESP_LOGE(TAG, "'%s': Can't convert 'max' value '%s' to number!", this->entity_id_.c_str(), max.c_str());
40  return;
41  }
42  ESP_LOGD(TAG, "'%s': Max retrieved: %s", get_name().c_str(), max.c_str());
43  this->traits.set_max_value(max_value.value());
44 }
45 
46 void HomeassistantNumber::step_retrieved_(const std::string &step) {
47  auto step_value = parse_number<float>(step);
48  if (!step_value.has_value()) {
49  ESP_LOGE(TAG, "'%s': Can't convert 'step' value '%s' to number!", this->entity_id_.c_str(), step.c_str());
50  return;
51  }
52  ESP_LOGD(TAG, "'%s': Step Retrieved %s", get_name().c_str(), step.c_str());
53  this->traits.set_step(step_value.value());
54 }
55 
58  this->entity_id_, nullopt, std::bind(&HomeassistantNumber::state_changed_, this, std::placeholders::_1));
59 
61  this->entity_id_, optional<std::string>("min"),
62  std::bind(&HomeassistantNumber::min_retrieved_, this, std::placeholders::_1));
64  this->entity_id_, optional<std::string>("max"),
65  std::bind(&HomeassistantNumber::max_retrieved_, this, std::placeholders::_1));
67  this->entity_id_, optional<std::string>("step"),
68  std::bind(&HomeassistantNumber::step_retrieved_, this, std::placeholders::_1));
69 }
70 
72  LOG_NUMBER("", "Homeassistant Number", this);
73  ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
74 }
75 
77 
78 void HomeassistantNumber::control(float value) {
80  ESP_LOGE(TAG, "No clients connected to API server");
81  return;
82  }
83 
84  this->publish_state(value);
85 
87  resp.service = "number.set_value";
88 
90  entity_id.key = "entity_id";
91  entity_id.value = this->entity_id_;
92  resp.data.push_back(entity_id);
93 
94  api::HomeassistantServiceMap entity_value;
95  entity_value.key = "value";
96  entity_value.value = to_string(value);
97  resp.data.push_back(entity_value);
98 
100 }
101 
102 } // namespace homeassistant
103 } // namespace esphome
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition: component.cpp:27
void publish_state(float state)
Definition: number.cpp:9
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
Definition: api_server.cpp:350
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition: util.cpp:15
const nullopt_t nullopt((nullopt_t::init()))
void set_min_value(float min_value)
Definition: number_traits.h:18
void set_max_value(float max_value)
Definition: number_traits.h:20
NumberTraits traits
Definition: number.h:49
constexpr const char * c_str() const
Definition: string_ref.h:68
std::string to_string(int value)
Definition: helpers.cpp:81
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
Definition: api_server.cpp:357
void get_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
Definition: api_server.cpp:366
std::vector< HomeassistantServiceMap > data
Definition: api_pb2.h:817
APIServer * global_api_server
Definition: api_server.cpp:347
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool state
Definition: fan.h:34