1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-15 15:18:16 +00:00

more suggested changes

This commit is contained in:
rfdarter 2025-02-25 00:41:00 +01:00
parent dbd396a115
commit a6f33d730b
4 changed files with 21 additions and 14 deletions

View File

@ -1,11 +1,18 @@
import esphome.codegen as cg import esphome.codegen as cg
from esphome.components import text_sensor, time from esphome.components import text_sensor, time
import esphome.config_validation as cv 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") uptime_ns = cg.esphome_ns.namespace("uptime")
UptimeSecondsTextSensor = uptime_ns.class_( UptimeDurationTextSensor = uptime_ns.class_(
"UptimeSecondsTextSensor", text_sensor.TextSensor, cg.PollingComponent "UptimeDurationTextSensor", text_sensor.TextSensor, cg.PollingComponent
) )
UptimeTimestampTextSensor = uptime_ns.class_( UptimeTimestampTextSensor = uptime_ns.class_(
"UptimeTimestampTextSensor", text_sensor.TextSensor, cg.Component "UptimeTimestampTextSensor", text_sensor.TextSensor, cg.Component
@ -13,7 +20,7 @@ UptimeTimestampTextSensor = uptime_ns.class_(
CONFIG_SCHEMA = text_sensor.text_sensor_schema( CONFIG_SCHEMA = text_sensor.text_sensor_schema(
UptimeSecondsTextSensor, UptimeDurationTextSensor,
icon=ICON_TIMER, icon=ICON_TIMER,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
).extend(cv.polling_component_schema("30s")) ).extend(cv.polling_component_schema("30s"))
@ -21,12 +28,12 @@ CONFIG_SCHEMA = text_sensor.text_sensor_schema(
CONFIG_SCHEMA = cv.typed_schema( CONFIG_SCHEMA = cv.typed_schema(
{ {
"seconds": text_sensor.text_sensor_schema( CONF_DURATION: text_sensor.text_sensor_schema(
UptimeSecondsTextSensor, UptimeDurationTextSensor,
icon=ICON_TIMER, icon=ICON_TIMER,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
).extend(cv.polling_component_schema("30s")), ).extend(cv.polling_component_schema("30s")),
"timestamp": text_sensor.text_sensor_schema( CONF_TIMESTAMP: text_sensor.text_sensor_schema(
UptimeTimestampTextSensor, UptimeTimestampTextSensor,
icon=ICON_TIMER, icon=ICON_TIMER,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=ENTITY_CATEGORY_DIAGNOSTIC,

View File

@ -1,4 +1,4 @@
#include "uptime_seconds_text_sensor.h" #include "uptime_duration_text_sensor.h"
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
@ -9,14 +9,14 @@ namespace uptime {
static const char *const TAG = "uptime.sensor"; static const char *const TAG = "uptime.sensor";
void UptimeSecondsTextSensor::setup() { void UptimeDurationTextSensor::setup() {
this->last_ms_ = millis(); this->last_ms_ = millis();
if (this->last_ms_ < 60 * 1000) if (this->last_ms_ < 60 * 1000)
this->last_ms_ = 0; this->last_ms_ = 0;
this->update(); this->update();
} }
void UptimeSecondsTextSensor::update() { void UptimeDurationTextSensor::update() {
auto now = millis(); auto now = millis();
// get whole seconds since last update. Note that even if the millis count has overflowed between updates, // 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. // 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); this->publish_state(buffer);
} }
float UptimeSecondsTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; } float UptimeDurationTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
void UptimeSecondsTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Uptime Text Sensor", this); } void UptimeDurationTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Uptime Text Sensor", this); }
} // namespace uptime } // namespace uptime
} // namespace esphome } // namespace esphome

View File

@ -8,7 +8,7 @@
namespace esphome { namespace esphome {
namespace uptime { namespace uptime {
class UptimeSecondsTextSensor : public text_sensor::TextSensor, public PollingComponent { class UptimeDurationTextSensor : public text_sensor::TextSensor, public PollingComponent {
public: public:
void update() override; void update() override;
void dump_config() override; void dump_config() override;

View File

@ -21,7 +21,7 @@ class UptimeTimestampTextSensor : public text_sensor::TextSensor, public Compone
void set_time(time::RealTimeClock *time) { this->time_ = time; } void set_time(time::RealTimeClock *time) { this->time_ = time; }
protected: protected:
time::RealTimeClock *time_; time::RealTimeClock *time_{};
}; };
} // namespace uptime } // namespace uptime