Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
platform_last_contact_sensor.cpp
Go to the documentation of this file.
1/// @file platform_last_contact_sensor.cpp
2/// @brief Diagnostic sensor exposing seconds elapsed since a device's last received frame.
3/// @ingroup hioc_platforms
4
6
7#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
9
10namespace esphome {
11namespace home_io_control {
12
13static const char *const TAG = "home_io_control.last_contact";
14
15namespace {
16
17constexpr float MS_PER_SECOND = 1000.0F;
18// How often the heartbeat re-publishes the age while the device stays quiet. Frame-driven
19// updates alone would freeze the entity's value at whatever it was the moment traffic stopped;
20// without this it can't count up in Home Assistant between frames.
21constexpr uint32_t HEARTBEAT_INTERVAL_MS = 60000;
22
23} // namespace
24
26 this->register_companion_binding_([this](const IoDevice &dev) { this->publish_age_(dev); });
27
28 this->set_interval("heartbeat", HEARTBEAT_INTERVAL_MS, [this]() {
29 if (this->parent_ == nullptr)
30 return;
31 if (const auto *dev = this->parent_->get_device(this->device_id_); dev != nullptr)
32 this->publish_age_(*dev);
33 });
34}
35
37 if (dev.last_seen_ms != 0)
38 this->publish_state(static_cast<float>(millis() - dev.last_seen_ms) / MS_PER_SECOND);
39}
40
42 LOG_SENSOR("", "IO-Homecontrol Last Contact", this);
43 ESP_LOGCONFIG(TAG, " Device ID: %s", this->device_id_.c_str());
44}
45
46} // namespace home_io_control
47} // namespace esphome
void register_companion_binding_(const std::function< void(const IoDevice &)> &publish)
Shared setup() body: subscribe to this device's updates and publish the initial state.
void dump_config() override
Dump sensor configuration to the log.
void publish_age_(const IoDevice &dev)
Compute and publish seconds since dev.last_seen_ms; no-op before the first frame.
void setup() override
Register the device-update subscription, start the heartbeat, and publish the initial cached state.
static constexpr const char * TAG
Diagnostic sensor exposing seconds elapsed since a device's last received frame.
Runtime state of a paired IO‑Homecontrol device.
uint32_t last_seen_ms
millis() of the last frame received from this device (any command), 0 = never.