mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
Allow id() syntax for custom code (#621)
* Allow id() syntax for custom code * Lint
This commit is contained in:
parent
726b0e73d9
commit
4fe0c95ccb
4 changed files with 20 additions and 3 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace globals {
|
||||
|
@ -64,5 +65,7 @@ template<class C, typename... Ts> class GlobalVarSetAction : public Action<Ts...
|
|||
C *parent_;
|
||||
};
|
||||
|
||||
template<typename T> T &id(GlobalsComponent<T> *value) { return value->value(); }
|
||||
|
||||
} // namespace globals
|
||||
} // namespace esphome
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <functional>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
#include "esphome/core/optional.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... 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;
|
||||
|
||||
/** 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
|
||||
};
|
||||
|
||||
template<bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
|
||||
|
||||
template<typename T, typename... X> class TemplatableValue {
|
||||
public:
|
||||
TemplatableValue() : type_(EMPTY) {}
|
||||
|
|
|
@ -3,7 +3,12 @@
|
|||
|
||||
class CustomSensor : public Component, public Sensor {
|
||||
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 {
|
||||
|
|
|
@ -246,6 +246,11 @@ binary_sensor:
|
|||
- id: custom_binary_sensor
|
||||
name: Custom Binary Sensor
|
||||
|
||||
globals:
|
||||
- id: my_global_string
|
||||
type: std::string
|
||||
initial_value: '""'
|
||||
|
||||
remote_receiver:
|
||||
pin: GPIO12
|
||||
dump: []
|
||||
|
|
Loading…
Reference in a new issue