mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-19 20:53:26 +02:00
8e75980ebd
* Cleanup dashboard JS * Add vscode * Save start_mark/end_mark * Updates * Updates * Remove need for cv.nameable It's a bit hacky but removes so much bloat from integrations * Add enum helper * Document APIs, and Improvements * Fixes * Fixes * Update PULL_REQUEST_TEMPLATE.md * Updates * Updates * Updates
31 lines
962 B
C++
31 lines
962 B
C++
#include "homeassistant_sensor.h"
|
|
#include "esphome/core/log.h"
|
|
#include "esphome/components/api/api_server.h"
|
|
|
|
namespace esphome {
|
|
namespace homeassistant {
|
|
|
|
static const char *TAG = "homeassistant.sensor";
|
|
|
|
void HomeassistantSensor::setup() {
|
|
api::global_api_server->subscribe_home_assistant_state(this->entity_id_, [this](std::string state) {
|
|
auto val = parse_float(state);
|
|
if (!val.has_value()) {
|
|
ESP_LOGW(TAG, "Can't convert '%s' to number!", state.c_str());
|
|
this->publish_state(NAN);
|
|
return;
|
|
}
|
|
|
|
ESP_LOGD(TAG, "'%s': Got state %.2f", this->entity_id_.c_str(), *val);
|
|
this->publish_state(*val);
|
|
});
|
|
}
|
|
void HomeassistantSensor::dump_config() {
|
|
LOG_SENSOR("", "Homeassistant Sensor", this);
|
|
ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
|
|
}
|
|
float HomeassistantSensor::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; }
|
|
|
|
} // namespace homeassistant
|
|
} // namespace esphome
|