#include "esphome.h"
#include "tca9548a.h"
#include <Wire.h>
#include <Adafruit_seesaw.h>

class LocalPlantsSensor : public PollingComponent, public sensor::Sensor {

 private:
   Adafruit_seesaw plant_1_ss;

 public:
  sensor::Sensor *plant_1_sensor = new sensor::Sensor();
  sensor::Sensor *plant_2_sensor = new sensor::Sensor();
  sensor::Sensor *plant_3_sensor = new sensor::Sensor();
  sensor::Sensor *plant_4_sensor = new sensor::Sensor();
  sensor::Sensor *plant_5_sensor = new sensor::Sensor();
  sensor::Sensor *plant_6_sensor = new sensor::Sensor();
  sensor::Sensor *plant_7_sensor = new sensor::Sensor();
  sensor::Sensor *plant_8_sensor = new sensor::Sensor();

  // 4294967295UL == never per sources
  LocalPlantsSensor() : PollingComponent(60*60*1000) { }

  void setup() override {
      // Plant 1 : Adafruit STEMMA Soil Sensor
      enableMuxPort(1);
      plant_1_ss.begin(0x36);
      disableMuxPort(1);
  }

  void update() override {
    enableMuxPort(0);
    double plant_1_value = -1;
    disableMuxPort(0);
    plant_1_sensor->publish_state(plant_1_value);

    enableMuxPort(1);
    double plant_2_value = plant_1_ss.touchRead(0);
    disableMuxPort(1);
    plant_2_sensor->publish_state(plant_2_value);

    enableMuxPort(2);
    double plant_3_value = -1;
    disableMuxPort(2);
    plant_3_sensor->publish_state(plant_3_value);

    enableMuxPort(3);
    double plant_4_value = -1;
    disableMuxPort(3);
    plant_4_sensor->publish_state(plant_4_value);

    enableMuxPort(4);
    double plant_5_value = -1;
    disableMuxPort(4);
    plant_5_sensor->publish_state(plant_5_value);

    enableMuxPort(5);
    double plant_6_value = -1;
    disableMuxPort(5);
    plant_6_sensor->publish_state(plant_6_value);

    enableMuxPort(6);
    double plant_7_value = -1;
    disableMuxPort(6);
    plant_7_sensor->publish_state(plant_7_value);

    enableMuxPort(7);
    double plant_8_value = -1;
    disableMuxPort(7);
    plant_8_sensor->publish_state(plant_8_value);
  }
};
