mirror of
https://github.com/esphome/esphome.git
synced 2025-03-13 14:18:14 +00:00
more suggested changes
This commit is contained in:
parent
dbd396a115
commit
a6f33d730b
@ -1,11 +1,18 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import text_sensor, time
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_TIME_ID, ENTITY_CATEGORY_DIAGNOSTIC, ICON_TIMER
|
||||
from esphome.const import (
|
||||
CONF_DURATION,
|
||||
CONF_TIME_ID,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
ICON_TIMER,
|
||||
)
|
||||
|
||||
CONF_TIMESTAMP = "timestamp"
|
||||
|
||||
uptime_ns = cg.esphome_ns.namespace("uptime")
|
||||
UptimeSecondsTextSensor = uptime_ns.class_(
|
||||
"UptimeSecondsTextSensor", text_sensor.TextSensor, cg.PollingComponent
|
||||
UptimeDurationTextSensor = uptime_ns.class_(
|
||||
"UptimeDurationTextSensor", text_sensor.TextSensor, cg.PollingComponent
|
||||
)
|
||||
UptimeTimestampTextSensor = uptime_ns.class_(
|
||||
"UptimeTimestampTextSensor", text_sensor.TextSensor, cg.Component
|
||||
@ -13,7 +20,7 @@ UptimeTimestampTextSensor = uptime_ns.class_(
|
||||
|
||||
|
||||
CONFIG_SCHEMA = text_sensor.text_sensor_schema(
|
||||
UptimeSecondsTextSensor,
|
||||
UptimeDurationTextSensor,
|
||||
icon=ICON_TIMER,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
).extend(cv.polling_component_schema("30s"))
|
||||
@ -21,12 +28,12 @@ CONFIG_SCHEMA = text_sensor.text_sensor_schema(
|
||||
|
||||
CONFIG_SCHEMA = cv.typed_schema(
|
||||
{
|
||||
"seconds": text_sensor.text_sensor_schema(
|
||||
UptimeSecondsTextSensor,
|
||||
CONF_DURATION: text_sensor.text_sensor_schema(
|
||||
UptimeDurationTextSensor,
|
||||
icon=ICON_TIMER,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
).extend(cv.polling_component_schema("30s")),
|
||||
"timestamp": text_sensor.text_sensor_schema(
|
||||
CONF_TIMESTAMP: text_sensor.text_sensor_schema(
|
||||
UptimeTimestampTextSensor,
|
||||
icon=ICON_TIMER,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "uptime_seconds_text_sensor.h"
|
||||
#include "uptime_duration_text_sensor.h"
|
||||
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
@ -9,14 +9,14 @@ namespace uptime {
|
||||
|
||||
static const char *const TAG = "uptime.sensor";
|
||||
|
||||
void UptimeSecondsTextSensor::setup() {
|
||||
void UptimeDurationTextSensor::setup() {
|
||||
this->last_ms_ = millis();
|
||||
if (this->last_ms_ < 60 * 1000)
|
||||
this->last_ms_ = 0;
|
||||
this->update();
|
||||
}
|
||||
|
||||
void UptimeSecondsTextSensor::update() {
|
||||
void UptimeDurationTextSensor::update() {
|
||||
auto now = millis();
|
||||
// get whole seconds since last update. Note that even if the millis count has overflowed between updates,
|
||||
// the difference will still be correct due to the way twos-complement arithmetic works.
|
||||
@ -56,8 +56,8 @@ void UptimeSecondsTextSensor::update() {
|
||||
this->publish_state(buffer);
|
||||
}
|
||||
|
||||
float UptimeSecondsTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
void UptimeSecondsTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Uptime Text Sensor", this); }
|
||||
float UptimeDurationTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
void UptimeDurationTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Uptime Text Sensor", this); }
|
||||
|
||||
} // namespace uptime
|
||||
} // namespace esphome
|
@ -8,7 +8,7 @@
|
||||
namespace esphome {
|
||||
namespace uptime {
|
||||
|
||||
class UptimeSecondsTextSensor : public text_sensor::TextSensor, public PollingComponent {
|
||||
class UptimeDurationTextSensor : public text_sensor::TextSensor, public PollingComponent {
|
||||
public:
|
||||
void update() override;
|
||||
void dump_config() override;
|
@ -21,7 +21,7 @@ class UptimeTimestampTextSensor : public text_sensor::TextSensor, public Compone
|
||||
void set_time(time::RealTimeClock *time) { this->time_ = time; }
|
||||
|
||||
protected:
|
||||
time::RealTimeClock *time_;
|
||||
time::RealTimeClock *time_{};
|
||||
};
|
||||
|
||||
} // namespace uptime
|
||||
|
Loading…
x
Reference in New Issue
Block a user