11static const char *
const TAG =
"mqtt.climate";
20 root[MQTT_CURRENT_TEMPERATURE_TOPIC] = this->get_current_temperature_state_topic();
24 root[MQTT_CURRENT_HUMIDITY_TOPIC] = this->get_current_humidity_state_topic();
27 root[MQTT_MODE_COMMAND_TOPIC] = this->get_mode_command_topic();
29 root[MQTT_MODE_STATE_TOPIC] = this->get_mode_state_topic();
31 JsonArray modes = root[MQTT_MODES].to<JsonArray>();
34 modes.add(ESPHOME_F(
"auto"));
35 modes.add(ESPHOME_F(
"off"));
37 modes.add(ESPHOME_F(
"cool"));
39 modes.add(ESPHOME_F(
"heat"));
41 modes.add(ESPHOME_F(
"fan_only"));
43 modes.add(ESPHOME_F(
"dry"));
45 modes.add(ESPHOME_F(
"heat_cool"));
50 root[MQTT_TEMPERATURE_LOW_COMMAND_TOPIC] = this->get_target_temperature_low_command_topic();
52 root[MQTT_TEMPERATURE_LOW_STATE_TOPIC] = this->get_target_temperature_low_state_topic();
54 root[MQTT_TEMPERATURE_HIGH_COMMAND_TOPIC] = this->get_target_temperature_high_command_topic();
56 root[MQTT_TEMPERATURE_HIGH_STATE_TOPIC] = this->get_target_temperature_high_state_topic();
59 root[MQTT_TEMPERATURE_COMMAND_TOPIC] = this->get_target_temperature_command_topic();
61 root[MQTT_TEMPERATURE_STATE_TOPIC] = this->get_target_temperature_state_topic();
66 root[MQTT_TARGET_HUMIDITY_COMMAND_TOPIC] = this->get_target_humidity_command_topic();
68 root[MQTT_TARGET_HUMIDITY_STATE_TOPIC] = this->get_target_humidity_state_topic();
72 root[MQTT_MIN_TEMP] = traits.get_visual_min_temperature();
74 root[MQTT_MAX_TEMP] = traits.get_visual_max_temperature();
76 root[MQTT_TARGET_TEMPERATURE_STEP] = roundf(traits.get_visual_target_temperature_step() * 10) * 0.1;
78 root[MQTT_CURRENT_TEMPERATURE_STEP] = roundf(traits.get_visual_current_temperature_step() * 10) * 0.1;
80 root[MQTT_TEMPERATURE_UNIT] =
"C";
83 root[MQTT_MIN_HUMIDITY] = traits.get_visual_min_humidity();
85 root[MQTT_MAX_HUMIDITY] = traits.get_visual_max_humidity();
87 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
89 root[MQTT_PRESET_MODE_COMMAND_TOPIC] = this->get_preset_command_topic();
91 root[MQTT_PRESET_MODE_STATE_TOPIC] = this->get_preset_state_topic();
93 JsonArray presets = root[ESPHOME_F(
"preset_modes")].to<JsonArray>();
95 presets.add(ESPHOME_F(
"home"));
97 presets.add(ESPHOME_F(
"away"));
99 presets.add(ESPHOME_F(
"boost"));
101 presets.add(ESPHOME_F(
"comfort"));
103 presets.add(ESPHOME_F(
"eco"));
105 presets.add(ESPHOME_F(
"sleep"));
107 presets.add(ESPHOME_F(
"activity"));
108 for (
const auto &
preset : traits.get_supported_custom_presets())
114 root[MQTT_ACTION_TOPIC] = this->get_action_state_topic();
117 if (traits.get_supports_fan_modes()) {
119 root[MQTT_FAN_MODE_COMMAND_TOPIC] = this->get_fan_mode_command_topic();
121 root[MQTT_FAN_MODE_STATE_TOPIC] = this->get_fan_mode_state_topic();
123 JsonArray fan_modes = root[ESPHOME_F(
"fan_modes")].to<JsonArray>();
125 fan_modes.add(ESPHOME_F(
"on"));
127 fan_modes.add(ESPHOME_F(
"off"));
129 fan_modes.add(ESPHOME_F(
"auto"));
131 fan_modes.add(ESPHOME_F(
"low"));
133 fan_modes.add(ESPHOME_F(
"medium"));
135 fan_modes.add(ESPHOME_F(
"high"));
137 fan_modes.add(ESPHOME_F(
"middle"));
139 fan_modes.add(ESPHOME_F(
"focus"));
141 fan_modes.add(ESPHOME_F(
"diffuse"));
143 fan_modes.add(ESPHOME_F(
"quiet"));
144 for (
const auto &
fan_mode : traits.get_supported_custom_fan_modes())
148 if (traits.get_supports_swing_modes()) {
150 root[MQTT_SWING_MODE_COMMAND_TOPIC] = this->get_swing_mode_command_topic();
152 root[MQTT_SWING_MODE_STATE_TOPIC] = this->get_swing_mode_state_topic();
154 JsonArray swing_modes = root[ESPHOME_F(
"swing_modes")].to<JsonArray>();
156 swing_modes.add(ESPHOME_F(
"off"));
158 swing_modes.add(ESPHOME_F(
"both"));
160 swing_modes.add(ESPHOME_F(
"vertical"));
162 swing_modes.add(ESPHOME_F(
"horizontal"));
171 this->
subscribe(this->get_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
179 this->
subscribe(this->get_target_temperature_low_command_topic(),
180 [
this](
const std::string &topic,
const std::string &payload) {
182 if (!
val.has_value()) {
183 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
190 this->
subscribe(this->get_target_temperature_high_command_topic(),
191 [
this](
const std::string &topic,
const std::string &payload) {
193 if (!
val.has_value()) {
194 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
202 this->
subscribe(this->get_target_temperature_command_topic(),
203 [
this](
const std::string &topic,
const std::string &payload) {
205 if (!
val.has_value()) {
206 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
216 this->
subscribe(this->get_target_humidity_command_topic(),
217 [
this](
const std::string &topic,
const std::string &payload) {
219 if (!
val.has_value()) {
220 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
229 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
230 this->
subscribe(this->get_preset_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
237 if (traits.get_supports_fan_modes()) {
238 this->
subscribe(this->get_fan_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
245 if (traits.get_supports_swing_modes()) {
246 this->
subscribe(this->get_swing_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
284 mode_s =
"heat_cool";
290 if (!this->
publish(this->get_mode_state_topic(), mode_s))
293 int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
294 char payload[VALUE_ACCURACY_MAX_LEN];
297 !std::isnan(this->device_->current_temperature)) {
299 if (!this->
publish(this->get_current_temperature_state_topic(), payload,
len))
305 if (!this->
publish(this->get_target_temperature_low_state_topic(), payload,
len))
308 if (!this->
publish(this->get_target_temperature_high_state_topic(), payload,
len))
312 if (!this->
publish(this->get_target_temperature_state_topic(), payload,
len))
317 !std::isnan(this->device_->current_humidity)) {
319 if (!this->
publish(this->get_current_humidity_state_topic(), payload,
len))
323 !std::isnan(this->device_->target_humidity)) {
325 if (!this->
publish(this->get_target_humidity_state_topic(), payload,
len))
329 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
355 payload =
"activity";
363 if (!this->
publish(this->get_preset_state_topic(), payload))
391 if (!this->
publish(this->get_action_state_topic(), payload))
395 if (traits.get_supports_fan_modes()) {
435 if (!this->
publish(this->get_fan_mode_state_topic(), payload))
439 if (traits.get_supports_swing_modes()) {
449 payload =
"vertical";
452 payload =
"horizontal";
457 if (!this->
publish(this->get_swing_mode_state_topic(), payload))
constexpr const char * c_str() const
ClimateCall & set_target_temperature(float target_temperature)
Set the target temperature of the climate device.
ClimateCall & set_swing_mode(ClimateSwingMode swing_mode)
Set the swing mode of the climate device.
ClimateCall & set_target_temperature_low(float target_temperature_low)
Set the low point target temperature of the climate device.
ClimateCall & set_preset(ClimatePreset preset)
Set the preset of the climate device.
ClimateCall & set_fan_mode(ClimateFanMode fan_mode)
Set the fan mode of the climate device.
ClimateCall & set_target_humidity(float target_humidity)
Set the target humidity of the climate device.
ClimateCall & set_target_temperature_high(float target_temperature_high)
Set the high point target temperature of the climate device.
ClimateCall & set_mode(ClimateMode mode)
Set the mode of the climate device.
ClimateDevice - This is the base class for all climate integrations.
ClimateMode mode
The active mode of the climate device.
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
float target_temperature
The target temperature of the climate device.
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.
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
void add_on_state_callback(std::function< void(Climate &)> &&callback)
Add a callback for the climate device state, each time the state of the climate device is updated (us...
bool has_custom_preset() const
Check if a custom preset is currently active.
float current_temperature
The current temperature of the climate device, as reported from the integration.
ClimateAction action
The active state of the climate device.
ClimateCall make_call()
Make a climate device control call, this is used to control the climate device, see the ClimateCall d...
StringRef get_custom_preset() const
Get the active custom preset (read-only access). Returns StringRef.
bool has_custom_fan_mode() const
Check if a custom fan mode is currently active.
optional< ClimatePreset > preset
The active preset of the climate device.
float target_humidity
The target humidity of the climate device.
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
StringRef get_custom_fan_mode() const
Get the active custom fan mode (read-only access). Returns StringRef.
int8_t get_target_temperature_accuracy_decimals() const
MQTTClimateComponent(climate::Climate *device)
state command command command command command command state state state MQTT_COMPONENT_CUSTOM_TOPIC(preset, command) protected bool publish_state_()
climate::Climate * device_
bool send_initial_state() override
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
value_type const & value() const
@ CLIMATE_SUPPORTS_CURRENT_HUMIDITY
@ CLIMATE_SUPPORTS_TARGET_HUMIDITY
@ CLIMATE_SUPPORTS_TWO_POINT_TARGET_TEMPERATURE
@ CLIMATE_SUPPORTS_CURRENT_TEMPERATURE
@ CLIMATE_SUPPORTS_ACTION
@ CLIMATE_REQUIRES_TWO_POINT_TARGET_TEMPERATURE
@ CLIMATE_PRESET_NONE
No preset is active.
@ CLIMATE_PRESET_COMFORT
Device is in comfort preset.
@ CLIMATE_PRESET_AWAY
Device is in away preset.
@ CLIMATE_PRESET_BOOST
Device is in boost preset.
@ CLIMATE_PRESET_ACTIVITY
Device is reacting to activity (e.g., movement sensors)
@ CLIMATE_PRESET_SLEEP
Device is prepared for sleep.
@ CLIMATE_PRESET_HOME
Device is in home preset.
@ CLIMATE_PRESET_ECO
Device is running an energy-saving preset.
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_HORIZONTAL
The fan mode is set to Horizontal.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ CLIMATE_SWING_BOTH
The fan mode is set to Both.
@ CLIMATE_MODE_DRY
The climate device is set to dry/humidity mode.
@ CLIMATE_MODE_FAN_ONLY
The climate device only has the fan enabled, no heating or cooling is taking place.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_HEAT_COOL
The climate device is set to heat/cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
@ CLIMATE_MODE_AUTO
The climate device is adjusting the temperature dynamically.
@ CLIMATE_ACTION_OFF
The climate device is off (inactive or no power)
@ CLIMATE_ACTION_IDLE
The climate device is idle (monitoring climate but no action needed)
@ CLIMATE_ACTION_DRYING
The climate device is drying.
@ CLIMATE_ACTION_HEATING
The climate device is actively heating.
@ CLIMATE_ACTION_COOLING
The climate device is actively cooling.
@ CLIMATE_ACTION_FAN
The climate device is in fan only mode.
@ CLIMATE_FAN_MEDIUM
The fan mode is set to Medium.
@ CLIMATE_FAN_DIFFUSE
The fan mode is set to Diffuse.
@ CLIMATE_FAN_ON
The fan mode is set to On.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
@ CLIMATE_FAN_FOCUS
The fan mode is set to Focus.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_MIDDLE
The fan mode is set to Middle.
@ CLIMATE_FAN_QUIET
The fan mode is set to Quiet.
@ CLIMATE_FAN_OFF
The fan mode is set to Off.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
size_t value_accuracy_to_buf(std::span< char, VALUE_ACCURACY_MAX_LEN > buf, float value, int8_t accuracy_decimals)
Format value with accuracy to buffer, returns chars written (excluding null)
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.
bool state_topic
If the state topic should be included. Defaults to true.