Allow id() syntax for custom code (#621)

* Allow id() syntax for custom code

* Lint
This commit is contained in:
Otto Winter 2019-06-07 14:26:17 +02:00 committed by GitHub
parent 726b0e73d9
commit 4fe0c95ccb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 3 deletions

View file

@ -2,6 +2,7 @@
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/automation.h" #include "esphome/core/automation.h"
#include "esphome/core/helpers.h"
namespace esphome { namespace esphome {
namespace globals { namespace globals {
@ -64,5 +65,7 @@ template<class C, typename... Ts> class GlobalVarSetAction : public Action<Ts...
C *parent_; C *parent_;
}; };
template<typename T> T &id(GlobalsComponent<T> *value) { return value->value(); }
} // namespace globals } // namespace globals
} // namespace esphome } // namespace esphome

View file

@ -4,6 +4,7 @@
#include <functional> #include <functional>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <type_traits>
#include "esphome/core/optional.h" #include "esphome/core/optional.h"
#include "esphome/core/esphal.h" #include "esphome/core/esphal.h"
@ -160,6 +161,11 @@ template<int...> struct seq {}; // NOLINT
template<int N, int... S> struct gens : gens<N - 1, N - 1, S...> {}; // NOLINT template<int N, int... S> struct gens : gens<N - 1, N - 1, S...> {}; // NOLINT
template<int... S> struct gens<0, S...> { using type = seq<S...>; }; // NOLINT template<int... S> struct gens<0, S...> { using type = seq<S...>; }; // NOLINT
template<bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
template<typename T, enable_if_t<!std::is_pointer<T>::value, int> = 0> T id(T value) { return value; }
template<typename T, enable_if_t<std::is_pointer<T *>::value, int> = 0> T &id(T *value) { return *value; }
template<typename... X> class CallbackManager; template<typename... X> class CallbackManager;
/** Simple helper class to allow having multiple subscribers to a signal. /** Simple helper class to allow having multiple subscribers to a signal.
@ -192,8 +198,6 @@ struct is_callable // NOLINT
static constexpr auto value = decltype(test<T>(nullptr))::value; // NOLINT static constexpr auto value = decltype(test<T>(nullptr))::value; // NOLINT
}; };
template<bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
template<typename T, typename... X> class TemplatableValue { template<typename T, typename... X> class TemplatableValue {
public: public:
TemplatableValue() : type_(EMPTY) {} TemplatableValue() : type_(EMPTY) {}

View file

@ -3,7 +3,12 @@
class CustomSensor : public Component, public Sensor { class CustomSensor : public Component, public Sensor {
public: public:
void loop() override { publish_state(42.0); } void loop() override {
// Test id() helper
float value = id(my_sensor).state;
id(my_global_string) = "Hello World";
publish_state(42.0);
}
}; };
class CustomTextSensor : public Component, public TextSensor { class CustomTextSensor : public Component, public TextSensor {

View file

@ -246,6 +246,11 @@ binary_sensor:
- id: custom_binary_sensor - id: custom_binary_sensor
name: Custom Binary Sensor name: Custom Binary Sensor
globals:
- id: my_global_string
type: std::string
initial_value: '""'
remote_receiver: remote_receiver:
pin: GPIO12 pin: GPIO12
dump: [] dump: []