ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
homeassistant_sensor.cpp
Go to the documentation of this file.
3#include "esphome/core/log.h"
5
7
8static const char *const TAG = "homeassistant.sensor";
9
12 auto val = parse_number<float>(state.c_str());
13 if (!val.has_value()) {
14 ESP_LOGW(TAG, "'%s': Can't convert '%s' to number!", this->entity_id_, state.c_str());
15 this->publish_state(NAN);
16 return;
17 }
18
19 if (this->attribute_ != nullptr) {
20 ESP_LOGV(TAG, "'%s::%s': Got attribute state %.2f", this->entity_id_, this->attribute_, *val);
21 } else {
22 ESP_LOGV(TAG, "'%s': Got state %.2f", this->entity_id_, *val);
23 }
24 this->publish_state(*val);
25 });
26}
28 LOG_SENSOR("", "Homeassistant Sensor", this);
29 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
30 if (this->attribute_ != nullptr) {
31 ESP_LOGCONFIG(TAG, " Attribute: '%s'", this->attribute_);
32 }
33}
35
36} // namespace esphome::homeassistant
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
constexpr const char * c_str() const
Definition string_ref.h:73
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(StringRef)> &&f)
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
mopeka_std_values val[3]
APIServer * global_api_server
constexpr float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.h:55
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1143