12 static const char *
const TAG =
"mqtt.climate";
17 auto traits = this->device_->get_traits();
19 if (traits.get_supports_current_temperature()) {
23 if (traits.get_supports_current_humidity()) {
31 JsonArray modes = root.createNestedArray(
MQTT_MODES);
41 modes.add(
"fan_only");
45 modes.add(
"heat_cool");
47 if (traits.get_supports_two_point_target_temperature()) {
63 if (traits.get_supports_target_humidity()) {
86 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
92 JsonArray presets = root.createNestedArray(
"preset_modes");
100 presets.add(
"comfort");
104 presets.add(
"sleep");
106 presets.add(
"activity");
107 for (
const auto &
preset : traits.get_supported_custom_presets())
111 if (traits.get_supports_action()) {
116 if (traits.get_supports_fan_modes()) {
122 JsonArray fan_modes = root.createNestedArray(
"fan_modes");
126 fan_modes.add(
"off");
128 fan_modes.add(
"auto");
130 fan_modes.add(
"low");
132 fan_modes.add(
"medium");
134 fan_modes.add(
"high");
136 fan_modes.add(
"middle");
138 fan_modes.add(
"focus");
140 fan_modes.add(
"diffuse");
142 fan_modes.add(
"quiet");
143 for (
const auto &
fan_mode : traits.get_supported_custom_fan_modes())
147 if (traits.get_supports_swing_modes()) {
153 JsonArray swing_modes = root.createNestedArray(
"swing_modes");
155 swing_modes.add(
"off");
157 swing_modes.add(
"both");
159 swing_modes.add(
"vertical");
161 swing_modes.add(
"horizontal");
168 auto traits = this->device_->get_traits();
169 this->subscribe(this->get_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
170 auto call = this->device_->make_call();
171 call.set_mode(payload);
175 if (traits.get_supports_two_point_target_temperature()) {
176 this->subscribe(this->get_target_temperature_low_command_topic(),
177 [
this](
const std::string &topic,
const std::string &payload) {
178 auto val = parse_number<float>(payload);
179 if (!
val.has_value()) {
180 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
183 auto call = this->device_->make_call();
184 call.set_target_temperature_low(*
val);
187 this->subscribe(this->get_target_temperature_high_command_topic(),
188 [
this](
const std::string &topic,
const std::string &payload) {
189 auto val = parse_number<float>(payload);
190 if (!
val.has_value()) {
191 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
194 auto call = this->device_->make_call();
195 call.set_target_temperature_high(*
val);
199 this->subscribe(this->get_target_temperature_command_topic(),
200 [
this](
const std::string &topic,
const std::string &payload) {
201 auto val = parse_number<float>(payload);
202 if (!
val.has_value()) {
203 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
206 auto call = this->device_->make_call();
207 call.set_target_temperature(*
val);
212 if (traits.get_supports_target_humidity()) {
213 this->subscribe(this->get_target_humidity_command_topic(),
214 [
this](
const std::string &topic,
const std::string &payload) {
215 auto val = parse_number<float>(payload);
216 if (!
val.has_value()) {
217 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
220 auto call = this->device_->make_call();
221 call.set_target_humidity(*
val);
226 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
227 this->subscribe(this->get_preset_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
228 auto call = this->device_->make_call();
229 call.set_preset(payload);
234 if (traits.get_supports_fan_modes()) {
235 this->subscribe(this->get_fan_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
236 auto call = this->device_->make_call();
237 call.set_fan_mode(payload);
242 if (traits.get_supports_swing_modes()) {
243 this->subscribe(this->get_swing_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
244 auto call = this->device_->make_call();
245 call.set_swing_mode(payload);
250 this->device_->add_on_state_callback([
this](
Climate & ) { this->publish_state_(); });
281 mode_s =
"heat_cool";
287 if (!this->
publish(this->get_mode_state_topic(), mode_s))
289 int8_t target_accuracy = traits.get_target_temperature_accuracy_decimals();
290 int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
293 if (!this->
publish(this->get_current_temperature_state_topic(), payload))
296 if (traits.get_supports_two_point_target_temperature()) {
298 if (!this->
publish(this->get_target_temperature_low_state_topic(), payload))
301 if (!this->
publish(this->get_target_temperature_high_state_topic(), payload))
305 if (!this->
publish(this->get_target_temperature_state_topic(), payload))
311 if (!this->
publish(this->get_current_humidity_state_topic(), payload))
316 if (!this->
publish(this->get_target_humidity_state_topic(), payload))
320 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
346 payload =
"activity";
354 if (!this->
publish(this->get_preset_state_topic(), payload))
358 if (traits.get_supports_action()) {
382 if (!this->
publish(this->get_action_state_topic(), payload))
386 if (traits.get_supports_fan_modes()) {
426 if (!this->
publish(this->get_fan_mode_state_topic(), payload))
430 if (traits.get_supports_swing_modes()) {
440 payload =
"vertical";
443 payload =
"horizontal";
448 if (!this->
publish(this->get_swing_mode_state_topic(), payload))
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TOPIC
value_type const & value() const
constexpr const char *const MQTT_MIN_TEMP
bool send_initial_state() override
float current_humidity
The current humidity of the climate device, as reported from the integration.
ClimateSwingMode swing_mode
The active swing mode of the climate device.
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals)
Create a string from a value and an accuracy in decimals.
constexpr const char *const MQTT_FAN_MODE_COMMAND_TOPIC
constexpr const char *const MQTT_SWING_MODE_COMMAND_TOPIC
constexpr const char *const MQTT_FAN_MODE_STATE_TOPIC
float target_temperature
The target temperature of the climate device.
constexpr const char *const MQTT_CURRENT_HUMIDITY_TOPIC
constexpr const char *const MQTT_ACTION_TOPIC
constexpr const char *const MQTT_TARGET_HUMIDITY_STATE_TOPIC
bool state_topic
If the state topic should be included. Defaults to true.
constexpr const char *const MQTT_TEMPERATURE_COMMAND_TOPIC
constexpr const char *const MQTT_TEMPERATURE_STATE_TOPIC
ClimateMode mode
The active mode of the climate device.
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
float current_temperature
The current temperature of the climate device, as reported from the integration.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
constexpr const char *const MQTT_MAX_TEMP
constexpr const char *const MQTT_MODE_STATE_TOPIC
constexpr const char *const MQTT_PRESET_MODE_COMMAND_TOPIC
float target_humidity
The target humidity of the climate device.
MQTTClimateComponent(climate::Climate *device)
bool command_topic
If the command topic should be included. Default to true.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
optional< std::string > custom_fan_mode
The active custom fan mode of the climate device.
constexpr const char *const MQTT_TEMPERATURE_LOW_COMMAND_TOPIC
constexpr const char *const MQTT_TEMPERATURE_HIGH_COMMAND_TOPIC
constexpr const char *const MQTT_TEMPERATURE_UNIT
constexpr const char *const MQTT_CURRENT_TEMPERATURE_STEP
optional< ClimatePreset > preset
The active preset of the climate device.
climate::Climate * device_
virtual const EntityBase * get_entity() const =0
Gets the Entity served by this MQTT component.
state command command command command command command state state state MQTT_COMPONENT_CUSTOM_TOPIC(preset, command) protected bool publish_state_()
constexpr const char *const MQTT_MIN_HUMIDITY
Simple Helper struct used for Home Assistant MQTT send_discovery().
constexpr const char *const MQTT_MAX_HUMIDITY
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
optional< std::string > custom_preset
The active custom preset mode of the climate device.
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
constexpr const char *const MQTT_MODES
constexpr const char *const MQTT_SWING_MODE_STATE_TOPIC
constexpr const char *const MQTT_TARGET_HUMIDITY_COMMAND_TOPIC
Implementation of SPI Controller mode.
constexpr const char *const MQTT_PRESET_MODE_STATE_TOPIC
constexpr const char *const MQTT_TARGET_TEMPERATURE_STEP
constexpr const char *const MQTT_TEMPERATURE_HIGH_STATE_TOPIC
std::string component_type() const override
constexpr const char *const MQTT_MODE_COMMAND_TOPIC
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
constexpr const char *const MQTT_TEMPERATURE_LOW_STATE_TOPIC
ClimateAction action
The active state of the climate device.
ClimateDevice - This is the base class for all climate integrations.