espHome-NBS-files/external_components/hoermann_door/light_component.h

106 lines
2.6 KiB
C++

#pragma once
#ifdef USE_LIGHT
#include "esphome.h"
#include "Arduino.h"
#include "hciemulator.h"
#include "door_singleton.h"
//#include "cover.h"
#define RS485 Serial2
#define TX_ON 25
//#define configMAX_PRIORITIES 25
#define TAG "hoermann_door"
namespace esphome {
namespace hoermann_door {
// Custom binary output, for exposing binary states
class NbsLightManager: public binary::BinaryLightOutput, public light::LightState {
protected:
HoermannSingleton *single;
public:
void setup() override {
//single = HoermannSingleton::getInstance();
//single->initializeGetInstance
}
bool lastState = false;
bool firstState = false;
void loop() override {
//if(!DoorManager.getEmulator().getState().valid) false;
//if(!firstState || DoorManager.getEmulator().getState().lampOn != lastState){
//ESP_LOGD("Test", "I have no idea");
// lastState = DoorManager.getEmulator().getState().lampOn;
// this->write_state(lastState);
// firstState = true;
//}
}
};
// Custom binary output, for exposing binary states
class NbsLightOutput: public output::BinaryOutput, public Component{
light::LightState *callback;
HoermannSingleton *single;
bool lastState = false;
bool firstState = true;
public:
virtual void write_state(bool state){
if(lastState != state){
single->getEmulator()->toggleLamp();
//lastState = state;
}
}
void set_state_callback(light::LightState *callback){
if(callback == nullptr) ESP_LOGW("Hoermann_door(Light)", "Got Nullable callback");
this->callback = callback;
}
void loop() override {
if(!single->getEmulator()->getState().valid) false;
if(firstState || single->getEmulator()->getState().lampOn != lastState){
//ESP_LOGD("Test", "I have no idea");
lastState = single->getEmulator()->getState().lampOn;
if(lastState == true){
ESP_LOGD("Hoermann_door(Light)", "Light State ON");
auto call = callback->make_call();
call.set_state(true);
call.perform();
}
else {
ESP_LOGD("Hoermann_door(Light)", "Light State OFF");
auto call = callback->make_call();
call.set_state(false);
call.perform();
}
firstState = false;
}
}
void setup() override {
single = HoermannSingleton::getInstance();
single->initializeEmulator();
}
void turn_on() override {
this->write_state(true);
}
void turn_off() override {
this->write_state(false);
}
};
} // namespace hoermann
} // namespace esphome
#endif