forked from esphome/esphome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add ClimateIR * update climate ir * update class comment * lint * moved to climate_ir * fix include path * use climateir * updates * update include path * lint * fixed variable assigned to itself
- Loading branch information
1 parent
1242f43
commit 578e5a0
Showing
6 changed files
with
121 additions
and
82 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "climate_ir.h" | ||
|
||
namespace esphome { | ||
namespace climate { | ||
|
||
climate::ClimateTraits ClimateIR::traits() { | ||
auto traits = climate::ClimateTraits(); | ||
traits.set_supports_current_temperature(this->sensor_ != nullptr); | ||
traits.set_supports_auto_mode(true); | ||
traits.set_supports_cool_mode(this->supports_cool_); | ||
traits.set_supports_heat_mode(this->supports_heat_); | ||
traits.set_supports_two_point_target_temperature(false); | ||
traits.set_supports_away(false); | ||
traits.set_visual_min_temperature(this->minimum_temperature_); | ||
traits.set_visual_max_temperature(this->maximum_temperature_); | ||
traits.set_visual_temperature_step(this->temperature_step_); | ||
return traits; | ||
} | ||
|
||
void ClimateIR::setup() { | ||
if (this->sensor_) { | ||
this->sensor_->add_on_state_callback([this](float state) { | ||
this->current_temperature = state; | ||
// current temperature changed, publish state | ||
this->publish_state(); | ||
}); | ||
this->current_temperature = this->sensor_->state; | ||
} else | ||
this->current_temperature = NAN; | ||
// restore set points | ||
auto restore = this->restore_state_(); | ||
if (restore.has_value()) { | ||
restore->apply(this); | ||
} else { | ||
// restore from defaults | ||
this->mode = climate::CLIMATE_MODE_OFF; | ||
// initialize target temperature to some value so that it's not NAN | ||
this->target_temperature = | ||
roundf(clamp(this->current_temperature, this->minimum_temperature_, this->maximum_temperature_)); | ||
} | ||
// Never send nan to HA | ||
if (isnan(this->target_temperature)) | ||
this->target_temperature = 24; | ||
} | ||
|
||
void ClimateIR::control(const climate::ClimateCall &call) { | ||
if (call.get_mode().has_value()) | ||
this->mode = *call.get_mode(); | ||
if (call.get_target_temperature().has_value()) | ||
this->target_temperature = *call.get_target_temperature(); | ||
|
||
this->transmit_state(); | ||
this->publish_state(); | ||
} | ||
|
||
} // namespace climate | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#pragma once | ||
|
||
#include "esphome/components/climate/climate.h" | ||
#include "esphome/components/remote_base/remote_base.h" | ||
#include "esphome/components/remote_transmitter/remote_transmitter.h" | ||
#include "esphome/components/sensor/sensor.h" | ||
|
||
namespace esphome { | ||
namespace climate { | ||
|
||
/* A base for climate which works by sending (and receiving) IR codes | ||
To send IR codes implement | ||
void ClimateIR::transmit_state_() | ||
Likewise to decode a IR into the AC state, implement | ||
bool RemoteReceiverListener::on_receive(remote_base::RemoteReceiveData data) and return true | ||
*/ | ||
class ClimateIR : public climate::Climate, public Component, public remote_base::RemoteReceiverListener { | ||
public: | ||
ClimateIR(float minimum_temperature, float maximum_temperature, float temperature_step = 1.0f) { | ||
this->minimum_temperature_ = minimum_temperature; | ||
this->maximum_temperature_ = maximum_temperature; | ||
this->temperature_step_ = temperature_step; | ||
} | ||
|
||
void setup() override; | ||
void set_transmitter(remote_transmitter::RemoteTransmitterComponent *transmitter) { | ||
this->transmitter_ = transmitter; | ||
} | ||
void set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; } | ||
void set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; } | ||
void set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; } | ||
|
||
protected: | ||
float minimum_temperature_, maximum_temperature_, temperature_step_; | ||
|
||
/// Override control to change settings of the climate device. | ||
void control(const climate::ClimateCall &call) override; | ||
/// Return the traits of this controller. | ||
climate::ClimateTraits traits() override; | ||
|
||
/// Transmit via IR the state of this climate controller. | ||
virtual void transmit_state() {} | ||
|
||
bool supports_cool_{true}; | ||
bool supports_heat_{true}; | ||
|
||
remote_transmitter::RemoteTransmitterComponent *transmitter_; | ||
sensor::Sensor *sensor_{nullptr}; | ||
}; | ||
} // namespace climate | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters