1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-29 22:24:26 +00:00

Add rgbct and color_temperature light platforms (#2138)

Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
Jesse Hills
2021-08-10 21:28:56 +12:00
committed by GitHub
parent 854f4a8896
commit d258e06fd7
14 changed files with 254 additions and 4 deletions

View File

@@ -0,0 +1,38 @@
#pragma once
#include "esphome/components/light/light_output.h"
#include "esphome/components/output/float_output.h"
#include "esphome/core/component.h"
namespace esphome {
namespace color_temperature {
class CTLightOutput : public light::LightOutput {
public:
void set_color_temperature(output::FloatOutput *color_temperature) { color_temperature_ = color_temperature; }
void set_brightness(output::FloatOutput *brightness) { brightness_ = brightness; }
void set_cold_white_temperature(float cold_white_temperature) { cold_white_temperature_ = cold_white_temperature; }
void set_warm_white_temperature(float warm_white_temperature) { warm_white_temperature_ = warm_white_temperature; }
light::LightTraits get_traits() override {
auto traits = light::LightTraits();
traits.set_supported_color_modes({light::ColorMode::COLOR_TEMPERATURE});
traits.set_min_mireds(this->cold_white_temperature_);
traits.set_max_mireds(this->warm_white_temperature_);
return traits;
}
void write_state(light::LightState *state) override {
float color_temperature, brightness;
state->current_values_as_ct(&color_temperature, &brightness);
this->color_temperature_->set_level(color_temperature);
this->brightness_->set_level(brightness);
}
protected:
output::FloatOutput *color_temperature_;
output::FloatOutput *brightness_;
float cold_white_temperature_;
float warm_white_temperature_;
};
} // namespace color_temperature
} // namespace esphome

View File

@@ -0,0 +1,42 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import light, output
from esphome.const import (
CONF_BRIGHTNESS,
CONF_COLOR_TEMPERATURE,
CONF_OUTPUT_ID,
CONF_COLD_WHITE_COLOR_TEMPERATURE,
CONF_WARM_WHITE_COLOR_TEMPERATURE,
)
CODEOWNERS = ["@jesserockz"]
color_temperature_ns = cg.esphome_ns.namespace("color_temperature")
CTLightOutput = color_temperature_ns.class_("CTLightOutput", light.LightOutput)
CONFIG_SCHEMA = cv.All(
light.RGB_LIGHT_SCHEMA.extend(
{
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(CTLightOutput),
cv.Required(CONF_COLOR_TEMPERATURE): cv.use_id(output.FloatOutput),
cv.Required(CONF_BRIGHTNESS): cv.use_id(output.FloatOutput),
cv.Required(CONF_COLD_WHITE_COLOR_TEMPERATURE): cv.color_temperature,
cv.Required(CONF_WARM_WHITE_COLOR_TEMPERATURE): cv.color_temperature,
}
),
light.validate_color_temperature_channels,
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
await light.register_light(var, config)
color_temperature = await cg.get_variable(config[CONF_COLOR_TEMPERATURE])
cg.add(var.set_color_temperature(color_temperature))
brightness = await cg.get_variable(config[CONF_BRIGHTNESS])
cg.add(var.set_brightness(brightness))
cg.add(var.set_cold_white_temperature(config[CONF_COLD_WHITE_COLOR_TEMPERATURE]))
cg.add(var.set_warm_white_temperature(config[CONF_WARM_WHITE_COLOR_TEMPERATURE]))