esphome-ratgdo/components/ratgdo/light/ratgdo_light_output.cpp

69 lines
1.8 KiB
C++
Raw Normal View History

2023-06-07 18:49:13 +00:00
#include "ratgdo_light_output.h"
2023-06-07 18:27:01 +00:00
#include "../ratgdo_state.h"
#include "esphome/core/log.h"
namespace esphome {
namespace ratgdo {
2023-06-07 18:28:01 +00:00
using namespace esphome::light;
2023-06-07 18:27:01 +00:00
static const char* const TAG = "ratgdo.light";
void RATGDOLightOutput::dump_config()
{
ESP_LOGCONFIG(TAG, "RATGDO Light");
2023-06-07 18:27:01 +00:00
}
void RATGDOLightOutput::setup()
{
this->parent_->subscribe_light_state([=](LightState state) {
this->on_light_state(state);
});
}
2023-06-08 00:31:08 +00:00
void RATGDOLightOutput::on_light_state(esphome::ratgdo::LightState state)
2023-06-07 22:14:35 +00:00
{
if (this->light_state_) {
2023-06-09 21:51:34 +00:00
this->has_initial_state_ = true;
2023-06-09 21:45:49 +00:00
set_state(state);
2023-06-07 22:14:35 +00:00
}
}
2023-07-03 02:45:00 +00:00
2023-06-09 21:45:49 +00:00
void RATGDOLightOutput::set_state(esphome::ratgdo::LightState state)
2023-06-09 21:46:28 +00:00
{
bool is_on = state == LightState::ON;
2023-06-09 21:46:28 +00:00
this->light_state_->current_values.set_state(is_on);
this->light_state_->remote_values.set_state(is_on);
this->light_state_->publish_state();
}
2023-06-09 21:46:28 +00:00
void RATGDOLightOutput::setup_state(light::LightState* light_state)
{
esphome::ratgdo::LightState state = this->parent_->get_light_state();
2023-06-09 21:46:28 +00:00
this->light_state_ = light_state;
this->set_state(state);
}
2023-06-09 21:46:28 +00:00
LightTraits RATGDOLightOutput::get_traits()
{
auto traits = LightTraits();
traits.set_supported_color_modes({ light::ColorMode::ON_OFF });
return traits;
}
2023-06-09 21:45:53 +00:00
2023-06-09 21:46:28 +00:00
void RATGDOLightOutput::write_state(light::LightState* state)
{
2023-06-09 21:51:34 +00:00
if (!this->has_initial_state_)
return;
2023-06-09 21:46:28 +00:00
bool binary;
state->current_values_as_binary(&binary);
if (binary) {
this->parent_->light_on();
2023-06-09 21:46:28 +00:00
} else {
this->parent_->light_off();
2023-06-09 21:46:28 +00:00
}
2023-06-07 18:27:01 +00:00
}
} // namespace ratgdo
} // namespace esphome