Files
esphome-dev/esphome/components/mhz19/mhz19.h
T
Andrew Zaborowski 2f07225984 Fix: Component script not stopped in certain situations (#1004)
* Move stop/is_running implementation to Action base class

Try to fix issue #1105.  Until now if an UpdateComponentAction, a
LambdaAction, or any action that can contain those inside their
"else" or "then" action lists, resulted in a call to script.stop on
the script that contains them, the script would continue running,
because they didn't implement a stop() method.  Basically only
the asynchronous ones did: DelayAction, WaitUntilAction and
ScriptWaitAction.

With this change num_running_ in Action replaces
DelayAction::num_running_ and WaitUntilAction::triggered_ to provide
the same is_running logic to other actions.

* Make some Action methods protected

Apparently play()/stop() etc. are not meant to be called directly by
users of the class and if they're called directly that would not give
the expected result for the classes that have an empty play().

Make all methods except play_complex, stop_comples and is_running
protected.  While there also make RemoteTransmitterActionBase::encode
protected.

* lint

* format

Co-authored-by: Guillermo Ruffino <glm.net@gmail.com>
2020-06-28 19:44:15 -03:00

69 lines
1.9 KiB
C++

#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
namespace esphome {
namespace mhz19 {
enum MHZ19ABCLogic { MHZ19_ABC_NONE = 0, MHZ19_ABC_ENABLED, MHZ19_ABC_DISABLED };
class MHZ19Component : public PollingComponent, public uart::UARTDevice {
public:
float get_setup_priority() const override;
void setup() override;
void update() override;
void dump_config() override;
void calibrate_zero();
void abc_enable();
void abc_disable();
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
void set_co2_sensor(sensor::Sensor *co2_sensor) { co2_sensor_ = co2_sensor; }
void set_abc_enabled(bool abc_enabled) { abc_boot_logic_ = abc_enabled ? MHZ19_ABC_ENABLED : MHZ19_ABC_DISABLED; }
protected:
bool mhz19_write_command_(const uint8_t *command, uint8_t *response);
sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *co2_sensor_{nullptr};
MHZ19ABCLogic abc_boot_logic_{MHZ19_ABC_NONE};
};
template<typename... Ts> class MHZ19CalibrateZeroAction : public Action<Ts...> {
public:
MHZ19CalibrateZeroAction(MHZ19Component *mhz19) : mhz19_(mhz19) {}
void play(Ts... x) override { this->mhz19_->calibrate_zero(); }
protected:
MHZ19Component *mhz19_;
};
template<typename... Ts> class MHZ19ABCEnableAction : public Action<Ts...> {
public:
MHZ19ABCEnableAction(MHZ19Component *mhz19) : mhz19_(mhz19) {}
void play(Ts... x) override { this->mhz19_->abc_enable(); }
protected:
MHZ19Component *mhz19_;
};
template<typename... Ts> class MHZ19ABCDisableAction : public Action<Ts...> {
public:
MHZ19ABCDisableAction(MHZ19Component *mhz19) : mhz19_(mhz19) {}
void play(Ts... x) override { this->mhz19_->abc_disable(); }
protected:
MHZ19Component *mhz19_;
};
} // namespace mhz19
} // namespace esphome