Skip to content

Commit 0b4521d

Browse files
authored
Merge pull request #4 from distante/saninn/refactor-ventilation-controller-to-not-track-single-tasks
Saninn/refactor ventilation controller to not track single tasks
2 parents f7d6cc7 + d27b9d7 commit 0b4521d

7 files changed

Lines changed: 203 additions & 321 deletions

File tree

components/sec_touch/_definitions.h

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
namespace esphome {
1111
namespace sec_touch {
1212

13-
#define STX 0x02
14-
#define ETX 0x0A
15-
#define ACK 0x06
16-
#define NAK 0x15
17-
#define TAB 0x09
13+
#define STX 0x02 // Start of Text
14+
#define ETX 0x0A // End of Text
15+
#define ACK 0x06 // Acknowledge
16+
#define NAK 0x15 // Not Acknowledge
17+
#define TAB 0x09 // Tab
1818
#define NOISE 0xFF // 255
1919

2020
constexpr const int COMMANDID_SET = 32;
@@ -111,17 +111,14 @@ template<typename T, size_t N> static bool contains(const std::array<T, N> &arr,
111111
*/
112112
enum class TaskTargetType { LEVEL, LABEL };
113113
enum class TaskType {
114-
MANUAL_SET,
115-
AUTO_GET,
116-
MANUAL_GET,
114+
SET_DATA,
115+
GET_DATA,
117116
/**
118117
* Used to indicate that no task is currently being processed
119118
*/
120119
NONE
121120
};
122121

123-
enum class TaskState { TO_BE_SENT, TO_BE_PROCESSED };
124-
125122
class EnumToString {
126123
private:
127124
EnumToString() = default;
@@ -138,25 +135,12 @@ class EnumToString {
138135
}
139136
}
140137

141-
static const char *TaskState(TaskState v) {
142-
switch (v) {
143-
case TaskState::TO_BE_SENT:
144-
return "TO_BE_SENT";
145-
146-
case TaskState::TO_BE_PROCESSED:
147-
return "TO_BE_PROCESSED";
148-
default:
149-
return "UNKNOWN";
150-
}
151-
}
152138
static const char *TaskType(TaskType v) {
153139
switch (v) {
154-
case TaskType::MANUAL_SET:
155-
return "MANUAL_SET";
156-
case TaskType::AUTO_GET:
157-
return "AUTO_GET";
158-
case TaskType::MANUAL_GET:
159-
return "MANUAL_GET";
140+
case TaskType::SET_DATA:
141+
return "SET_DATA";
142+
case TaskType::GET_DATA:
143+
return "GET_DATA";
160144
case TaskType::NONE:
161145
return "NONE";
162146
default:
@@ -221,13 +205,15 @@ struct IncomingMessage {
221205
}
222206
};
223207

224-
struct SetDataTask {
208+
struct BaseTask {
209+
virtual ~BaseTask() = default;
210+
virtual TaskType get_task_type() const = 0;
225211
TaskTargetType targetType;
226212
int property_id;
227-
char value[8];
228-
TaskState state = TaskState::TO_BE_SENT;
213+
};
229214

230-
const TaskType taskType = TaskType::MANUAL_SET;
215+
struct SetDataTask : public BaseTask {
216+
char value[8];
231217

232218
static std::unique_ptr<SetDataTask> create(TaskTargetType targetType, int property_id, const char *value) {
233219
if ((targetType == TaskTargetType::LEVEL && contains(FAN_LEVEL_IDS, property_id)) ||
@@ -237,20 +223,18 @@ struct SetDataTask {
237223
return nullptr; // Null unique_ptr if validation fails
238224
}
239225

226+
TaskType get_task_type() const override { return TaskType::SET_DATA; }
227+
240228
private:
241-
SetDataTask(TaskTargetType targetType, int property_id, const char *value)
242-
: targetType(targetType), property_id(property_id) {
229+
SetDataTask(TaskTargetType targetType, int property_id, const char *value) {
230+
this->targetType = targetType;
231+
this->property_id = property_id;
243232
std::strncpy(this->value, value, sizeof(this->value) - 1);
244233
this->value[sizeof(this->value) - 1] = '\0'; // Ensure null termination
245234
}
246235
};
247236

248-
struct GetDataTask {
249-
TaskTargetType targetType;
250-
int property_id;
251-
TaskState state = TaskState::TO_BE_SENT;
252-
const TaskType taskType = TaskType::AUTO_GET;
253-
237+
struct GetDataTask : public BaseTask {
254238
static std::unique_ptr<GetDataTask> create(TaskTargetType targetType, int property_id) {
255239
if ((targetType == TaskTargetType::LEVEL && contains(FAN_LEVEL_IDS, property_id)) ||
256240
(targetType == TaskTargetType::LABEL && contains(FAN_LABEL_IDS, property_id))) {
@@ -259,8 +243,13 @@ struct GetDataTask {
259243
return nullptr; // Null unique_ptr if validation fails
260244
}
261245

246+
TaskType get_task_type() const override { return TaskType::GET_DATA; }
247+
262248
private:
263-
GetDataTask(TaskTargetType targetType, int property_id) : targetType(targetType), property_id(property_id) {}
249+
GetDataTask(TaskTargetType targetType, int property_id) {
250+
this->targetType = targetType;
251+
this->property_id = property_id;
252+
}
264253
};
265254

266255
} // namespace sec_touch

components/sec_touch/button/program_text_update_button.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace esphome {
44
namespace sec_touch {
55

6-
void ProgramTextUpdateButton::press_action() { this->parent_->add_manual_tasks_to_get_queue(); }
6+
void ProgramTextUpdateButton::press_action() { this->parent_->add_manual_tasks_to_queue(); }
77

88
} // namespace sec_touch
99
} // namespace esphome

components/sec_touch/fan/sec_touch_fan.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ SecTouchFan::SecTouchFan(SECTouchComponent *parent, int level_id, int label_id)
5656

5757
const std::string &current_label = label_text_sensor->get_state(); // Get the current state of the text sensor
5858
if (current_label == new_label) {
59-
ESP_LOGD(TAG, "Label is already up-to-date: %s", current_label);
59+
ESP_LOGD(TAG, "Label is already up-to-date: %s", current_label.c_str());
6060
return; // Do not publish if the value is the same
6161
}
6262

@@ -209,8 +209,8 @@ void SecTouchFan::update_label_mode() {
209209
}
210210

211211
// Print method for debugging
212-
void SecTouchFan::printConfig() {
213-
ESP_LOGCONFIG(TAG, "Level ID: %d, Label ID: %d, Fan Level Value: %d", this->level_id, this->label_id, this->speed);
212+
void SecTouchFan::dump_config() {
213+
ESP_LOGCONFIG(TAG, "Level ID: %d, Label ID: %d, Current Fan Level: %d", this->level_id, this->label_id, this->speed);
214214
}
215215

216216
} // namespace sec_touch

components/sec_touch/fan/sec_touch_fan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SecTouchFan : public Component, public fan::Fan {
4141
fan::FanTraits get_traits() override { return this->traits_; }
4242

4343
// Print method for debugging
44-
void printConfig();
44+
void dump_config() override;
4545
};
4646

4747
} // namespace sec_touch

components/sec_touch/readme.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,8 @@ where:
493493
- `32` is the command id for a SET request.
494494
- `173` is the property id of the fan pair.
495495
- `5` is the value assigned to that id.
496-
- `42625` is the checksum.
497-
-
496+
- `42625` is the checksum (probably).
497+
498498
### SET Response Message
499499
Sadly (IMHO) the SEC-TOUCH just sends an `ACK` message after receiving a SET message and sometimes it takes a couple of seconds for the SEC-TOUCH screen to update the new value, so the best we can do is to wait for the `ACK` message and then send a `GET` message to do a security sync of the new value.
500500

@@ -504,11 +504,7 @@ Byte received: 6 // ACK 0x06
504504
Byte received: 10 // ETX 0x0A
505505
```
506506

507-
508-
509-
510-
511-
### Update submodules
507+
# Update submodules
512508
```
513509
git submodule update --remote --merge
514510
```

0 commit comments

Comments
 (0)