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()) {
75 root[
"temp_step"] = traits.get_visual_target_temperature_step();
84 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
90 JsonArray presets = root.createNestedArray(
"preset_modes");
98 presets.add(
"comfort");
102 presets.add(
"sleep");
104 presets.add(
"activity");
105 for (
const auto &
preset : traits.get_supported_custom_presets())
109 if (traits.get_supports_action()) {
114 if (traits.get_supports_fan_modes()) {
120 JsonArray fan_modes = root.createNestedArray(
"fan_modes");
124 fan_modes.add(
"off");
126 fan_modes.add(
"auto");
128 fan_modes.add(
"low");
130 fan_modes.add(
"medium");
132 fan_modes.add(
"high");
134 fan_modes.add(
"middle");
136 fan_modes.add(
"focus");
138 fan_modes.add(
"diffuse");
140 fan_modes.add(
"quiet");
141 for (
const auto &
fan_mode : traits.get_supported_custom_fan_modes())
145 if (traits.get_supports_swing_modes()) {
151 JsonArray swing_modes = root.createNestedArray(
"swing_modes");
153 swing_modes.add(
"off");
155 swing_modes.add(
"both");
157 swing_modes.add(
"vertical");
159 swing_modes.add(
"horizontal");
166 auto traits = this->device_->get_traits();
167 this->subscribe(this->get_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
168 auto call = this->device_->make_call();
169 call.set_mode(payload);
173 if (traits.get_supports_two_point_target_temperature()) {
174 this->subscribe(this->get_target_temperature_low_command_topic(),
175 [
this](
const std::string &topic,
const std::string &payload) {
176 auto val = parse_number<float>(payload);
177 if (!
val.has_value()) {
178 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
181 auto call = this->device_->make_call();
182 call.set_target_temperature_low(*
val);
185 this->subscribe(this->get_target_temperature_high_command_topic(),
186 [
this](
const std::string &topic,
const std::string &payload) {
187 auto val = parse_number<float>(payload);
188 if (!
val.has_value()) {
189 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
192 auto call = this->device_->make_call();
193 call.set_target_temperature_high(*
val);
197 this->subscribe(this->get_target_temperature_command_topic(),
198 [
this](
const std::string &topic,
const std::string &payload) {
199 auto val = parse_number<float>(payload);
200 if (!
val.has_value()) {
201 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
204 auto call = this->device_->make_call();
205 call.set_target_temperature(*
val);
210 if (traits.get_supports_target_humidity()) {
211 this->subscribe(this->get_target_humidity_command_topic(),
212 [
this](
const std::string &topic,
const std::string &payload) {
213 auto val = parse_number<float>(payload);
214 if (!
val.has_value()) {
215 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
218 auto call = this->device_->make_call();
219 call.set_target_humidity(*
val);
224 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
225 this->subscribe(this->get_preset_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
226 auto call = this->device_->make_call();
227 call.set_preset(payload);
232 if (traits.get_supports_fan_modes()) {
233 this->subscribe(this->get_fan_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
234 auto call = this->device_->make_call();
235 call.set_fan_mode(payload);
240 if (traits.get_supports_swing_modes()) {
241 this->subscribe(this->get_swing_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
242 auto call = this->device_->make_call();
243 call.set_swing_mode(payload);
248 this->device_->add_on_state_callback([
this](
Climate & ) { this->publish_state_(); });
258 const char *mode_s =
"";
279 mode_s =
"heat_cool";
283 if (!this->
publish(this->get_mode_state_topic(), mode_s))
285 int8_t target_accuracy = traits.get_target_temperature_accuracy_decimals();
286 int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
289 if (!this->
publish(this->get_current_temperature_state_topic(), payload))
292 if (traits.get_supports_two_point_target_temperature()) {
294 if (!this->
publish(this->get_target_temperature_low_state_topic(), payload))
297 if (!this->
publish(this->get_target_temperature_high_state_topic(), payload))
301 if (!this->
publish(this->get_target_temperature_state_topic(), payload))
307 if (!this->
publish(this->get_current_humidity_state_topic(), payload))
312 if (!this->
publish(this->get_target_humidity_state_topic(), payload))
316 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
342 payload =
"activity";
348 if (!this->
publish(this->get_preset_state_topic(), payload))
352 if (traits.get_supports_action()) {
353 const char *payload =
"unknown";
374 if (!this->
publish(this->get_action_state_topic(), payload))
378 if (traits.get_supports_fan_modes()) {
416 if (!this->
publish(this->get_fan_mode_state_topic(), payload))
420 if (traits.get_supports_swing_modes()) {
421 const char *payload =
"";
430 payload =
"vertical";
433 payload =
"horizontal";
436 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
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_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.