Browse Source

Implemented the slider sensor, which is used to publish a slider level when the slider is touched. The example yaml files now use this sensor to update the brightness of the LEDs.

pull/4/head
Maurice Makaay 3 years ago
parent
commit
5e328bef8f
8 changed files with 125 additions and 12 deletions
  1. +3
    -4
      binary_sensor/__init__.py
  2. +6
    -3
      binary_sensor/binary_sensor.h
  3. +12
    -2
      doc/example-full.yaml
  4. +13
    -1
      doc/example.yaml
  5. +1
    -1
      light/__init__.py
  6. +1
    -1
      light/light_output.h
  7. +29
    -0
      sensor/__init__.py
  8. +60
    -0
      sensor/sensor.h

+ 3
- 4
binary_sensor/__init__.py View File

@ -1,8 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import binary_sensor from esphome.components import binary_sensor
#from esphome import automation
from esphome.const import CONF_DEVICE_CLASS, CONF_ID
from esphome.const import CONF_ID
from .. import ( from .. import (
bs2_ns, CODEOWNERS, bs2_ns, CODEOWNERS,
CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL
@ -21,7 +20,7 @@ PARTS = {
def validate_part(value): def validate_part(value):
value = cv.string(value) value = cv.string(value)
return cv.enum(PARTS, upper=True)(value)
return cv.enum(PARTS, upper=True, space='_')(value)
YeelightBS2Button = bs2_ns.class_("YeelightBS2Button", binary_sensor.BinarySensor, cg.Component) YeelightBS2Button = bs2_ns.class_("YeelightBS2Button", binary_sensor.BinarySensor, cg.Component)
@ -39,5 +38,5 @@ def to_code(config):
yield binary_sensor.register_binary_sensor(var, config) yield binary_sensor.register_binary_sensor(var, config)
front_panel_hal_var = yield cg.get_variable(config[CONF_FRONT_PANEL_HAL_ID]) front_panel_hal_var = yield cg.get_variable(config[CONF_FRONT_PANEL_HAL_ID])
cg.add(var.set_front_panel_hal(front_panel_hal_var))
cg.add(var.set_parent(front_panel_hal_var))
cg.add(var.set_part(config[CONF_PART])) cg.add(var.set_part(config[CONF_PART]))

binary_sensor/button.h → binary_sensor/binary_sensor.h View File

@ -14,7 +14,7 @@ namespace bs2 {
*/ */
class YeelightBS2Button : public binary_sensor::BinarySensor, public Component { class YeelightBS2Button : public binary_sensor::BinarySensor, public Component {
public: public:
void set_front_panel_hal(FrontPanelHAL *front_panel) {
void set_parent(FrontPanelHAL *front_panel) {
front_panel_ = front_panel; front_panel_ = front_panel;
} }
@ -24,12 +24,15 @@ public:
void setup() { void setup() {
ESP_LOGCONFIG(TAG, "Setting up binary_sensor ..."); ESP_LOGCONFIG(TAG, "Setting up binary_sensor ...");
ESP_LOGCONFIG(TAG, " Part id: %d", part_);
ESP_LOGCONFIG(TAG, " Part: %s (id %d)",
(part_ == 1 ? "power button" :
part_ == 2 ? "color button" :
part_ == 3 ? "slider" : "any"),
part_);
front_panel_->add_on_event_callback( front_panel_->add_on_event_callback(
[this](EVENT ev) { [this](EVENT ev) {
// Filter events by part, when requested. // Filter events by part, when requested.
ESP_LOGI(TAG, "FILTER event=%d, part=%d", ev, part_);
if (part_ > 0) { if (part_ > 0) {
if ((ev & FLAG_PART_MASK) != (part_ << FLAG_PART_SHIFT)) { if ((ev & FLAG_PART_MASK) != (part_ << FLAG_PART_SHIFT)) {
return; return;

+ 12
- 2
doc/example-full.yaml View File

@ -73,12 +73,22 @@ light:
transition_length: 3s transition_length: 3s
update_interval: 3s update_interval: 3s
# An implementation for the power button: touch to toggle the light.
# The binary sensor is used to handle front panel touch events.
binary_sensor: binary_sensor:
- platform: yeelight_bs2 - platform: yeelight_bs2
id: ${name}_power_button id: ${name}_power_button
part: POWER_BUTTON
part: power button
on_press: on_press:
then: then:
- light.toggle: ${name} - light.toggle: ${name}
# The sensor is used to publish a slider level when the slider is touched.
sensor:
- platform: yeelight_bs2
id: ${name}_slider_level
on_value:
then:
- light.turn_on:
id: ${name}
brightness: !lambda return x;

+ 13
- 1
doc/example.yaml View File

@ -48,11 +48,23 @@ light:
transition_length: 3s transition_length: 3s
update_interval: 3s update_interval: 3s
# The binary sensor is used to handle front panel touch events.
binary_sensor: binary_sensor:
- platform: yeelight_bs2 - platform: yeelight_bs2
id: ${name}_power_button id: ${name}_power_button
part: POWER_BUTTON
part: power button
on_press: on_press:
then: then:
- light.toggle: ${name} - light.toggle: ${name}
# The sensor is used to publish a slider level when the slider is touched.
sensor:
- platform: yeelight_bs2
id: ${name}_slider_level
on_value:
then:
- light.turn_on:
id: ${name}
brightness: !lambda return x;

+ 1
- 1
light/__init__.py View File

@ -36,7 +36,7 @@ def to_code(config):
yield light.register_light(var, config) yield light.register_light(var, config)
light_hal_var = yield cg.get_variable(config[CONF_LIGHT_HAL_ID]) light_hal_var = yield cg.get_variable(config[CONF_LIGHT_HAL_ID])
cg.add(var.set_light_hal(light_hal_var))
cg.add(var.set_parent(light_hal_var))
for conf in config.get(CONF_ON_BRIGHTNESS, []): for conf in config.get(CONF_ON_BRIGHTNESS, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)


+ 1
- 1
light/light_output.h View File

@ -20,7 +20,7 @@ namespace bs2 {
*/ */
class YeelightBS2LightOutput : public Component, public light::LightOutput { class YeelightBS2LightOutput : public Component, public light::LightOutput {
public: public:
void set_light_hal(LightHAL *light) { light_ = light; }
void set_parent(LightHAL *light) { light_ = light; }
/** /**
* Returns a LightTraits object, which is used to explain to the outside * Returns a LightTraits object, which is used to explain to the outside


+ 29
- 0
sensor/__init__.py View File

@ -0,0 +1,29 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import CONF_ID, CONF_FORCE_UPDATE
from .. import (
bs2_ns, CODEOWNERS,
CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL
)
AUTO_LOAD = ["yeelight_bs2"]
YeelightBS2SliderSensor = bs2_ns.class_(
"YeelightBS2SliderSensor", sensor.Sensor, cg.Component)
CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(YeelightBS2SliderSensor),
cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.use_id(FrontPanelHAL),
cv.Optional(CONF_FORCE_UPDATE, default=True): cv.boolean,
}
).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield sensor.register_sensor(var, config)
front_panel_hal_var = yield cg.get_variable(config[CONF_FRONT_PANEL_HAL_ID])
cg.add(var.set_parent(front_panel_hal_var))

+ 60
- 0
sensor/sensor.h View File

@ -0,0 +1,60 @@
#pragma once
#include <cmath>
#include "../common.h"
#include "../front_panel_hal.h"
#include "esphome/components/sensor/sensor.h"
namespace esphome {
namespace yeelight {
namespace bs2 {
/**
* A sensor for the slider on the Yeelight Bedside Lamp 2.
*
* This sensor publishes the level at which the slider was touched, so it
* can be used to implement automations. Note that it does not represent
* the brightness of the LED lights (this is implemented by the light output
* component), nor the level as displayed by the slider using the front
* panel light (this is implemented by the slider light component).
*/
class YeelightBS2SliderSensor : public sensor::Sensor, public Component {
public:
void set_parent(FrontPanelHAL *front_panel) {
front_panel_ = front_panel;
}
void setup() {
ESP_LOGCONFIG(TAG, "Setting up slider sensor ...");
front_panel_->add_on_event_callback(
[this](EVENT ev) {
if ((ev & FLAG_PART_MASK) == FLAG_PART_SLIDER) {
auto level = (ev & FLAG_LEVEL_MASK) >> FLAG_LEVEL_SHIFT;
// Slider level 1 is really hard to touch. It is between
// the power button and the slider space, so it doesn't
// look like this one was ever meant to be used, or that
// the design was faulty on this. Therefore, level 1 is
// ignored here.
level = max(1.0f, level - 1.0f);
// Convert the slider level to a float between 0.01 and
// 1.00, which is useful as a representation for a
// brightness value. The input level is now between
// 1 and 20.
auto publish_level = max(0.01f, (level-1.0f) * (1.00f / 19.0f));
this->publish_state(publish_level);
}
}
);
}
protected:
FrontPanelHAL *front_panel_;
};
} // namespace bs2
} // namespace yeelight
} // namespace esphome

Loading…
Cancel
Save