Advertisement
Otisburgh_its

Foobar.h

Jun 1st, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include "esphome.h"
  2.  
  3. class Foobar : public PollingComponent, public Sensor {
  4.  public:
  5.  
  6.   Foobar(int poll=5000) : PollingComponent(poll){}
  7.  
  8.   int fubar=0;
  9.   int multiplier=0;
  10.  
  11.   void bar(int x)
  12.   {
  13.      multiplier=x;
  14.      ESP_LOGD("custom", "multiplier changed to: %i",  multiplier);
  15.   }
  16.  
  17.   void setup() override {
  18.      bar(1);
  19.   }
  20.  
  21.   void update() override {
  22.      if (((rand() % 5) + 1) == 5)   //1 out of 5 times throw a random number into things
  23.      {
  24.         int x;
  25.         do {
  26.            x=(rand() % 5) + 1;
  27.         } while (multiplier==x);
  28.         ESP_LOGD("TRICKERY!!!", "multiplier changed!!");
  29.         bar(x);
  30.         // hey, multiplier isn't the same value that was passed in.
  31.         // we should somehow report this new value back to Home Assistant
  32.      }
  33.  
  34.      fubar += (1*multiplier);
  35.      publish_state(fubar);
  36.      ESP_LOGD("custom", "fubar: %i", fubar);
  37.   }
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement