From d4e43f2efae3364425567a000134db336cb2a8f6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 7 Jun 2023 13:27:01 -0500 Subject: [PATCH] light --- components/ratgdo/light/__init__.py | 35 ++++++++++++++++ .../ratgdo/light/ratgdo_light_output.cpp | 42 +++++++++++++++++++ components/ratgdo/light/ratgdo_light_output.h | 31 ++++++++++++++ components/ratgdo/ratgdo.h | 2 +- 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 components/ratgdo/light/__init__.py create mode 100644 components/ratgdo/light/ratgdo_light_output.cpp create mode 100644 components/ratgdo/light/ratgdo_light_output.h diff --git a/components/ratgdo/light/__init__.py b/components/ratgdo/light/__init__.py new file mode 100644 index 0000000..c47a410 --- /dev/null +++ b/components/ratgdo/light/__init__.py @@ -0,0 +1,35 @@ +import esphome.codegen as cg +from esphome.const import CONF_ID +import esphome.config_validation as cv +from esphome.const import ( + CONF_DURATION, + CONF_CONSTANT_BRIGHTNESS, + CONF_OUTPUT_ID, + CONF_COLD_WHITE_COLOR_TEMPERATURE, + CONF_WARM_WHITE_COLOR_TEMPERATURE, + CONF_REVERSED, + CONF_MIN_BRIGHTNESS, # New in 2023.5 +) +from esphome.components import light +from .. import ( + ratgdo_ns, + RATGDO, + register_ratgdo_child, + RATGDO_CLIENT_SCHMEA +) + +DEPENDENCIES = ["ratgdo"] + +RATGDOLightOutput = ratgdo_ns.class_( + "RATGDOLightOutput", light.LightOutput, cg.Component +) + + +CONFIG_SCHEMA = light.LIGHT_SCHEMA.extend({cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(RATGDOLightOutput)}).extend(RATGDO_CLIENT_SCHMEA) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_OUTPUT_ID]) + await cg.register_component(var, config) + await light.register_light(var, config) + await register_ratgdo_child(var, config) diff --git a/components/ratgdo/light/ratgdo_light_output.cpp b/components/ratgdo/light/ratgdo_light_output.cpp new file mode 100644 index 0000000..21fa2f8 --- /dev/null +++ b/components/ratgdo/light/ratgdo_light_output.cpp @@ -0,0 +1,42 @@ +#include "../ratgdo_state.h" +#include "esphome/core/log.h" +#include "ratgdo_cover.h" + +namespace esphome { +namespace ratgdo { + + using namespace esphome::cover; + + static const char* const TAG = "ratgdo.light"; + + void RATGDOLightOutput::dump_config() + { + LOG_CONFIG("", "RATGDO Light"); + } + void RATGDOLightOutput::on_motion_state(esphome::ratgdo::MotionState state) { } + void RATGDOLightOutput::on_obstruction_state(esphome::ratgdo::ObstructionState state) { } + void RATGDOLightOutput::on_door_state(esphome::ratgdo::DoorState state) { } + void RATGDOLightOutput::on_light_state(esphome::ratgdo::LightState state) { } + void RATGDOLightOutput::on_lock_state(esphome::ratgdo::LockState state) { } + + LightTraits RATGDOLightOutput::get_traits() + { + auto traits = LightTraits(); + traits.set_supported_color_modes({ light::ColorMode::ON_OFF }); + return traits; + } + LightState* RATGDOLightOutput::get_state() { return this->state_; } + + void RATGDOLightOutput::write_state(light::LightState* state) + { + bool binary; + state->current_values_as_binary(&binary); + if (binary) { + this->parent_->turnOnLight(); + } else { + this->parent_->turnOffLight(); + } + } + +} // namespace ratgdo +} // namespace esphome diff --git a/components/ratgdo/light/ratgdo_light_output.h b/components/ratgdo/light/ratgdo_light_output.h new file mode 100644 index 0000000..a710e8b --- /dev/null +++ b/components/ratgdo/light/ratgdo_light_output.h @@ -0,0 +1,31 @@ +#pragma once + +#include "../ratgdo.h" +#include "../ratgdo_child.h" +#include "../ratgdo_state.h" +#include "esphome/components/light/light_output.h" +#include "esphome/core/component.h" + +namespace esphome { +namespace ratgdo { + + class RATGDOLightOutput : public light::LightOutput, public RATGDOClient, public Component { + public: + void dump_config() override; + light::LightTraits get_traits() override; + light::LightState* get_state() const; + void setup_state(light::LightState* state) override { this->light_state_ = state; } + + void on_motion_state(esphome::ratgdo::MotionState state) override; + void on_obstruction_state(esphome::ratgdo::ObstructionState state) override; + void on_door_state(esphome::ratgdo::DoorState state) override; + void on_light_state(esphome::ratgdo::LightState state) override; + void on_lock_state(esphome::ratgdo::LockState state) override; + + protected: + bool _is_on; + light::LightState* state_; + }; + +} // namespace ratgdo +} // namespace esphome diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index e794a57..93839c0 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -12,11 +12,11 @@ ************************************/ #pragma once +#include "SoftwareSerial.h" // Using espsoftwareserial https://github.com/plerup/espsoftwareserial #include "esphome/core/component.h" #include "esphome/core/gpio.h" #include "esphome/core/log.h" #include "esphome/core/preferences.h" -#include "SoftwareSerial.h" // Using espsoftwareserial https://github.com/plerup/espsoftwareserial extern "C" { #include "secplus.h"