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_temperature_step();
75 if (traits.get_supports_action()) {
80 if (traits.get_supports_fan_modes() || !traits.get_supported_custom_fan_modes().empty()) {
86 JsonArray fan_modes = root.createNestedArray(
"fan_modes");
92 fan_modes.add(
"auto");
96 fan_modes.add(
"medium");
98 fan_modes.add(
"high");
100 fan_modes.add(
"middle");
102 fan_modes.add(
"focus");
104 fan_modes.add(
"diffuse");
105 for (
const auto &
fan_mode : traits.get_supported_custom_fan_modes())
109 if (traits.get_supports_swing_modes()) {
115 JsonArray swing_modes = root.createNestedArray(
"swing_modes");
117 swing_modes.add(
"off");
119 swing_modes.add(
"both");
121 swing_modes.add(
"vertical");
123 swing_modes.add(
"horizontal");
130 auto traits = this->device_->get_traits();
131 this->subscribe(this->get_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
132 auto call = this->device_->make_call();
133 call.set_mode(payload);
137 if (traits.get_supports_two_point_target_temperature()) {
138 this->subscribe(this->get_target_temperature_low_command_topic(),
139 [
this](
const std::string &topic,
const std::string &payload) {
140 auto val = parse_number<float>(payload);
141 if (!
val.has_value()) {
142 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
145 auto call = this->device_->make_call();
146 call.set_target_temperature_low(*
val);
149 this->subscribe(this->get_target_temperature_high_command_topic(),
150 [
this](
const std::string &topic,
const std::string &payload) {
151 auto val = parse_number<float>(payload);
152 if (!
val.has_value()) {
153 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
156 auto call = this->device_->make_call();
157 call.set_target_temperature_high(*
val);
161 this->subscribe(this->get_target_temperature_command_topic(),
162 [
this](
const std::string &topic,
const std::string &payload) {
163 auto val = parse_number<float>(payload);
164 if (!
val.has_value()) {
165 ESP_LOGW(TAG,
"Can't convert '%s' to number!", payload.c_str());
168 auto call = this->device_->make_call();
169 call.set_target_temperature(*
val);
175 this->subscribe(this->get_away_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
177 auto call = this->device_->make_call();
190 ESP_LOGW(TAG,
"Unknown payload '%s'", payload.c_str());
197 if (traits.get_supports_fan_modes()) {
198 this->subscribe(this->get_fan_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
199 auto call = this->device_->make_call();
200 call.set_fan_mode(payload);
205 if (traits.get_supports_swing_modes()) {
206 this->subscribe(this->get_swing_mode_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
207 auto call = this->device_->make_call();
208 call.set_swing_mode(payload);
213 this->device_->add_on_state_callback([
this]() { this->publish_state_(); });
223 const char *mode_s =
"";
244 mode_s =
"heat_cool";
248 if (!this->
publish(this->get_mode_state_topic(), mode_s))
250 int8_t accuracy = traits.get_temperature_accuracy_decimals();
253 if (!this->
publish(this->get_current_temperature_state_topic(), payload))
256 if (traits.get_supports_two_point_target_temperature()) {
258 if (!this->
publish(this->get_target_temperature_low_state_topic(), payload))
261 if (!this->
publish(this->get_target_temperature_high_state_topic(), payload))
265 if (!this->
publish(this->get_target_temperature_state_topic(), payload))
271 if (!this->
publish(this->get_away_state_topic(), payload))
274 if (traits.get_supports_action()) {
275 const char *payload =
"unknown";
296 if (!this->
publish(this->get_action_state_topic(), payload))
300 if (traits.get_supports_fan_modes()) {
335 if (!this->
publish(this->get_fan_mode_state_topic(), payload))
339 if (traits.get_supports_swing_modes()) {
340 const char *payload =
"";
349 payload =
"vertical";
352 payload =
"horizontal";
355 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
constexpr const char *const MQTT_AWAY_MODE_COMMAND_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
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.
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
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.
constexpr const char *const MQTT_AWAY_MODE_STATE_TOPIC
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_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.