12 static const char *
const TAG =
"mqtt.climate";
17 auto traits = this->device_->get_traits();
19 if (traits.get_supports_current_temperature()) {
28 JsonArray modes = root.createNestedArray(
MQTT_MODES);
38 modes.add(
"fan_only");
42 modes.add(
"heat_cool");
44 if (traits.get_supports_two_point_target_temperature()) {
65 root[
"temp_step"] = traits.get_visual_target_temperature_step();
69 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
75 JsonArray presets = root.createNestedArray(
"preset_modes");
83 presets.add(
"comfort");
89 presets.add(
"activity");
90 for (
const auto &
preset : traits.get_supported_custom_presets())
94 if (traits.get_supports_action()) {
99 if (traits.get_supports_fan_modes()) {
105 JsonArray fan_modes = root.createNestedArray(
"fan_modes");
109 fan_modes.add(
"off");
111 fan_modes.add(
"auto");
113 fan_modes.add(
"low");
115 fan_modes.add(
"medium");
117 fan_modes.add(
"high");
119 fan_modes.add(
"middle");
121 fan_modes.add(
"focus");
123 fan_modes.add(
"diffuse");
125 fan_modes.add(
"quiet");
126 for (
const auto &
fan_mode : traits.get_supported_custom_fan_modes())
130 if (traits.get_supports_swing_modes()) {
136 JsonArray swing_modes = root.createNestedArray(
"swing_modes");
138 swing_modes.add(
"off");
140 swing_modes.add(
"both");
142 swing_modes.add(
"vertical");
144 swing_modes.add(
"horizontal");
151 auto traits = this->device_->get_traits();
152 this->subscribe(this->get_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
153 auto call = this->device_->make_call();
154 call.set_mode(payload);
158 if (traits.get_supports_two_point_target_temperature()) {
159 this->subscribe(this->get_target_temperature_low_command_topic(),
160 [
this](
const std::string &topic,
const std::string &payload) {
161 auto val = parse_number<float>(payload);
162 if (!
val.has_value()) {
163 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
166 auto call = this->device_->make_call();
167 call.set_target_temperature_low(*
val);
170 this->subscribe(this->get_target_temperature_high_command_topic(),
171 [
this](
const std::string &topic,
const std::string &payload) {
172 auto val = parse_number<float>(payload);
173 if (!
val.has_value()) {
174 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
177 auto call = this->device_->make_call();
178 call.set_target_temperature_high(*
val);
182 this->subscribe(this->get_target_temperature_command_topic(),
183 [
this](
const std::string &topic,
const std::string &payload) {
184 auto val = parse_number<float>(payload);
185 if (!
val.has_value()) {
186 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
189 auto call = this->device_->make_call();
190 call.set_target_temperature(*
val);
195 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
196 this->subscribe(this->get_preset_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
197 auto call = this->device_->make_call();
198 call.set_preset(payload);
203 if (traits.get_supports_fan_modes()) {
204 this->subscribe(this->get_fan_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
205 auto call = this->device_->make_call();
206 call.set_fan_mode(payload);
211 if (traits.get_supports_swing_modes()) {
212 this->subscribe(this->get_swing_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
213 auto call = this->device_->make_call();
214 call.set_swing_mode(payload);
219 this->device_->add_on_state_callback([
this](
Climate & ) { this->publish_state_(); });
229 const char *mode_s =
"";
250 mode_s =
"heat_cool";
254 if (!this->
publish(this->get_mode_state_topic(), mode_s))
256 int8_t target_accuracy = traits.get_target_temperature_accuracy_decimals();
257 int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
260 if (!this->
publish(this->get_current_temperature_state_topic(), payload))
263 if (traits.get_supports_two_point_target_temperature()) {
265 if (!this->
publish(this->get_target_temperature_low_state_topic(), payload))
268 if (!this->
publish(this->get_target_temperature_high_state_topic(), payload))
272 if (!this->
publish(this->get_target_temperature_state_topic(), payload))
276 if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
302 payload =
"activity";
308 if (!this->
publish(this->get_preset_state_topic(), payload))
312 if (traits.get_supports_action()) {
313 const char *payload =
"unknown";
334 if (!this->
publish(this->get_action_state_topic(), payload))
338 if (traits.get_supports_fan_modes()) {
376 if (!this->
publish(this->get_fan_mode_state_topic(), payload))
380 if (traits.get_supports_swing_modes()) {
381 const char *payload =
"";
390 payload =
"vertical";
393 payload =
"horizontal";
396 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
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_ACTION_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
const EntityBase * get_entity() const override
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
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_
Simple Helper struct used for Home Assistant MQTT send_discovery().
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
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.