ESPHome  2024.10.2
appliance_base.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ARDUINO
4 
5 // MideaUART
6 #include <Appliance/ApplianceBase.h>
7 #include <Helpers/Logger.h>
8 
9 // Include global defines
10 #include "esphome/core/defines.h"
11 
12 #include "esphome/core/component.h"
13 #include "esphome/core/log.h"
16 #include "ir_transmitter.h"
17 
18 namespace esphome {
19 namespace midea {
20 
21 /* Stream from UART component */
22 class UARTStream : public Stream {
23  public:
24  void set_uart(uart::UARTComponent *uart) { this->uart_ = uart; }
25 
26  /* Stream interface implementation */
27 
28  int available() override { return this->uart_->available(); }
29  int read() override {
30  uint8_t data;
31  this->uart_->read_byte(&data);
32  return data;
33  }
34  int peek() override {
35  uint8_t data;
36  this->uart_->peek_byte(&data);
37  return data;
38  }
39  size_t write(uint8_t data) override {
40  this->uart_->write_byte(data);
41  return 1;
42  }
43  size_t write(const uint8_t *data, size_t size) override {
44  this->uart_->write_array(data, size);
45  return size;
46  }
47  void flush() override { this->uart_->flush(); }
48 
49  protected:
51 };
52 
53 template<typename T> class ApplianceBase : public Component {
54  static_assert(std::is_base_of<dudanov::midea::ApplianceBase, T>::value,
55  "T must derive from dudanov::midea::ApplianceBase class");
56 
57  public:
59  this->base_.setStream(&this->stream_);
60  this->base_.addOnStateCallback(std::bind(&ApplianceBase::on_status_change, this));
61  dudanov::midea::ApplianceBase::setLogger(
62  [](int level, const char *tag, int line, const String &format, va_list args) {
63  esp_log_vprintf_(level, tag, line, format.c_str(), args);
64  });
65  }
66 
67 #ifdef USE_REMOTE_TRANSMITTER
68  void set_transmitter(RemoteTransmitterBase *transmitter) { this->transmitter_.set_transmitter(transmitter); }
69 #endif
70 
71  /* UART communication */
72 
73  void set_uart_parent(uart::UARTComponent *parent) { this->stream_.set_uart(parent); }
74  void set_period(uint32_t ms) { this->base_.setPeriod(ms); }
75  void set_response_timeout(uint32_t ms) { this->base_.setTimeout(ms); }
76  void set_request_attempts(uint32_t attempts) { this->base_.setNumAttempts(attempts); }
77 
78  /* Component methods */
79 
80  void setup() override { this->base_.setup(); }
81  void loop() override { this->base_.loop(); }
82  float get_setup_priority() const override { return setup_priority::BEFORE_CONNECTION; }
83  bool can_proceed() override {
84  return this->base_.getAutoconfStatus() != dudanov::midea::AutoconfStatus::AUTOCONF_PROGRESS;
85  }
86 
87  void set_beeper_feedback(bool state) { this->base_.setBeeper(state); }
88  void set_autoconf(bool value) { this->base_.setAutoconf(value); }
89  virtual void on_status_change() = 0;
90 
91  protected:
92  T base_;
94 #ifdef USE_REMOTE_TRANSMITTER
96 #endif
97 };
98 
99 } // namespace midea
100 } // namespace esphome
101 
102 #endif // USE_ARDUINO
void set_response_timeout(uint32_t ms)
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected. ...
Definition: component.cpp:25
size_t write(const uint8_t *data, size_t size) override
bool read_byte(uint8_t *data)
virtual void on_status_change()=0
float get_setup_priority() const override
size_t write(uint8_t data) override
void HOT esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args)
Definition: log.cpp:26
void set_period(uint32_t ms)
virtual bool peek_byte(uint8_t *data)=0
void set_request_attempts(uint32_t attempts)
void set_beeper_feedback(bool state)
void write_byte(uint8_t data)
uart::UARTComponent * uart_
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_uart(uart::UARTComponent *uart)
void set_transmitter(RemoteTransmitterBase *transmitter)
void set_uart_parent(uart::UARTComponent *parent)
void write_array(const std::vector< uint8_t > &data)
bool state
Definition: fan.h:34