mirror of
https://github.com/esphome/esphome.git
synced 2025-03-13 14:18:14 +00:00
[uptime] Add separator config for text_sensor
This commit is contained in:
parent
bfa3254d6c
commit
4503acf3dc
@ -7,13 +7,21 @@ uptime_ns = cg.esphome_ns.namespace("uptime")
|
||||
UptimeTextSensor = uptime_ns.class_(
|
||||
"UptimeTextSensor", text_sensor.TextSensor, cg.PollingComponent
|
||||
)
|
||||
CONFIG_SCHEMA = text_sensor.text_sensor_schema(
|
||||
UptimeTextSensor,
|
||||
icon=ICON_TIMER,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
).extend(cv.polling_component_schema("30s"))
|
||||
|
||||
CONF_SEPARATOR = "separator"
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
text_sensor.text_sensor_schema(
|
||||
UptimeTextSensor,
|
||||
icon=ICON_TIMER,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
)
|
||||
.extend({cv.Optional(CONF_SEPARATOR, default=""): cv.string})
|
||||
.extend(cv.polling_component_schema("30s"))
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await text_sensor.new_text_sensor(config)
|
||||
cg.add(var.set_separator(config[CONF_SEPARATOR]))
|
||||
await cg.register_component(var, config)
|
||||
|
@ -16,6 +16,12 @@ void UptimeTextSensor::setup() {
|
||||
this->update();
|
||||
}
|
||||
|
||||
void UptimeTextSensor::insert_buffer(std::string &buffer, const char *key, unsigned value) const {
|
||||
if (!buffer.empty() && !this->separator_.empty())
|
||||
buffer.insert(0, this->separator_);
|
||||
buffer.insert(0, str_sprintf("%u%s", value, key));
|
||||
}
|
||||
|
||||
void UptimeTextSensor::update() {
|
||||
auto now = millis();
|
||||
// get whole seconds since last update. Note that even if the millis count has overflowed between updates,
|
||||
@ -32,25 +38,25 @@ void UptimeTextSensor::update() {
|
||||
unsigned remainder = uptime % 60;
|
||||
uptime /= 60;
|
||||
if (interval < 30) {
|
||||
buffer.insert(0, str_sprintf("%us", remainder));
|
||||
insert_buffer(buffer, "s", remainder);
|
||||
if (uptime == 0)
|
||||
break;
|
||||
}
|
||||
remainder = uptime % 60;
|
||||
uptime /= 60;
|
||||
if (interval < 1800) {
|
||||
buffer.insert(0, str_sprintf("%um", remainder));
|
||||
insert_buffer(buffer, "m", remainder);
|
||||
if (uptime == 0)
|
||||
break;
|
||||
}
|
||||
remainder = uptime % 24;
|
||||
uptime /= 24;
|
||||
if (interval < 12 * 3600) {
|
||||
buffer.insert(0, str_sprintf("%uh", remainder));
|
||||
insert_buffer(buffer, "h", remainder);
|
||||
if (uptime == 0)
|
||||
break;
|
||||
}
|
||||
buffer.insert(0, str_sprintf("%ud", (unsigned) uptime));
|
||||
insert_buffer(buffer, "d", (unsigned) uptime);
|
||||
break;
|
||||
}
|
||||
this->publish_state(buffer);
|
||||
|
@ -13,10 +13,14 @@ class UptimeTextSensor : public text_sensor::TextSensor, public PollingComponent
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
void setup() override;
|
||||
void insert_buffer(std::string &buffer, const char *key, unsigned value) const;
|
||||
|
||||
void set_separator(const std::string &separator) { this->separator_ = separator; }
|
||||
|
||||
float get_setup_priority() const override;
|
||||
|
||||
protected:
|
||||
std::string separator_{};
|
||||
uint32_t uptime_{0}; // uptime in seconds, will overflow after 136 years
|
||||
uint32_t last_ms_{0};
|
||||
};
|
||||
|
@ -17,3 +17,7 @@ sensor:
|
||||
text_sensor:
|
||||
- platform: uptime
|
||||
name: Uptime Text
|
||||
- platform: uptime
|
||||
name: Uptime Text With Separator
|
||||
separator: "-"
|
||||
update_interval: 10s
|
||||
|
Loading…
x
Reference in New Issue
Block a user