Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
OttoWinter committed Jun 1, 2018
1 parent fb3fe36 commit d44e3ce
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 33 deletions.
10 changes: 4 additions & 6 deletions src/esphomelib/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,9 +735,8 @@ Application::MakeTemplateBinarySensor Application::make_template_binary_sensor(c
#endif

#ifdef USE_TEMPLATE_SWITCH
Application::MakeTemplateSwitch Application::make_template_switch(const std::string &name,
optional<std::function<optional<bool>()>> f) {
auto *template_ = this->register_component(new TemplateSwitch(name, std::move(f)));
Application::MakeTemplateSwitch Application::make_template_switch(const std::string &name) {
auto *template_ = this->register_component(new TemplateSwitch(name));

return MakeTemplateSwitch{
.template_ = template_,
Expand All @@ -758,9 +757,8 @@ cover::MQTTCoverComponent *Application::register_cover(cover::Cover *cover) {
#endif

#ifdef USE_TEMPLATE_COVER
Application::MakeTemplateCover Application::make_template_cover(const std::string &name,
optional<std::function<optional<cover::CoverState>()>> f) {
auto *template_ = this->register_component(new TemplateCover(name, std::move(f)));
Application::MakeTemplateCover Application::make_template_cover(const std::string &name) {
auto *template_ = this->register_component(new TemplateCover(name));

return MakeTemplateCover{
.template_ = template_,
Expand Down
5 changes: 2 additions & 3 deletions src/esphomelib/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ class Application {
switch_::MQTTSwitchComponent *mqtt;
};

MakeTemplateSwitch make_template_switch(const std::string &name, optional<std::function<optional<bool>()>> f = {});
MakeTemplateSwitch make_template_switch(const std::string &name);
#endif


Expand Down Expand Up @@ -919,8 +919,7 @@ class Application {
cover::MQTTCoverComponent *mqtt;
};

MakeTemplateCover make_template_cover(const std::string &name,
optional<std::function<optional<cover::CoverState>()>> f = {});
MakeTemplateCover make_template_cover(const std::string &name);
#endif


Expand Down
3 changes: 3 additions & 0 deletions src/esphomelib/cover/cover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ void Cover::publish_state(CoverState state) {
this->last_state_ = state;
this->state_callback_.call(state);
}
bool Cover::optimistic() {
return false;
}

} // namespace cover

Expand Down
8 changes: 8 additions & 0 deletions src/esphomelib/cover/cover.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ class Cover : public Nameable {
template<typename T>
StopAction<T> *make_stop_action();

/** Return whether this cover is optimistic - i.e. if both the OPEN/CLOSE actions should be displayed in
* Home Assistant because the real state is unknown.
*
* Defaults to false.
*/
virtual bool optimistic();

protected:

CoverState last_state_{COVER_MAX};
CallbackManager<void(CoverState)> state_callback_{};
};
Expand Down
7 changes: 6 additions & 1 deletion src/esphomelib/cover/mqtt_cover_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ static const char *TAG = "cover.mqtt";
MQTTCoverComponent::MQTTCoverComponent(Cover *cover) : cover_(cover) {}
void MQTTCoverComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up MQTT cover '%s'...", this->friendly_name().c_str());
if (this->cover_->optimistic()) {
ESP_LOGCONFIG(TAG, " Optimistic: YES");
}

this->cover_->add_on_publish_state_callback([this](CoverState state) {
const char *state_s;
switch (state) {
Expand Down Expand Up @@ -50,7 +54,8 @@ void MQTTCoverComponent::setup() {
}

void MQTTCoverComponent::send_discovery(JsonBuffer &buffer, JsonObject &root, mqtt::SendDiscoveryConfig &config) {
// Nothing to do here, yay!
if (this->cover_->optimistic())
root["optimistic"] = true;
}
std::string MQTTCoverComponent::component_type() const {
return "cover";
Expand Down
10 changes: 8 additions & 2 deletions src/esphomelib/cover/template_cover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ ESPHOMELIB_NAMESPACE_BEGIN

namespace cover {

TemplateCover::TemplateCover(const std::string &name, optional<std::function<optional<CoverState>()>> f)
: Cover(name), f_(std::move(f)) {
TemplateCover::TemplateCover(const std::string &name)
: Cover(name) {

}
void TemplateCover::open() {
Expand Down Expand Up @@ -50,6 +50,12 @@ void TemplateCover::loop() {
void TemplateCover::set_optimistic(bool optimistic) {
this->optimistic_ = optimistic;
}
bool TemplateCover::optimistic() {
return this->optimistic_;
}
void TemplateCover::set_state_lambda(std::function<optional<CoverState>()> &&f) {
this->f_ = f;
}

} // namespace cover

Expand Down
5 changes: 4 additions & 1 deletion src/esphomelib/cover/template_cover.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ namespace cover {

class TemplateCover : public Cover, public Component {
public:
explicit TemplateCover(const std::string &name, optional<std::function<optional<CoverState>()>> f = {});
explicit TemplateCover(const std::string &name);

void set_state_lambda(std::function<optional<CoverState>()> &&f);
void add_open_actions(const std::vector<Action<NoArg> *> &actions);
void add_close_actions(const std::vector<Action<NoArg> *> &actions);
void add_stop_actions(const std::vector<Action<NoArg> *> &actions);
Expand All @@ -35,6 +36,8 @@ class TemplateCover : public Cover, public Component {
void stop() override;

protected:
bool optimistic() override;

optional<std::function<optional<CoverState>()>> f_;
bool optimistic_{false};
ActionList<NoArg> open_action_;
Expand Down
4 changes: 3 additions & 1 deletion src/esphomelib/sensor/adc_sensor_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ void ADCSensorComponent::set_pin(const GPIOInputPin &pin) {
}
void ADCSensorComponent::update() {
uint16_t value = analogRead(this->pin_.get_pin());
float value_v = value / 4095.0f;
#ifdef ARDUINO_ARCH_ESP32
float value_v = value / 4095.0f;
switch (this->attenuation_) {
case ADC_0db:
value_v *= 1.1;
Expand All @@ -65,6 +65,8 @@ void ADCSensorComponent::update() {
value_v *= 3.9;
break;
}
#else
float value_v = value / 1023.0f;
#endif
this->push_new_value(value_v);
}
Expand Down
2 changes: 1 addition & 1 deletion src/esphomelib/sensor/bme280_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void BME280Component::update() {
float pressure = this->read_pressure_(t_fine);
float humidity = this->read_humidity_(t_fine);

ESP_LOGD(TAG, "Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%",
ESP_LOGD(TAG, "Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%%",
temperature, pressure, humidity);
this->temperature_sensor_->push_new_value(temperature);
this->pressure_sensor_->push_new_value(pressure);
Expand Down
2 changes: 1 addition & 1 deletion src/esphomelib/sensor/bme680_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void BME680Component::read_data_() {
float humidity = this->calc_humidity_(raw_humidity);
float gas_resistance = this->calc_gas_resistance_(raw_gas, gas_range);

ESP_LOGD(TAG, "Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f% gas_resistance=%.1fΩ",
ESP_LOGD(TAG, "Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%% gas_resistance=%.1fΩ",
temperature, pressure, humidity, gas_resistance);
this->temperature_sensor_->push_new_value(temperature);
this->pressure_sensor_->push_new_value(pressure);
Expand Down
14 changes: 13 additions & 1 deletion src/esphomelib/sensor/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,25 @@ ValueRangeTrigger::ValueRangeTrigger(Sensor *parent) {
bool in_range = (isnan(local_min) && value <= local_max) || (isnan(local_max) && value >= local_min)
|| (!isnan(local_min) && !isnan(local_max) && local_min <= value && value <= local_max);

if (in_range != this->previous_in_range_) {
if (in_range != this->previous_in_range_ && in_range) {
this->trigger(value);
}

this->previous_in_range_ = in_range;
});
}
void ValueRangeTrigger::set_min(std::function<float(float)> &&min) {
this->min_ = std::move(min);
}
void ValueRangeTrigger::set_min(float min) {
this->min_ = min;
}
void ValueRangeTrigger::set_max(std::function<float(float)> &&max) {
this->max_ = std::move(max);
}
void ValueRangeTrigger::set_max(float max) {
this->max_ = max;
}

} // namespace sensor

Expand Down
16 changes: 4 additions & 12 deletions src/esphomelib/sensor/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,10 @@ class ValueRangeTrigger : public Trigger<float> {
public:
explicit ValueRangeTrigger(Sensor *parent);

void set_min(std::function<float(float)> &&min) {
this->min_ = std::move(min);
}
void set_min(float min) {
this->min_ = min;
}
void set_max(std::function<float(float)> &&max) {
this->max_ = std::move(max);
}
void set_max(float max) {
this->max_ = max;
}
void set_min(std::function<float(float)> &&min);
void set_min(float min);
void set_max(std::function<float(float)> &&max);
void set_max(float max);

protected:
bool previous_in_range_{false};
Expand Down
2 changes: 1 addition & 1 deletion src/esphomelib/sensor/sht3xd_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void SHT3XDComponent::update() {
float temperature = 175.0f * float(raw_data[0]) / 65535.0f - 45.0f;
float humidity = 100.0f * float(raw_data[1]) / 65535.0f;

ESP_LOGD(TAG, "Got temperature=%.2°C humidity=%.2%");
ESP_LOGD(TAG, "Got temperature=%.2°C humidity=%.2%%");
this->temperature_sensor_->push_new_value(temperature);
this->humidity_sensor_->push_new_value(humidity);
}
Expand Down
5 changes: 5 additions & 0 deletions src/esphomelib/switch_/mqtt_switch_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ MQTTSwitchComponent::MQTTSwitchComponent(switch_::Switch *switch_)
void MQTTSwitchComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up MQTT switch '%s'", this->switch_->get_name().c_str());
ESP_LOGCONFIG(TAG, " Icon: '%s'", this->switch_->get_icon().c_str());
if (this->switch_->optimistic()) {
ESP_LOGCONFIG(TAG, " Optimistic: YES");
}

this->subscribe(this->get_command_topic(), [&](const std::string &payload) {
if (strcasecmp(payload.c_str(), this->get_payload_on().c_str()) == 0)
Expand Down Expand Up @@ -58,6 +61,8 @@ void MQTTSwitchComponent::send_discovery(JsonBuffer &buffer, JsonObject &root, m
root["payload_on"] = this->get_payload_on();
if (this->get_payload_off() != "OFF")
root["payload_off"] = this->get_payload_off();
if (this->switch_->optimistic())
root["optimistic"] = true;
}

} // namespace switch_
Expand Down
3 changes: 3 additions & 0 deletions src/esphomelib/switch_/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ void Switch::publish_state(bool state) {
global_preferences.put_bool(this->get_name(), "state", state);
this->state_callback_.call(state);
}
bool Switch::optimistic() {
return false;
}

} // namespace switch_

Expand Down
7 changes: 7 additions & 0 deletions src/esphomelib/switch_/switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class Switch : public binary_sensor::BinarySensor, public Component {
template<typename T>
TurnOnAction<T> *make_turn_on_action();

/** Return whether this switch is optimistic - i.e. if both the ON/OFF actions should be displayed in Home Assistant
* because the real state is unknown.
*
* Defaults to false.
*/
virtual bool optimistic();

protected:
/// Turn this switch on. When creating a switch, you should implement this (inversion will already be applied).
virtual void turn_on() = 0;
Expand Down
10 changes: 8 additions & 2 deletions src/esphomelib/switch_/template_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ ESPHOMELIB_NAMESPACE_BEGIN

namespace switch_ {

TemplateSwitch::TemplateSwitch(const std::string &name, optional<std::function<optional<bool>()>> f)
: Switch(name), f_(std::move(f)) {
TemplateSwitch::TemplateSwitch(const std::string &name)
: Switch(name) {

}
void TemplateSwitch::loop() {
Expand Down Expand Up @@ -44,6 +44,12 @@ void TemplateSwitch::add_turn_off_actions(const std::vector<Action<NoArg> *> &ac
void TemplateSwitch::set_optimistic(bool optimistic) {
this->optimistic_ = optimistic;
}
bool TemplateSwitch::optimistic() {
return this->optimistic_;
}
void TemplateSwitch::set_state_lambda(std::function<optional<bool>()> &&f) {
this->f_ = f;
}

} // namespace switch_

Expand Down
5 changes: 4 additions & 1 deletion src/esphomelib/switch_/template_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ namespace switch_ {

class TemplateSwitch : public Switch {
public:
explicit TemplateSwitch(const std::string &name, optional<std::function<optional<bool>()>> f = {});
explicit TemplateSwitch(const std::string &name);

void set_state_lambda(std::function<optional<bool>()> &&f);
void add_turn_on_actions(const std::vector<Action<NoArg> *> &actions);
void add_turn_off_actions(const std::vector<Action<NoArg> *> &actions);
void set_optimistic(bool optimistic);
void loop() override;

protected:
bool optimistic() override;

void turn_on() override;
void turn_off() override;

Expand Down

0 comments on commit d44e3ce

Please sign in to comment.