mirror of
https://github.com/esphome/esphome.git
synced 2025-03-13 14:18:14 +00:00
init
This commit is contained in:
parent
422fb8f1a5
commit
fe066baa73
@ -1,19 +1,55 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import text_sensor
|
||||
from esphome.components import text_sensor, time
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import ENTITY_CATEGORY_DIAGNOSTIC, ICON_TIMER
|
||||
from esphome.const import CONF_TIME_ID, ENTITY_CATEGORY_DIAGNOSTIC, ICON_TIMER
|
||||
|
||||
uptime_ns = cg.esphome_ns.namespace("uptime")
|
||||
UptimeTextSensor = uptime_ns.class_(
|
||||
"UptimeTextSensor", text_sensor.TextSensor, cg.PollingComponent
|
||||
UptimeSecondsTextSensor = uptime_ns.class_(
|
||||
"UptimeSecondsTextSensor", text_sensor.TextSensor, cg.PollingComponent
|
||||
)
|
||||
UptimeTimestampTextSensor = uptime_ns.class_(
|
||||
"UptimeTimestampTextSensor", text_sensor.TextSensor, cg.Component
|
||||
)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = text_sensor.text_sensor_schema(
|
||||
UptimeTextSensor,
|
||||
UptimeSecondsTextSensor,
|
||||
icon=ICON_TIMER,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
).extend(cv.polling_component_schema("30s"))
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.typed_schema(
|
||||
{
|
||||
"seconds": text_sensor.text_sensor_schema(
|
||||
UptimeSecondsTextSensor,
|
||||
icon=ICON_TIMER,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
).extend(cv.polling_component_schema("30s")),
|
||||
"timestamp": text_sensor.text_sensor_schema(
|
||||
UptimeTimestampTextSensor,
|
||||
icon=ICON_TIMER,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
)
|
||||
.extend(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(CONF_TIME_ID): cv.All(
|
||||
cv.requires_component("time"), cv.use_id(time.RealTimeClock)
|
||||
),
|
||||
}
|
||||
)
|
||||
)
|
||||
.extend(cv.COMPONENT_SCHEMA),
|
||||
},
|
||||
default_type="seconds",
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await text_sensor.new_text_sensor(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if time_id_config := config.get(CONF_TIME_ID):
|
||||
time_id = await cg.get_variable(time_id_config)
|
||||
cg.add(var.set_time(time_id))
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "uptime_text_sensor.h"
|
||||
#include "uptime_seconds_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 UptimeTextSensor::setup() {
|
||||
void UptimeSecondsTextSensor::setup() {
|
||||
this->last_ms_ = millis();
|
||||
if (this->last_ms_ < 60 * 1000)
|
||||
this->last_ms_ = 0;
|
||||
this->update();
|
||||
}
|
||||
|
||||
void UptimeTextSensor::update() {
|
||||
void UptimeSecondsTextSensor::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 UptimeTextSensor::update() {
|
||||
this->publish_state(buffer);
|
||||
}
|
||||
|
||||
float UptimeTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
void UptimeTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Uptime Text Sensor", this); }
|
||||
float UptimeSecondsTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
void UptimeSecondsTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Uptime Text Sensor", this); }
|
||||
|
||||
} // namespace uptime
|
||||
} // namespace esphome
|
@ -8,7 +8,7 @@
|
||||
namespace esphome {
|
||||
namespace uptime {
|
||||
|
||||
class UptimeTextSensor : public text_sensor::TextSensor, public PollingComponent {
|
||||
class UptimeSecondsTextSensor : public text_sensor::TextSensor, public PollingComponent {
|
||||
public:
|
||||
void update() override;
|
||||
void dump_config() override;
|
@ -0,0 +1,36 @@
|
||||
#include "uptime_timestamp_text_sensor.h"
|
||||
|
||||
#ifdef USE_TIME
|
||||
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace uptime {
|
||||
|
||||
static const char *const TAG = "uptime.sensor";
|
||||
|
||||
void UptimeTimestampTextSensor::setup() {
|
||||
this->time_->add_on_time_sync_callback([this]() {
|
||||
if (this->has_state_)
|
||||
return; // No need to update the timestamp if it's already set
|
||||
|
||||
auto now = this->time_->now();
|
||||
const uint32_t ms = millis();
|
||||
if (!now.is_valid())
|
||||
return; // No need to update the timestamp if the time is not valid
|
||||
|
||||
std::string now_string = this->time_->now().strftime("%F %T");
|
||||
|
||||
this->publish_state(now_string);
|
||||
});
|
||||
}
|
||||
|
||||
float UptimeTimestampTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
void UptimeTimestampTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Uptime Text Sensor", this); }
|
||||
|
||||
} // namespace uptime
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_TIME
|
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#ifdef USE_TIME
|
||||
|
||||
#include "esphome/components/text_sensor/text_sensor.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/time/real_time_clock.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace uptime {
|
||||
|
||||
class UptimeTimestampTextSensor : public text_sensor::TextSensor, public Component {
|
||||
public:
|
||||
void dump_config() override;
|
||||
void setup() override;
|
||||
|
||||
float get_setup_priority() const override;
|
||||
|
||||
void set_time(time::RealTimeClock *time) { this->time_ = time; }
|
||||
|
||||
protected:
|
||||
time::RealTimeClock *time_;
|
||||
};
|
||||
|
||||
} // namespace uptime
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_TIME
|
Loading…
x
Reference in New Issue
Block a user