-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaikin_ir_test.h
211 lines (193 loc) · 7.43 KB
/
daikin_ir_test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "esphome.h"
#include "IRremoteESP8266.h"
#include "IRsend.h"
#include "IRrecv.h"
#include "ir_Daikin.h"
///This code is relevant for cases where the IR control for an AC is available in IRremoteESP8266, but isn't supported yet in Esphome
const uint16_t kIrLed = 0; // ESP8266 GPIO pin to use for transmitter bulb. Recommended: 0 (D3).
// const uint16_t kIrRecvPin = 12; // For future testing - ESP8266 GPIO pin for receiver
IRDaikin128 ac(kIrLed);
// Setup files. This is the equivalent of the code written in the setup loop of Arduino
class DaikinAC : public Component, public Climate {
public:
// Start by importing temp. sensor from Home Assistant (Defined in ESPHome Yaml)
sensor::Sensor *sensor_{nullptr};
// Insure correct power state is sent based on previous state
void togglePowerOn(void) {
ac.setPowerToggle(this->mode == CLIMATE_MODE_OFF);
}
void set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; }
void setup() override
{
if (this->sensor_) {
this->sensor_->add_on_state_callback([this](float state) {
this->current_temperature = state;
this->publish_state();
});
this->current_temperature = this->sensor_->state;
} else {
this->current_temperature = NAN;
}
auto restore = this->restore_state_();
if (restore.has_value()) {
restore->apply(this);
} else {
this->mode = climate::CLIMATE_MODE_OFF;
this->target_temperature = roundf(clamp(this->current_temperature, 18.0f, 30.0f));
this->fan_mode = climate::CLIMATE_FAN_AUTO;
this->swing_mode = climate::CLIMATE_SWING_OFF;
}
if (isnan(this->target_temperature)) {
this->target_temperature = 23;
}
ac.begin();
if (this->mode == CLIMATE_MODE_OFF) {
ac.setPowerToggle(true);
} else if (this->mode == CLIMATE_MODE_AUTO) {
togglePowerOn();
ac.setMode(kDaikin128Auto);
} else if (this->mode == CLIMATE_MODE_COOL) {
togglePowerOn();
ac.setMode(kDaikin128Cool);
} else if (this->mode == CLIMATE_MODE_HEAT) {
togglePowerOn();
ac.setMode(kDaikin128Heat);
} else if (this->mode == CLIMATE_MODE_FAN_ONLY) {
togglePowerOn();
ac.setMode(kDaikin128Fan);
} else if (this->mode == CLIMATE_MODE_DRY) {
togglePowerOn();
ac.setMode(kDaikin128Dry);
}
ac.setTemp(this->target_temperature);
if (this->fan_mode == CLIMATE_FAN_AUTO) {
togglePowerOn();
ac.setFan(kDaikin128FanAuto);
} else if (this->fan_mode == CLIMATE_FAN_LOW) {
togglePowerOn();
ac.setFan(kDaikin128FanLow);
} else if (this->fan_mode == CLIMATE_FAN_MEDIUM) {
togglePowerOn();
ac.setFan(kDaikin128FanMed);
} else if (this->fan_mode == CLIMATE_FAN_HIGH) {
togglePowerOn();
ac.setFan(kDaikin128FanHigh);
} else if (this->fan_mode == CLIMATE_FAN_FOCUS) {
togglePowerOn();
ac.setFan(kDaikin128FanPowerful);
} else if (this->fan_mode == CLIMATE_FAN_DIFFUSE) {
togglePowerOn();
ac.setFan(kDaikin128FanQuiet);
}
if (this->swing_mode == CLIMATE_SWING_OFF) {
togglePowerOn();
ac.setSwingVertical(false);
} else if (this->swing_mode == CLIMATE_SWING_VERTICAL) {
togglePowerOn();
ac.setSwingVertical(true);
}
ac.send();
ESP_LOGD("DEBUG", "Daikin A/C remote is in the following state:");
ESP_LOGD("DEBUG", " %s\n", ac.toString().c_str());
}
// Traits: This tells home assistant what "traits" are supported by AC in terms of heating/cooling/fan speeds/swing modes. These are used by Home Assistant to customize the AC card on the dashboard
climate::ClimateTraits traits() {
auto traits = climate::ClimateTraits();
traits.set_supported_modes({
climate::CLIMATE_MODE_OFF,
climate::CLIMATE_MODE_AUTO,
climate::CLIMATE_MODE_COOL,
climate::CLIMATE_MODE_HEAT,
climate::CLIMATE_MODE_FAN_ONLY,
climate::CLIMATE_MODE_DRY,
});
traits.set_supported_fan_modes({
climate::CLIMATE_FAN_AUTO,
climate::CLIMATE_FAN_HIGH,
climate::CLIMATE_FAN_LOW,
climate::CLIMATE_FAN_MEDIUM,
climate::CLIMATE_FAN_DIFFUSE,
climate::CLIMATE_FAN_FOCUS,
});
traits.set_supported_swing_modes({
climate::CLIMATE_SWING_OFF,
climate::CLIMATE_SWING_VERTICAL,
});
traits.set_supports_current_temperature(this->sensor_ != nullptr);
traits.set_supports_two_point_target_temperature(false);
traits.set_visual_max_temperature(30);
traits.set_visual_min_temperature(18);
traits.set_visual_temperature_step(1);
return traits;
}
//Code for what to do when the mode of the AC is changed on the dashboard
void control(const ClimateCall &call) override {
if (call.get_mode().has_value()) {
ClimateMode mode = *call.get_mode();
//For each mode, need to find the relevant mode from the list of constants. This list can be found in the relevant .h library from IRremoteESP8266 library. In this case the file is "ir_Hitachi.h". Typically the function should be the same - .setMode. However, best check the relevant .h library.
if (mode == CLIMATE_MODE_OFF) {
ac.setPowerToggle(true);
} else if (mode == CLIMATE_MODE_AUTO) {
togglePowerOn();
ac.setMode(kDaikin128Auto);
} else if (mode == CLIMATE_MODE_COOL) {
togglePowerOn();
ac.setMode(kDaikin128Cool);
} else if (mode == CLIMATE_MODE_HEAT) {
togglePowerOn();
ac.setMode(kDaikin128Heat);
} else if (mode == CLIMATE_MODE_FAN_ONLY) {
togglePowerOn();
ac.setMode(kDaikin128Fan);
} else if (mode == CLIMATE_MODE_DRY) {
togglePowerOn();
ac.setMode(kDaikin128Dry);
}
this->mode = mode;
}
if (call.get_target_temperature().has_value()) {
float temp = *call.get_target_temperature();
togglePowerOn();
ac.setTemp(temp);
this->target_temperature = temp;
}
if (call.get_fan_mode().has_value()) {
ClimateFanMode fan_mode = *call.get_fan_mode();
if (fan_mode == CLIMATE_FAN_AUTO) {
togglePowerOn();
ac.setFan(kDaikin128FanAuto);
} else if (fan_mode == CLIMATE_FAN_LOW) {
togglePowerOn();
ac.setFan(kDaikin128FanLow);
} else if (fan_mode == CLIMATE_FAN_MEDIUM) {
togglePowerOn();
ac.setFan(kDaikin128FanMed);
} else if (fan_mode == CLIMATE_FAN_HIGH) {
togglePowerOn();
ac.setFan(kDaikin128FanHigh);
} else if (fan_mode == CLIMATE_FAN_FOCUS) {
togglePowerOn();
ac.setFan(kDaikin128FanPowerful);
} else if (fan_mode == CLIMATE_FAN_DIFFUSE) {
togglePowerOn();
ac.setFan(kDaikin128FanQuiet);
}
this->fan_mode = fan_mode;
}
if (call.get_swing_mode().has_value()) {
ClimateSwingMode swing_mode = *call.get_swing_mode();
if (swing_mode == CLIMATE_SWING_OFF) {
togglePowerOn();
ac.setSwingVertical(false);
} else if (swing_mode == CLIMATE_SWING_VERTICAL) {
togglePowerOn();
ac.setSwingVertical(true);
}
this->swing_mode = swing_mode;
}
ac.send();
this->publish_state();
ESP_LOGD("DEBUG", "Daikin A/C remote is in the following state:");
ESP_LOGD("DEBUG", " %s\n", ac.toString().c_str());
}
};