// Auto generated code by esphome
// ========== AUTO GENERATED INCLUDE BLOCK BEGIN ===========
#include "esphome.h"
using namespace esphome;
using namespace time;
using namespace text_sensor;
logger::Logger *logger_logger;
web_server_base::WebServerBase *web_server_base_webserverbase;
captive_portal::CaptivePortal *captive_portal_captiveportal;
wifi::WiFiComponent *wifi_wificomponent;
ota::OTAComponent *ota_otacomponent;
api::APIServer *api_apiserver;
using namespace api;
web_server::WebServer *web_server_webserver;
using namespace json;
homeassistant::HomeassistantTime *esptime;
sun::Sun *sun_sun;
sun::SunTextSensor *sun_suntextsensor;
sun::SunTextSensor *sun_suntextsensor_2;
version::VersionTextSensor *version_versiontextsensor;
text_sensor::TextSensorStateTrigger *text_sensor_textsensorstatetrigger;
Automation<std::string> *automation_3;
template_::TemplateTextSensor *template__templatetextsensor;
template_::TemplateTextSensor *sunrise_shift;
template_::TemplateTextSensor *sunset_shift;
template_::TemplateTextSensor *template__templatetextsensor_2;
LambdaAction<std::string> *lambdaaction_3;
sun::SunTrigger *sun_suntrigger;
Automation<> *automation;
LambdaAction<> *lambdaaction;
sun::SunTrigger *sun_suntrigger_2;
Automation<> *automation_2;
LambdaAction<> *lambdaaction_2;
#include "MySunTextSensor.h"
// ========== AUTO GENERATED INCLUDE BLOCK END ==========="

void setup() {
  // ===== DO NOT EDIT ANYTHING BELOW THIS LINE =====
  // ========== AUTO GENERATED CODE BEGIN ===========
  // async_tcp:
  // esphome:
  //   name: livingroom
  //   platform: ESP8266
  //   board: esp01_1m
  //   includes:
  //   - MySunTextSensor.h
  //   arduino_version: espressif8266@2.6.2
  //   build_path: livingroom
  //   platformio_options: {}
  //   esp8266_restore_from_flash: false
  //   board_flash_mode: dout
  //   libraries: []
  App.pre_setup("livingroom", __DATE__ ", " __TIME__);
  // time:
  // text_sensor:
  // logger:
  //   id: logger_logger
  //   baud_rate: 115200
  //   tx_buffer_size: 512
  //   hardware_uart: UART0
  //   level: DEBUG
  //   logs: {}
  //   esp8266_store_log_strings_in_flash: true
  logger_logger = new logger::Logger(115200, 512, logger::UART_SELECTION_UART0);
  logger_logger->pre_setup();
  App.register_component(logger_logger);
  // web_server_base:
  //   id: web_server_base_webserverbase
  web_server_base_webserverbase = new web_server_base::WebServerBase();
  App.register_component(web_server_base_webserverbase);
  // captive_portal:
  //   id: captive_portal_captiveportal
  //   web_server_base_id: web_server_base_webserverbase
  captive_portal_captiveportal = new captive_portal::CaptivePortal(web_server_base_webserverbase);
  App.register_component(captive_portal_captiveportal);
  // wifi:
  //   ap:
  //     ssid: Livingroom Fallback Hotspot
  //     password: R8ZKoXbtquSv
  //     id: wifi_wifiap
  //     ap_timeout: 1min
  //   id: wifi_wificomponent
  //   domain: .local
  //   reboot_timeout: 15min
  //   power_save_mode: NONE
  //   fast_connect: false
  //   output_power: 20.0
  //   networks:
  //   - ssid: WLAN-LX
  //     password: SP-RAXX749x7
  //     id: wifi_wifiap_2
  //     priority: 0.0
  //   use_address: livingroom.local
  wifi_wificomponent = new wifi::WiFiComponent();
  wifi_wificomponent->set_use_address("livingroom.local");
  wifi::WiFiAP wifi_wifiap_2 = wifi::WiFiAP();
  wifi_wifiap_2.set_ssid("WLAN-LX");
  wifi_wifiap_2.set_password("SP-RAXX749x7");
  wifi_wifiap_2.set_priority(0.0f);
  wifi_wificomponent->add_sta(wifi_wifiap_2);
  wifi::WiFiAP wifi_wifiap = wifi::WiFiAP();
  wifi_wifiap.set_ssid("Livingroom Fallback Hotspot");
  wifi_wifiap.set_password("R8ZKoXbtquSv");
  wifi_wificomponent->set_ap(wifi_wifiap);
  wifi_wificomponent->set_ap_timeout(60000);
  wifi_wificomponent->set_reboot_timeout(900000);
  wifi_wificomponent->set_power_save_mode(wifi::WIFI_POWER_SAVE_NONE);
  wifi_wificomponent->set_fast_connect(false);
  wifi_wificomponent->set_output_power(20.0f);
  App.register_component(wifi_wificomponent);
  // ota:
  //   id: ota_otacomponent
  //   safe_mode: true
  //   port: 8266
  //   password: ''
  //   reboot_timeout: 5min
  //   num_attempts: 10
  ota_otacomponent = new ota::OTAComponent();
  ota_otacomponent->set_port(8266);
  ota_otacomponent->set_auth_password("");
  App.register_component(ota_otacomponent);
  if (ota_otacomponent->should_enter_safe_mode(10, 300000)) return;
  // api:
  //   id: api_apiserver
  //   port: 6053
  //   password: ''
  //   reboot_timeout: 15min
  api_apiserver = new api::APIServer();
  App.register_component(api_apiserver);
  // web_server:
  //   port: 80
  //   id: web_server_webserver
  //   css_url: https:esphome.io/_static/webserver-v1.min.css
  //   js_url: https:esphome.io/_static/webserver-v1.min.js
  //   web_server_base_id: web_server_base_webserverbase
  api_apiserver->set_port(6053);
  api_apiserver->set_password("");
  api_apiserver->set_reboot_timeout(900000);
  web_server_webserver = new web_server::WebServer(web_server_base_webserverbase);
  App.register_component(web_server_webserver);
  web_server_base_webserverbase->set_port(80);
  web_server_webserver->set_css_url("https://esphome.io/_static/webserver-v1.min.css");
  web_server_webserver->set_js_url("https://esphome.io/_static/webserver-v1.min.js");
  // json:
  // substitutions:
  //   shift_sunset: '+17'
  //   shift_sunrise: '+6'
  // time.homeassistant:
  //   platform: homeassistant
  //   timezone: CET-1CEST-2,M3.4.0/2,M10.5.0/3
  //   id: esptime
  //   update_interval: 15min
  esptime = new homeassistant::HomeassistantTime();
  esptime->set_timezone("CET-1CEST-2,M3.4.0/2,M10.5.0/3");
  // sun:
  //   latitude: 48.77034
  //   longitude: 8.34306
  //   on_sunrise:
  //   - then:
  //     - logger.log:
  //         format: Good morning!
  //         args: []
  //         level: DEBUG
  //         tag: main
  //       type_id: lambdaaction
  //     automation_id: automation
  //     trigger_id: sun_suntrigger
  //     elevation: -0.883
  //   on_sunset:
  //   - then:
  //     - logger.log:
  //         format: Good evening!
  //         args: []
  //         level: DEBUG
  //         tag: main
  //       type_id: lambdaaction_2
  //     automation_id: automation_2
  //     trigger_id: sun_suntrigger_2
  //     elevation: -0.883
  //   id: sun_sun
  //   time_id: esptime
  sun_sun = new sun::Sun();
  // text_sensor.sun:
  //   platform: sun
  //   name: Sun Next Sunrise
  //   type: sunrise
  //   id: sun_suntextsensor
  //   sun_id: sun_sun
  //   elevation: -0.883
  //   format: '%X'
  //   update_interval: 60s
  //   icon: mdi:weather-sunset-up
  sun_suntextsensor = new sun::SunTextSensor();
  sun_suntextsensor->set_update_interval(60000);
  App.register_component(sun_suntextsensor);
  // text_sensor.sun:
  //   platform: sun
  //   name: Sun Next Sunset
  //   type: sunset
  //   id: sun_suntextsensor_2
  //   sun_id: sun_sun
  //   elevation: -0.883
  //   format: '%X'
  //   update_interval: 60s
  //   icon: mdi:weather-sunset-down
  sun_suntextsensor_2 = new sun::SunTextSensor();
  sun_suntextsensor_2->set_update_interval(60000);
  App.register_component(sun_suntextsensor_2);
  // text_sensor.version:
  //   platform: version
  //   name: Platform
  //   on_value:
  //   - then:
  //     - lambda: !lambda "ESP_LOGD(\"main\", \"The current version is %s\", x.c_str()); "
  //       type_id: lambdaaction_3
  //     automation_id: automation_3
  //     trigger_id: text_sensor_textsensorstatetrigger
  //   id: version_versiontextsensor
  //   icon: mdi:new-box
  //   hide_timestamp: false
  version_versiontextsensor = new version::VersionTextSensor();
  App.register_text_sensor(version_versiontextsensor);
  version_versiontextsensor->set_name("Platform");
  version_versiontextsensor->set_icon("mdi:new-box");
  text_sensor_textsensorstatetrigger = new text_sensor::TextSensorStateTrigger(version_versiontextsensor);
  automation_3 = new Automation<std::string>(text_sensor_textsensorstatetrigger);
  // text_sensor.template:
  //   platform: template
  //   name: Template Text Sensor
  //   lambda: !lambda |-
  //     char str[17];
  //     time_t currTime = id(esptime).now().timestamp ;
  //     strftime(str, sizeof(str), "%Y-%m-%d %H:%M", localtime(&currTime));
  //     return {str};
  //   update_interval: 60s
  //   id: template__templatetextsensor
  template__templatetextsensor = new template_::TemplateTextSensor();
  template__templatetextsensor->set_update_interval(60000);
  App.register_component(template__templatetextsensor);
  // text_sensor.template:
  //   platform: template
  //   name: Sunrise
  //   id: sunrise_shift
  //   lambda: !lambda |-
  //     auto sunrise = id(sun_sun).sunrise();
  //     auto time = id(esptime).now();
  //     sun_sun->set_latitude(48.77034);
  //     sun_sun->set_longitude(8.34306);
  //     if (!sunrise.has_value())
  //       return {"NaN"};
  //     int sunrise_min = sunrise.value().hour * 60 + sunrise.value().minute + +6 ;
  //     int sunrise_hour = int(sunrise_min / 60);
  //     int sunrise_minute = sunrise_min % 60;
  //     ESP_LOGD("sunrise_shift", "sunrise.value.()hour is %i , sunrise.value().minute is %i ", sunrise.value().hour , sunrise.value().minute);
  //     ESP_LOGD("sunrise_shift", "sunrise_min is %i , sunrise_hour is %i ,sunrise_minute is %i ", sunrise_min , sunrise_hour , sunrise_minute);
  //     char buffer[6];
  //     sprintf(buffer,"%02i:%02i", sunrise_hour , sunrise_minute);
  //     return {buffer};
  //   update_interval: 60s
  sunrise_shift = new template_::TemplateTextSensor();
  sunrise_shift->set_update_interval(60000);
  App.register_component(sunrise_shift);
  // text_sensor.template:
  //   platform: template
  //   name: Sunset
  //   id: sunset_shift
  //   lambda: !lambda |-
  //     auto sunset = id(sun_sun).sunset();
  //     auto time = id(esptime).now();
  //     if (!sunset.has_value())
  //       return {"NaN"};
  //     int sunset_min = sunset.value().hour * 60 + sunset.value().minute + +17;
  //     int sunset_hour = int(sunset_min / 60);
  //     int sunset_minute = sunset_min % 60;
  //     ESP_LOGD("sunset_shift", "sunset.value.()hour is %i , sunset.value().minute is %i ", sunset.value().hour , sunset.value().minute);
  //     ESP_LOGD("sunset_shift", "sunset_min is %i , sunset_hour is %i ,sunset_minute is %i ", sunset_min , sunset_hour , sunset_minute);
  //     char buffer[6];
  //     sprintf(buffer,"%02i:%02i", sunset_hour , sunset_minute);
  //     return {buffer};
  //   update_interval: 60s
  sunset_shift = new template_::TemplateTextSensor();
  sunset_shift->set_update_interval(60000);
  App.register_component(sunset_shift);
  // text_sensor.template:
  //   platform: template
  //   name: Sunset Algo
  //   lambda: !lambda |-
  //     int sunrise;
  //     int sunset;
  //     auto sunSensor = new MySunTextSensor();
  //     App.register_component(sunSensor);
  //     auto sun_sensor = sunSensor->sun;
  //     auto time = id(esptime).now();
  //     sun_sensor->setPosition(48.77034, 8.34306, 1);
  //     sun_sensor->setCurrentDate(2021, 03, 05);
  //     sunrise = static_cast<int>(sun_sensor->calcSunrise());
  //     sunset = static_cast<int>(sun_sensor->calcSunset());
  //     char buffer[100];
  //   
  //     sprintf(buffer, "Sunrise at %.2d:%.2dam, Sunset at %.2d:%.2dpm", (sunrise/60), (sunrise%60), (sunset/60), (sunset%60));
  //     return {buffer};
  //   id: template__templatetextsensor_2
  //   update_interval: 60s
  template__templatetextsensor_2 = new template_::TemplateTextSensor();
  template__templatetextsensor_2->set_update_interval(60000);
  App.register_component(template__templatetextsensor_2);
  esptime->set_update_interval(900000);
  App.register_component(esptime);
  App.register_text_sensor(sun_suntextsensor);
  sun_suntextsensor->set_name("Sun Next Sunrise");
  sun_suntextsensor->set_icon("mdi:weather-sunset-up");
  App.register_text_sensor(sun_suntextsensor_2);
  sun_suntextsensor_2->set_name("Sun Next Sunset");
  sun_suntextsensor_2->set_icon("mdi:weather-sunset-down");
  lambdaaction_3 = new LambdaAction<std::string>([=](std::string x) -> void {
      ESP_LOGD("main", "The current version is %s", x.c_str()); 
  });
  App.register_text_sensor(template__templatetextsensor);
  template__templatetextsensor->set_name("Template Text Sensor");
  App.register_text_sensor(sunrise_shift);
  sunrise_shift->set_name("Sunrise");
  App.register_text_sensor(sunset_shift);
  sunset_shift->set_name("Sunset");
  App.register_text_sensor(template__templatetextsensor_2);
  template__templatetextsensor_2->set_name("Sunset Algo");
  sun_sun->set_time(esptime);
  sun_sun->set_latitude(48.77034f);
  sun_sun->set_longitude(8.34306f);
  sun_suntrigger = new sun::SunTrigger();
  App.register_component(sun_suntrigger);
  sun_suntrigger->set_parent(sun_sun);
  sun_suntrigger->set_sunrise(true);
  sun_suntrigger->set_elevation(-0.883f);
  automation = new Automation<>(sun_suntrigger);
  sun_suntextsensor->set_parent(sun_sun);
  sun_suntextsensor->set_sunrise(true);
  sun_suntextsensor->set_elevation(-0.883f);
  sun_suntextsensor->set_format("%X");
  sun_suntextsensor_2->set_parent(sun_sun);
  sun_suntextsensor_2->set_sunrise(false);
  sun_suntextsensor_2->set_elevation(-0.883f);
  sun_suntextsensor_2->set_format("%X");
  automation_3->add_actions({lambdaaction_3});
  template__templatetextsensor->set_template([=]() -> optional<std::string> {
      char str[17];
      time_t currTime = esptime->now().timestamp ;
      strftime(str, sizeof(str), "%Y-%m-%d %H:%M", localtime(&currTime));
      return {str};
  });
  template__templatetextsensor_2->set_template([=]() -> optional<std::string> {
      int sunrise;
      int sunset;
      auto sunSensor = new MySunTextSensor();
      App.register_component(sunSensor);
      auto sun_sensor = sunSensor->sun;
      auto time = esptime->now();
      sun_sensor->setPosition(48.77034, 8.34306, 1);
      sun_sensor->setCurrentDate(2021, 03, 05);
      sunrise = static_cast<int>(sun_sensor->calcSunrise());
      sunset = static_cast<int>(sun_sensor->calcSunset());
      char buffer[100];
      
      sprintf(buffer, "Sunrise at %.2d:%.2dam, Sunset at %.2d:%.2dpm", (sunrise/60), (sunrise%60), (sunset/60), (sunset%60));
      return {buffer};
  });
  lambdaaction = new LambdaAction<>([=]() -> void {
      ESP_LOGD("main", "Good morning!");
  });
  sunrise_shift->set_template([=]() -> optional<std::string> {
      auto sunrise = sun_sun->sunrise();
      auto time = esptime->now();
      sun_sun->set_latitude(48.77034);
      sun_sun->set_longitude(8.34306);
      if (!sunrise.has_value())
        return {"NaN"};
      int sunrise_min = sunrise.value().hour * 60 + sunrise.value().minute + +6 ;
      int sunrise_hour = int(sunrise_min / 60);
      int sunrise_minute = sunrise_min % 60;
      ESP_LOGD("sunrise_shift", "sunrise.value.()hour is %i , sunrise.value().minute is %i ", sunrise.value().hour , sunrise.value().minute);
      ESP_LOGD("sunrise_shift", "sunrise_min is %i , sunrise_hour is %i ,sunrise_minute is %i ", sunrise_min , sunrise_hour , sunrise_minute);
      char buffer[6];
      sprintf(buffer,"%02i:%02i", sunrise_hour , sunrise_minute);
      return {buffer};
  });
  sunset_shift->set_template([=]() -> optional<std::string> {
      auto sunset = sun_sun->sunset();
      auto time = esptime->now();
      if (!sunset.has_value())
        return {"NaN"};
      int sunset_min = sunset.value().hour * 60 + sunset.value().minute + +17;
      int sunset_hour = int(sunset_min / 60);
      int sunset_minute = sunset_min % 60;
      ESP_LOGD("sunset_shift", "sunset.value.()hour is %i , sunset.value().minute is %i ", sunset.value().hour , sunset.value().minute);
      ESP_LOGD("sunset_shift", "sunset_min is %i , sunset_hour is %i ,sunset_minute is %i ", sunset_min , sunset_hour , sunset_minute);
      char buffer[6];
      sprintf(buffer,"%02i:%02i", sunset_hour , sunset_minute);
      return {buffer};
  });
  App.register_component(version_versiontextsensor);
  automation->add_actions({lambdaaction});
  version_versiontextsensor->set_hide_timestamp(false);
  sun_suntrigger_2 = new sun::SunTrigger();
  App.register_component(sun_suntrigger_2);
  sun_suntrigger_2->set_parent(sun_sun);
  sun_suntrigger_2->set_sunrise(false);
  sun_suntrigger_2->set_elevation(-0.883f);
  automation_2 = new Automation<>(sun_suntrigger_2);
  lambdaaction_2 = new LambdaAction<>([=]() -> void {
      ESP_LOGD("main", "Good evening!");
  });
  automation_2->add_actions({lambdaaction_2});
  // =========== AUTO GENERATED CODE END ============
  // ========= YOU CAN EDIT AFTER THIS LINE =========
  App.setup();
}

void loop() {
  App.loop();
}
