Skip to content

Commit

Permalink
remove static dependencies to sensor, binary_sensor, text_sensor (m3_…
Browse files Browse the repository at this point in the history
…vedirect)
  • Loading branch information
krahabb committed Nov 17, 2024
1 parent e481cd6 commit f0af858
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
3 changes: 1 addition & 2 deletions esphome/components/m3_vedirect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from . import ve_reg

CODEOWNERS = ["@krahabb"]
DEPENDENCIES = ["binary_sensor", "sensor", "text_sensor", "uart"]
AUTO_LOAD = ["binary_sensor", "sensor", "text_sensor"]
DEPENDENCIES = ["uart"]
MULTI_CONF = True


Expand Down
12 changes: 10 additions & 2 deletions esphome/components/m3_vedirect/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ void Manager::setup() {

void Manager::loop() {
const uint32_t millis_ = millis();

#ifdef USE_SENSOR
if (this->run_time_) {
float run_time = millis_ / 1000;
if (run_time != this->run_time_->raw_state)
this->run_time_->publish_state(run_time);
}

#endif
auto available = this->available();
if (!available) {
if (this->connected_ && ((millis_ - this->millis_last_rx_) > VEDIRECT_TIMEOUT_MILLIS)) {
Expand Down Expand Up @@ -117,9 +117,11 @@ void Manager::send_hexframe(const char *rawframe, bool addchecksum) {
void Manager::on_connected_() {
ESP_LOGD(this->logtag_, "LINK: connected");
this->connected_ = true;
#ifdef USE_BINARY_SENSOR
if (auto link_connected = this->link_connected_) {
link_connected->publish_state(true);
}
#endif
if (this->auto_create_hex_entities_ || this->hex_registers_.size()) {
this->send_hexframe(HexFrame_Command(HEXFRAME::COMMAND::Ping));
this->millis_last_ping_tx_ = this->millis_last_hexframe_tx_;
Expand All @@ -130,9 +132,11 @@ void Manager::on_disconnected_() {
ESP_LOGD(this->logtag_, "LINK: disconnected");
this->connected_ = false;
this->reset(); // cleanup the frame handler
#ifdef USE_BINARY_SENSOR
if (auto link_connected = this->link_connected_) {
link_connected->publish_state(false);
}
#endif
for (auto &pair : this->text_entities_) {
pair.second->link_disconnected_();
}
Expand All @@ -148,8 +152,10 @@ void Manager::on_frame_hex_(const RxHexFrame &hexframe) {

this->hexframe_callback_.call(hexframe);

#ifdef USE_TEXT_SENSOR
if (this->rawhexframe_)
this->rawhexframe_->publish_state(std::string(hexframe.encoded()));
#endif

this->millis_last_hexframe_rx_ = this->millis_last_rx_;
switch (hexframe.command()) {
Expand All @@ -175,6 +181,7 @@ void Manager::on_frame_text_(TextRecord **text_records, uint8_t text_records_cou

this->millis_last_textframe_rx_ = this->millis_last_rx_;

#ifdef USE_TEXT_SENSOR
if (auto rawtextframe = this->rawtextframe_) {
std::string textframe_value;
textframe_value.reserve(text_records_count * sizeof(FrameHandler::TextRecord));
Expand All @@ -189,6 +196,7 @@ void Manager::on_frame_text_(TextRecord **text_records, uint8_t text_records_cou
rawtextframe->publish_state(textframe_value);
}
}
#endif

for (uint8_t i = 0; i < text_records_count; ++i) {
const TextRecord *text_record = text_records[i];
Expand Down
26 changes: 20 additions & 6 deletions esphome/components/m3_vedirect/manager.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#pragma once
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"

#ifdef USE_BINARY_SENSOR
#include "esphome/components/binary_sensor/binary_sensor.h"
#endif
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif
#ifdef USE_TEXT_SENSOR
#include "esphome/components/text_sensor/text_sensor.h"
#endif

#include "defines.h"
#include "ve_hexframe.h"

Expand All @@ -27,12 +34,19 @@ namespace m3_vedirect {
}

class Manager : public uart::UARTDevice, public Component, protected FrameHandler {
// dedicated entities to manage component state/behavior
MANAGER_ENTITY_(text_sensor::TextSensor, rawhexframe)
MANAGER_ENTITY_(text_sensor::TextSensor, rawtextframe)
// dedicated entities to manage component state/behavior
#ifdef USE_BINARY_SENSOR
MANAGER_ENTITY_(binary_sensor::BinarySensor, link_connected)
#endif
#ifdef USE_SENSOR
MANAGER_ENTITY_(sensor::Sensor, run_time)
#endif
#ifdef USE_TEXT_SENSOR
MANAGER_ENTITY_(text_sensor::TextSensor, rawhexframe)
MANAGER_ENTITY_(text_sensor::TextSensor, rawtextframe)
#endif

public:
const char *get_vedirect_id() { return this->vedirect_id_; }
void set_vedirect_id(const char *vedirect_id) { this->vedirect_id_ = vedirect_id; }
const char *get_vedirect_name() { return this->vedirect_name_; }
Expand Down

0 comments on commit f0af858

Please sign in to comment.