Compare commits
2 Commits
534d6fd05d
...
55a34ba6b0
| Author | SHA1 | Date |
|---|---|---|
|
|
55a34ba6b0 | 2 years ago |
|
|
d2cbadecf4 | 2 years ago |
@ -1,35 +0,0 @@
|
|||||||
import esphome.codegen as cg
|
|
||||||
import esphome.config_validation as cv
|
|
||||||
from esphome.components import light, output, i2c
|
|
||||||
from esphome.const import CONF_OUTPUT_ID, CONF_OUTPUT
|
|
||||||
|
|
||||||
CONF_I2C_ADDR = 0x09
|
|
||||||
|
|
||||||
UPPER_OUTPUT = "upper"
|
|
||||||
LOWER_OUTPUT = "lower"
|
|
||||||
|
|
||||||
c_monochromatic_ns = cg.esphome_ns.namespace("c_monochromatic")
|
|
||||||
MonochromaticLightOutput = c_monochromatic_ns.class_(
|
|
||||||
"MonochromaticLightOutput", light.LightOutput, cg.Component
|
|
||||||
)
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = light.BRIGHTNESS_ONLY_LIGHT_SCHEMA.extend(
|
|
||||||
{
|
|
||||||
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(MonochromaticLightOutput),
|
|
||||||
cv.Required(UPPER_OUTPUT): cv.use_id(output.FloatOutput),
|
|
||||||
cv.Required(LOWER_OUTPUT): cv.use_id(output.FloatOutput),
|
|
||||||
|
|
||||||
}
|
|
||||||
).extend(i2c.i2c_device_schema(CONF_I2C_ADDR)).extend(cv.COMPONENT_SCHEMA)
|
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
|
||||||
var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
|
|
||||||
await light.register_light(var, config)
|
|
||||||
|
|
||||||
upper = await cg.get_variable(config[UPPER_OUTPUT])
|
|
||||||
lower = await cg.get_variable(config[LOWER_OUTPUT])
|
|
||||||
cg.add(var.set_output(upper, lower))
|
|
||||||
|
|
||||||
await cg.register_component(var, config)
|
|
||||||
await i2c.register_i2c_device(var, config)
|
|
||||||
@ -1,97 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "esphome/components/i2c/i2c.h"
|
|
||||||
#include "esphome/components/light/light_output.h"
|
|
||||||
#include "esphome/components/output/float_output.h"
|
|
||||||
#include "esphome/core/component.h"
|
|
||||||
#include "esphome/core/log.h"
|
|
||||||
|
|
||||||
namespace esphome {
|
|
||||||
namespace c_monochromatic {
|
|
||||||
|
|
||||||
enum State { Off, Auto, On };
|
|
||||||
|
|
||||||
class MonochromaticLightOutput : public light::LightOutput,
|
|
||||||
public i2c::I2CDevice,
|
|
||||||
public Component {
|
|
||||||
public:
|
|
||||||
void set_output(output::FloatOutput *upper, output::FloatOutput *lower) {
|
|
||||||
upper_ = upper;
|
|
||||||
lower_ = lower;
|
|
||||||
state_ = Off;
|
|
||||||
bright_ = 0.0;
|
|
||||||
}
|
|
||||||
light::LightTraits get_traits() override {
|
|
||||||
auto traits = light::LightTraits();
|
|
||||||
traits.set_supported_color_modes({light::ColorMode::BRIGHTNESS});
|
|
||||||
return traits;
|
|
||||||
}
|
|
||||||
void write_state(light::LightState *state) override {
|
|
||||||
this->light_state_ = state;
|
|
||||||
state->current_values_as_brightness(&(this->bright_));
|
|
||||||
if (this->state_ == On) {
|
|
||||||
this->upper_->set_level(this->bright_);
|
|
||||||
this->lower_->set_level(this->bright_);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_state(State s) {
|
|
||||||
this->state_ = s;
|
|
||||||
switch (s) {
|
|
||||||
case (On):
|
|
||||||
/* this -> light_state_->turn_on(); */
|
|
||||||
this->upper_->set_level(this->bright_);
|
|
||||||
this->lower_->set_level(this->bright_);
|
|
||||||
break;
|
|
||||||
case (Off):
|
|
||||||
/* this -> light_state_->turn_off(); */
|
|
||||||
this->upper_->set_level(0.0);
|
|
||||||
this->lower_->set_level(0.0);
|
|
||||||
break;
|
|
||||||
case (Auto):
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
/* this -> write_state(this -> light_state_); */
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
uint8_t data = 10;
|
|
||||||
this->read_register(0x0, &data, 0x1);
|
|
||||||
ESP_LOGI(TAG, "READ value: %d", data);
|
|
||||||
if (data != 0) {
|
|
||||||
if (data & 1) {
|
|
||||||
this->set_state(Off);
|
|
||||||
ESP_LOGI("TEST", "Set to Off", data);
|
|
||||||
} else if ( data & 2 ){
|
|
||||||
this->set_state(Auto);
|
|
||||||
ESP_LOGI("TEST", "Set to Auto", data);
|
|
||||||
} else if (data & 4) {
|
|
||||||
this->set_state(On);
|
|
||||||
ESP_LOGI("TEST", "Set to On", data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(this->state_ == Auto) {
|
|
||||||
if(data & 8){
|
|
||||||
// TODO: turn on led
|
|
||||||
ESP_LOGI(TAG, "Auto On");
|
|
||||||
this->upper_->set_level(this->bright_);
|
|
||||||
this->lower_->set_level(this->bright_);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
ESP_LOGI(TAG, "Auto Off");
|
|
||||||
this->upper_->set_level(0.0);
|
|
||||||
this->lower_->set_level(0.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
State state_;
|
|
||||||
float bright_;
|
|
||||||
light::LightState *light_state_;
|
|
||||||
output::FloatOutput *upper_;
|
|
||||||
output::FloatOutput *lower_;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace c_monochromatic
|
|
||||||
} // namespace esphome
|
|
||||||
@ -1,22 +0,0 @@
|
|||||||
import esphome.codegen as cg
|
|
||||||
import esphome.config_validation as cv
|
|
||||||
from esphome.components import i2c, sensor
|
|
||||||
from esphome.const import CONF_ID, ICON_EMPTY, UNIT_EMPTY
|
|
||||||
|
|
||||||
DEPENDENCIES = ['i2c']
|
|
||||||
|
|
||||||
CONF_I2C_ADDR = 0x01
|
|
||||||
|
|
||||||
empty_i2c_sensor_ns = cg.esphome_ns.namespace('empty_i2c_sensor')
|
|
||||||
EmptyI2CSensor = empty_i2c_sensor_ns.class_('EmptyI2CSensor', cg.PollingComponent, i2c.I2CDevice)
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = sensor.sensor_schema().extend({
|
|
||||||
cv.GenerateID(): cv.declare_id(EmptyI2CSensor),
|
|
||||||
}).extend(cv.polling_component_schema('60s')).extend(i2c.i2c_device_schema(CONF_I2C_ADDR))
|
|
||||||
|
|
||||||
def to_code(config):
|
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
|
||||||
yield cg.register_component(var, config)
|
|
||||||
yield sensor.register_sensor(var, config)
|
|
||||||
yield i2c.register_i2c_device(var, config)
|
|
||||||
|
|
||||||
@ -1,11 +0,0 @@
|
|||||||
```yaml
|
|
||||||
# example configuration:
|
|
||||||
|
|
||||||
empty_i2c_component:
|
|
||||||
id: empty_i2c_component_1
|
|
||||||
address: 0x01 # optional
|
|
||||||
|
|
||||||
i2c:
|
|
||||||
sda: 4
|
|
||||||
scl: 5
|
|
||||||
```
|
|
||||||
@ -1,33 +0,0 @@
|
|||||||
import esphome.codegen as cg
|
|
||||||
import esphome.config_validation as cv
|
|
||||||
from esphome.components import i2c, sensor, monochromatic, output, light
|
|
||||||
from esphome.const import CONF_ID
|
|
||||||
|
|
||||||
DEPENDENCIES = ['i2c']
|
|
||||||
|
|
||||||
CONF_I2C_ADDR = 0x09
|
|
||||||
|
|
||||||
showcase_component_ns = cg.esphome_ns.namespace('showcase_component')
|
|
||||||
ShowcaseComponent = showcase_component_ns.class_('ShowcaseComponent', cg.Component, i2c.I2CDevice, light.LightOutput)
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = light.BRIGHTNESS_ONLY_LIGHT_SCHEMA.extend(
|
|
||||||
{
|
|
||||||
cv.GenerateID(): cv.declare_id(ShowcaseComponent),
|
|
||||||
|
|
||||||
cv.Required("upper"): cv.use_id(output.FloatOutput),
|
|
||||||
cv.Required("lower"): cv.use_id(output.FloatOutput)
|
|
||||||
# cv.Required("upper"): cv.use_id(monochromatic.MonochromaticLightOutput),
|
|
||||||
# cv.Required("lower"): cv.use_id(monochromatic.MonochromaticLightOutput)
|
|
||||||
}
|
|
||||||
).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(CONF_I2C_ADDR))
|
|
||||||
|
|
||||||
async def to_code(config):
|
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
|
||||||
|
|
||||||
upper = await cg.get_variable(config["upper"])
|
|
||||||
cg.add(var.set_upper(upper))
|
|
||||||
lower = await cg.get_variable(config["lower"])
|
|
||||||
cg.add(var.set_lower(lower))
|
|
||||||
|
|
||||||
await cg.register_component(var, config)
|
|
||||||
await i2c.register_i2c_device(var, config)
|
|
||||||
@ -1,57 +0,0 @@
|
|||||||
#include "esphome/core/log.h"
|
|
||||||
#include "showcase_component.h"
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
namespace esphome {
|
|
||||||
namespace showcase_component {
|
|
||||||
|
|
||||||
/* static const char *TAG = "showcase_component.component"; */
|
|
||||||
|
|
||||||
void ShowcaseComponent::setup() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShowcaseComponent::turn_on(){
|
|
||||||
this->write_state(On);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShowcaseComponent::turn_off(){
|
|
||||||
this->write_state(Off);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShowcaseComponent::loop() {
|
|
||||||
uint8_t data = 10;
|
|
||||||
this->read_register(0x0, &data, 0x1);
|
|
||||||
/* ESP_LOGI(TAG, "READ value: %d", data); */
|
|
||||||
if(data != 0){
|
|
||||||
if(data & 1){
|
|
||||||
this->write_state(Off);
|
|
||||||
/* ESP_LOGI(TAG, "Set to Off", data); */
|
|
||||||
} else if ( data & 2 ){
|
|
||||||
this->write_state(Auto);
|
|
||||||
/* ESP_LOGI(TAG, "Set to Auto", data); */
|
|
||||||
} else if ( data & 4 ){
|
|
||||||
this->write_state(On);
|
|
||||||
/* ESP_LOGI(TAG, "Set to On", data); */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(this->s_ == Auto) {
|
|
||||||
if(data & 8){
|
|
||||||
// TODO: turn on led
|
|
||||||
/* ESP_LOGI(TAG, "Auto On"); */
|
|
||||||
this->upper_->set_level(this->b_upper_);
|
|
||||||
this->lower_->set_level(this->b_lower_);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
/* ESP_LOGI(TAG, "Auto Off"); */
|
|
||||||
this->upper_->set_level(0.0);
|
|
||||||
this->lower_->set_level(0.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShowcaseComponent::dump_config(){
|
|
||||||
ESP_LOGCONFIG(TAG, "showcase component");
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace empty_i2c_component
|
|
||||||
} // namespace esphome
|
|
||||||
@ -1,106 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "esphome/core/entity_base.h"
|
|
||||||
#include "esphome/core/log.h"
|
|
||||||
#include "esphome/core/component.h"
|
|
||||||
#include "esphome/components/i2c/i2c.h"
|
|
||||||
#include "esphome/components/output/float_output.h"
|
|
||||||
#include "esphome/components/light/light_output.h"
|
|
||||||
|
|
||||||
namespace esphome {
|
|
||||||
namespace showcase_component {
|
|
||||||
|
|
||||||
/* class Output: public light::LightOutput{ */
|
|
||||||
/* public: */
|
|
||||||
/* void set_output(output::FloatOutput *output) { output_ = output; } */
|
|
||||||
/* light::LightTraits get_traits() override { */
|
|
||||||
/* auto traits = light::LightTraits(); */
|
|
||||||
/* traits.set_supported_color_modes({light::ColorMode::BRIGHTNESS}); */
|
|
||||||
/* return traits; */
|
|
||||||
/* } */
|
|
||||||
/* void write_state(light::LightState *state) override { */
|
|
||||||
/* float bright; */
|
|
||||||
/* state->current_values_as_brightness(&bright); */
|
|
||||||
/* this->output_->set_level(bright); */
|
|
||||||
/* } */
|
|
||||||
|
|
||||||
/* protected: */
|
|
||||||
/* output::FloatOutput *output_; */
|
|
||||||
/* }; */
|
|
||||||
|
|
||||||
enum State{ Off, Auto, On };
|
|
||||||
|
|
||||||
static const char *TAG = "showcase_component.component";
|
|
||||||
|
|
||||||
class ShowcaseComponent : public i2c::I2CDevice, public Component, public EntityBase, public light::LightOutput {
|
|
||||||
public:
|
|
||||||
State s_{Off};
|
|
||||||
float b_upper_{1.0};
|
|
||||||
float b_lower_{1.0};
|
|
||||||
|
|
||||||
void turn_on();
|
|
||||||
void turn_off();
|
|
||||||
|
|
||||||
void publish_state(){ this->remote_values_callback_.call(); }
|
|
||||||
|
|
||||||
void add_new_remote_values_callback(std::function<void()> &&send_callback) {
|
|
||||||
this->remote_values_callback_.add(std::move(send_callback));
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() override;
|
|
||||||
void loop() override;
|
|
||||||
void dump_config() override;
|
|
||||||
|
|
||||||
void set_upper(output::FloatOutput *upper) {this->upper_ = upper;}
|
|
||||||
void set_lower(output::FloatOutput *lower) {this->lower_ = lower;}
|
|
||||||
|
|
||||||
void write_state(State s){
|
|
||||||
ESP_LOGI(TAG, "Write_state");
|
|
||||||
this->s_ = s;
|
|
||||||
switch(s) {
|
|
||||||
case(On):
|
|
||||||
/* ESP_LOGI(TAG, "Set to On"); */
|
|
||||||
this->set_level(this->b_upper_, this->b_lower_);
|
|
||||||
break;
|
|
||||||
case(Off):
|
|
||||||
/* ESP_LOGI(TAG, "Set to Off"); */
|
|
||||||
this->upper_->set_level(0.0);
|
|
||||||
this->lower_->set_level(0.0);
|
|
||||||
break;
|
|
||||||
case(Auto):
|
|
||||||
/* ESP_LOGI(TAG, "Set to Auto"); */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_level(float bright){
|
|
||||||
this->set_level(bright, bright);
|
|
||||||
/* this->upper_->write_state(upper); */
|
|
||||||
/* this->lower_->write_state(lower); */
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_level(float upper, float lower){
|
|
||||||
this->set_level_upper(upper);
|
|
||||||
this->set_level_lower(lower);
|
|
||||||
/* this->upper_->write_state(upper); */
|
|
||||||
/* this->lower_->write_state(lower); */
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_level_upper(float bright){
|
|
||||||
this->b_upper_ = bright;
|
|
||||||
this->upper_->set_level(bright);
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_level_lower(float bright){
|
|
||||||
this->b_lower_ = bright;
|
|
||||||
this->lower_->set_level(bright);
|
|
||||||
}
|
|
||||||
protected:
|
|
||||||
CallbackManager<void()> remote_values_callback_{};
|
|
||||||
|
|
||||||
output::FloatOutput *upper_;
|
|
||||||
output::FloatOutput *lower_;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace empty_i2c_component
|
|
||||||
} // namespace esphome
|
|
||||||
Loading…
Reference in new issue