ESPHome  2024.12.2
homeassistant_sensor.cpp
Go to the documentation of this file.
1 #include "homeassistant_sensor.h"
2 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace homeassistant {
7 
8 static const char *const TAG = "homeassistant.sensor";
9 
12  this->entity_id_, this->attribute_, [this](const std::string &state) {
13  auto val = parse_number<float>(state);
14  if (!val.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 
20  if (this->attribute_.has_value()) {
21  ESP_LOGD(TAG, "'%s::%s': Got attribute state %.2f", this->entity_id_.c_str(),
22  this->attribute_.value().c_str(), *val);
23  } else {
24  ESP_LOGD(TAG, "'%s': Got state %.2f", this->entity_id_.c_str(), *val);
25  }
26  this->publish_state(*val);
27  });
28 }
30  LOG_SENSOR("", "Homeassistant Sensor", this);
31  ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
32  if (this->attribute_.has_value()) {
33  ESP_LOGCONFIG(TAG, " Attribute: '%s'", this->attribute_.value().c_str());
34  }
35 }
37 
38 } // namespace homeassistant
39 } // namespace esphome
value_type const & value() const
Definition: optional.h:89
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition: component.cpp:27
mopeka_std_values val[4]
bool has_value() const
Definition: optional.h:87
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:131
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
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
APIServer * global_api_server
Definition: api_server.cpp:347