ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
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
11
13#include "esphome/core/log.h"
16#include "ir_transmitter.h"
17
18namespace esphome {
19namespace midea {
20
21/* Stream from UART component */
22class 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 if (!this->uart_->read_byte(&data))
32 return -1;
33 return data;
34 }
35 int peek() override {
36 uint8_t data;
37 if (!this->uart_->peek_byte(&data))
38 return -1;
39 return data;
40 }
41 size_t write(uint8_t data) override {
42 this->uart_->write_byte(data);
43 return 1;
44 }
45 size_t write(const uint8_t *data, size_t size) override {
46 this->uart_->write_array(data, size);
47 return size;
48 }
49 void flush() override { this->uart_->flush(); }
50
51 protected:
53};
54
55template<typename T> class ApplianceBase : public Component {
56 static_assert(std::is_base_of<dudanov::midea::ApplianceBase, T>::value,
57 "T must derive from dudanov::midea::ApplianceBase class");
58
59 public:
61 this->base_.setStream(&this->stream_);
62 this->base_.addOnStateCallback([this]() { this->on_status_change(); });
63 dudanov::midea::ApplianceBase::setLogger(
64 [](int level, const char *tag, int line, const String &format, va_list args) {
65 esp_log_vprintf_(level, tag, line, format.c_str(), args);
66 });
67 }
68
69#ifdef USE_REMOTE_TRANSMITTER
70 void set_transmitter(RemoteTransmitterBase *transmitter) { this->transmitter_.set_transmitter(transmitter); }
71#endif
72
73 /* UART communication */
74
75 void set_uart_parent(uart::UARTComponent *parent) { this->stream_.set_uart(parent); }
76 void set_period(uint32_t ms) { this->base_.setPeriod(ms); }
77 void set_response_timeout(uint32_t ms) { this->base_.setTimeout(ms); }
78 void set_request_attempts(uint32_t attempts) { this->base_.setNumAttempts(attempts); }
79
80 /* Component methods */
81
82 void setup() override { this->base_.setup(); }
83 void loop() override { this->base_.loop(); }
84 float get_setup_priority() const override { return setup_priority::BEFORE_CONNECTION; }
85 bool can_proceed() override {
86 return this->base_.getAutoconfStatus() != dudanov::midea::AutoconfStatus::AUTOCONF_PROGRESS;
87 }
88
89 void set_beeper_feedback(bool state) { this->base_.setBeeper(state); }
90 void set_autoconf(bool value) { this->base_.setAutoconf(value); }
91 virtual void on_status_change() = 0;
92
93 protected:
96#ifdef USE_REMOTE_TRANSMITTER
98#endif
99};
100
101} // namespace midea
102} // namespace esphome
103
104#endif // USE_ARDUINO
void set_response_timeout(uint32_t ms)
float get_setup_priority() const override
void set_uart_parent(uart::UARTComponent *parent)
virtual void on_status_change()=0
void set_transmitter(RemoteTransmitterBase *transmitter)
void set_beeper_feedback(bool state)
void set_request_attempts(uint32_t attempts)
void set_transmitter(RemoteTransmitterBase *transmitter)
size_t write(const uint8_t *data, size_t size) override
void set_uart(uart::UARTComponent *uart)
size_t write(uint8_t data) override
uart::UARTComponent * uart_
void write_byte(uint8_t data)
void write_array(const std::vector< uint8_t > &data)
bool read_byte(uint8_t *data)
virtual UARTFlushResult flush()=0
virtual size_t available()=0
virtual bool peek_byte(uint8_t *data)=0
bool state
Definition fan.h:2
constexpr float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.h:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const char int line
Definition log.h:74
const char * tag
Definition log.h:74
const char int const __FlashStringHelper * format
Definition log.h:74
const char int const __FlashStringHelper va_list args
Definition log.h:74
uint16_t size
Definition helpers.cpp:25
void HOT esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args)
Definition log.cpp:51
static void uint32_t