2019-05-27 13:06:45 +02:00
|
|
|
#include "uptime_sensor.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
#include "esphome/core/log.h"
|
|
|
|
#include "esphome/core/helpers.h"
|
2021-09-20 11:47:51 +02:00
|
|
|
#include "esphome/core/hal.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace uptime {
|
|
|
|
|
2021-06-10 22:19:44 +02:00
|
|
|
static const char *const TAG = "uptime.sensor";
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
void UptimeSensor::update() {
|
|
|
|
const uint32_t ms = millis();
|
|
|
|
const uint64_t ms_mask = (1ULL << 32) - 1ULL;
|
|
|
|
const uint32_t last_ms = this->uptime_ & ms_mask;
|
|
|
|
if (ms < last_ms) {
|
|
|
|
this->uptime_ += ms_mask + 1ULL;
|
|
|
|
ESP_LOGD(TAG, "Detected roll-over \xf0\x9f\xa6\x84");
|
|
|
|
}
|
|
|
|
this->uptime_ &= ~ms_mask;
|
|
|
|
this->uptime_ |= ms;
|
|
|
|
|
|
|
|
// Do separate second and milliseconds conversion to avoid floating point division errors
|
|
|
|
// Probably some IEEE standard already guarantees this division can be done without loss
|
|
|
|
// of precision in a single division, but let's do it like this to be sure.
|
|
|
|
const uint64_t seconds_int = this->uptime_ / 1000ULL;
|
|
|
|
const float seconds = float(seconds_int) + (this->uptime_ % 1000ULL) / 1000.0f;
|
|
|
|
this->publish_state(seconds);
|
|
|
|
}
|
|
|
|
std::string UptimeSensor::unique_id() { return get_mac_address() + "-uptime"; }
|
|
|
|
float UptimeSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
|
2019-10-23 14:43:41 +02:00
|
|
|
void UptimeSensor::dump_config() { LOG_SENSOR("", "Uptime Sensor", this); }
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
} // namespace uptime
|
|
|
|
} // namespace esphome
|