ESPHome 2026.6.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::midea {
19
20/* Stream from UART component */
21class UARTStream : public Stream {
22 public:
23 void set_uart(uart::UARTComponent *uart) { this->uart_ = uart; }
24
25 /* Stream interface implementation */
26
27 int available() override { return this->uart_->available(); }
28 int read() override {
29 uint8_t data;
30 if (!this->uart_->read_byte(&data))
31 return -1;
32 return data;
33 }
34 int peek() override {
35 uint8_t data;
36 if (!this->uart_->peek_byte(&data))
37 return -1;
38 return data;
39 }
40 size_t write(uint8_t data) override {
41 this->uart_->write_byte(data);
42 return 1;
43 }
44 size_t write(const uint8_t *data, size_t size) override {
45 this->uart_->write_array(data, size);
46 return size;
47 }
48 void flush() override { this->uart_->flush(); }
49
50 protected:
52};
53
54template<typename T> class ApplianceBase : public Component {
55 static_assert(std::is_base_of<dudanov::midea::ApplianceBase, T>::value,
56 "T must derive from dudanov::midea::ApplianceBase class");
57
58 public:
60 this->base_.setStream(&this->stream_);
61 this->base_.addOnStateCallback([this]() { this->on_status_change(); });
62 dudanov::midea::ApplianceBase::setLogger(
63 [](int level, const char *tag, int line, const String &format, va_list args) {
64 esp_log_vprintf_(level, tag, line, format.c_str(), args);
65 });
66 }
67
68#ifdef USE_REMOTE_TRANSMITTER
69 void set_transmitter(RemoteTransmitterBase *transmitter) { this->transmitter_.set_transmitter(transmitter); }
70#endif
71
72 /* UART communication */
73
74 void set_uart_parent(uart::UARTComponent *parent) { this->stream_.set_uart(parent); }
75 void set_period(uint32_t ms) { this->base_.setPeriod(ms); }
76 void set_response_timeout(uint32_t ms) { this->base_.setTimeout(ms); }
77 void set_request_attempts(uint32_t attempts) { this->base_.setNumAttempts(attempts); }
78
79 /* Component methods */
80
81 void setup() override { this->base_.setup(); }
82 void loop() override { this->base_.loop(); }
83 float get_setup_priority() const override { return setup_priority::BEFORE_CONNECTION; }
84 bool can_proceed() override {
85 return this->base_.getAutoconfStatus() != dudanov::midea::AutoconfStatus::AUTOCONF_PROGRESS;
86 }
87
88 void set_beeper_feedback(bool state) { this->base_.setBeeper(state); }
89 void set_autoconf(bool value) { this->base_.setAutoconf(value); }
90 virtual void on_status_change() = 0;
91
92 protected:
95#ifdef USE_REMOTE_TRANSMITTER
97#endif
98};
99
100} // namespace esphome::midea
101
102#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:51
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