Defining types to be used in globals

I find it much easier to do non trivial logic using state machines, which are easy to model using enums, sadly the globals component does not allow for types defined in esphome.includes headers. Same applies to structs or any other types. The issue that prevents types in esphome.includes to be used in globals is that the globals are declared in main.cpp before the #include statement.

Right now I’m doing some evil code injection to define types before globals, while this works right now and hasn’t caused any problems, I don’t expect this to always work.

which produces the following code in main.cpp:

...
LambdaAction<> *lambdaaction_id_2;
globals::GlobalsComponent<
  // BEGIN HACKY WORKAROUND
  #if !defined HACKY_GLOBALS_TYPE_DEF___dummy_id_action_queue__
  void*> *__dummy_id_action_queue__;
  #define HACKY_GLOBALS_TYPE_DEF___dummy_id_action_queue__
  #include "action_queue.hpp"
  #else
  void*>(nullptr);
  #endif
  // END HACKY WORKAROUND
//> *__dummy_id_action_queue__;
globals::GlobalsComponent<action_queue::SingleDepthBinaryActionQueue> *switch_action_queue;
LambdaAction<climate::Climate &> *lambdaaction_id_3;
...

Could the order in which globals are declared and headers included be a solution? Will it cause any issues (besides globals not being available in headers)? Could as switch be added perhaps to allow this order to be switched? Say a file considered a “header” is included near top includes, while a file containing function definitions could be added after the globals are declared, like usual.

1 Like