ESPHome 2025.12.1
Loading...
Searching...
No Matches
homeassistant_sensor.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
4
5namespace esphome {
6namespace homeassistant {
7
8static const char *const TAG = "homeassistant.sensor";
9
12 this->entity_id_, this->attribute_, [this](const std::string &state) {
14 if (!val.has_value()) {
15 ESP_LOGW(TAG, "'%s': Can't convert '%s' to number!", this->entity_id_, state.c_str());
16 this->publish_state(NAN);
17 return;
18 }
19
20 if (this->attribute_ != nullptr) {
21 ESP_LOGD(TAG, "'%s::%s': Got attribute state %.2f", this->entity_id_, this->attribute_, *val);
22 } else {
23 ESP_LOGD(TAG, "'%s': Got state %.2f", this->entity_id_, *val);
24 }
25 this->publish_state(*val);
26 });
27}
29 LOG_SENSOR("", "Homeassistant Sensor", this);
30 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
31 if (this->attribute_ != nullptr) {
32 ESP_LOGCONFIG(TAG, " Attribute: '%s'", this->attribute_);
33 }
34}
36
37} // namespace homeassistant
38} // namespace esphome
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(std::string)> f)
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:77
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:117
mopeka_std_values val[4]
APIServer * global_api_server
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.cpp:89
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:536