Browse Source

Some changes to make the text_sensor work for logic expressions.

For example: `id(${id_light_mode}).state == "night"`
This is used in the example.yaml to turn off the illumination
of the front panel, when the lamp goes into night light mode.
pull/9/head
Maurice Makaay 3 years ago
parent
commit
0ac7589484
5 changed files with 31 additions and 53 deletions
  1. +5
    -1
      doc/example.yaml
  2. +0
    -1
      light/__init__.py
  3. +10
    -28
      light/automation.h
  4. +11
    -13
      light/light_output.h
  5. +5
    -10
      text_sensor/light_mode_text_sensor.h

+ 5
- 1
doc/example.yaml View File

@ -67,7 +67,11 @@ light:
then:
- output.set_level:
id: ${id_front_panel_output}
level: !lambda if (x < 0.012f) return 0; else return x;
level: !lambda |-
if (id(${id_light_mode}).state == "night")
return 0;
else
return x;
# You can use any effects that you like. These are just examples.
effects:
- random:


+ 0
- 1
light/__init__.py View File

@ -17,7 +17,6 @@ CONF_ON_BRIGHTNESS = "on_brightness"
YeelightBS2LightState = bs2_ns.class_("YeelightBS2LightState", light.LightState)
YeelightBS2LightOutput = bs2_ns.class_("YeelightBS2LightOutput", light.LightOutput)
BrightnessTrigger = bs2_ns.class_("BrightnessTrigger", automation.Trigger.template())
LightModeTrigger = bs2_ns.class_("LightModeTrigger", automation.Trigger.template())
CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend(
{


+ 10
- 28
light/automation.h View File

@ -12,38 +12,20 @@ namespace bs2 {
class BrightnessTrigger : public Trigger<float> {
public:
explicit BrightnessTrigger(YeelightBS2LightOutput *parent) {
parent->add_on_state_callback(
[this](light::LightColorValues values, std::string light_mode) {
auto new_brightness = values.get_brightness();
if (values.get_state() == 0) {
new_brightness = 0.0f;
}
new_brightness = roundf(new_brightness * 100.0f) / 100.0f;
if (last_brightness_ != new_brightness) {
trigger(new_brightness);
last_brightness_ = new_brightness;
}
parent->add_on_state_callback([this](light::LightColorValues values) {
auto new_brightness = values.get_brightness();
if (values.get_state() == 0) {
new_brightness = 0.0f;
}
);
}
protected:
float last_brightness_ = -1.0f;
};
class LightModeTrigger : public Trigger<std::string> {
public:
explicit LightModeTrigger(YeelightBS2LightOutput *parent) {
parent->add_on_state_callback(
[this](light::LightColorValues values, std::string light_mode) {
if (last_light_mode_ != light_mode) {
trigger(light_mode);
last_light_mode_ = light_mode;
}
new_brightness = roundf(new_brightness * 100.0f) / 100.0f;
if (last_brightness_ != new_brightness) {
trigger(new_brightness);
last_brightness_ = new_brightness;
}
);
});
}
protected:
std::string last_light_mode_ = LIGHT_MODE_UNKNOWN;
float last_brightness_ = -1.0f;
};
} // namespace yeelight_bs2


+ 11
- 13
light/light_output.h View File

@ -39,7 +39,11 @@ public:
return traits;
}
void add_on_state_callback(std::function<void(light::LightColorValues, std::string)> &&callback) {
void add_on_light_mode_callback(std::function<void(std::string)> &&callback) {
light_mode_callback_.add(std::move(callback));
}
void add_on_state_callback(std::function<void(light::LightColorValues)> &&callback) {
state_callback_.add(std::move(callback));
}
@ -57,17 +61,15 @@ public:
GPIOOutputs *delegate;
if (transition_handler_->set_light_color_values(values)) {
delegate = transition_handler_;
state_callback_.call(
transition_handler_->get_end_values(),
delegate->light_mode);
light_mode_callback_.call(delegate->light_mode);
state_callback_.call(transition_handler_->get_end_values());
} else {
instant_handler_->set_light_color_values(values);
delegate = instant_handler_;
state_callback_.call(values, delegate->light_mode);
light_mode_callback_.call(delegate->light_mode);
state_callback_.call(values);
}
light_mode_ = delegate->light_mode;
// Note: one might think that it is more logical to turn on the LED
// circuitry master switch after setting the individual channels,
// but this is the order that was used by the original firmware. I
@ -88,16 +90,12 @@ public:
light_->turn_off();
}
std::string get_light_mode() {
return light_mode_;
}
protected:
LightHAL *light_;
ColorTransitionHandler *transition_handler_;
ColorInstantHandler *instant_handler_ = new ColorInstantHandler();
CallbackManager<void(light::LightColorValues, std::string)> state_callback_{};
std::string light_mode_;
CallbackManager<void(std::string)> light_mode_callback_{};
CallbackManager<void(light::LightColorValues)> state_callback_{};
friend class YeelightBS2LightState;


+ 5
- 10
text_sensor/light_mode_text_sensor.h View File

@ -1,8 +1,5 @@
#pragma once
#include <cmath>
#include "../common.h"
#include "../front_panel_hal.h"
#include "esphome/components/text_sensor/text_sensor.h"
namespace esphome {
@ -20,14 +17,12 @@ public:
void set_parent(YeelightBS2LightOutput *light) { light_ = light; }
void setup() {
light_->add_on_state_callback(
[this](light::LightColorValues values, std::string light_mode) {
if (last_light_mode_ != light_mode) {
publish_state(light_mode);
last_light_mode_ = light_mode;
}
light_->add_on_light_mode_callback([this](std::string light_mode) {
if (last_light_mode_ != light_mode) {
publish_state(light_mode);
last_light_mode_ = light_mode;
}
);
});
}
protected:


Loading…
Cancel
Save