Added lamp

This commit is contained in:
radu
2026-08-09 09:44:14 +07:00
parent 76a30e0155
commit 3817827434
31 changed files with 3395 additions and 0 deletions
@@ -0,0 +1,36 @@
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, CONF_RANGE_FROM, CONF_RANGE_TO
from .. import bslamp2_ns, CODEOWNERS, CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL
__all__ = ["CODEOWNERS"]
DEPENDENCIES = ["xiaomi_bslamp2"]
XiaomiBslamp2SliderSensor = bslamp2_ns.class_("XiaomiBslamp2SliderSensor", sensor.Sensor, cg.Component)
CONFIG_SCHEMA = (
sensor.sensor_schema()
.extend(
{
cv.GenerateID(): cv.declare_id(XiaomiBslamp2SliderSensor),
cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.use_id(FrontPanelHAL),
cv.Optional(CONF_FORCE_UPDATE, default=True): cv.boolean,
cv.Optional(CONF_RANGE_FROM, default=0.01): cv.float_,
cv.Optional(CONF_RANGE_TO, default=1.00): cv.float_,
}
)
.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))
cg.add(var.set_range_from(config[CONF_RANGE_FROM]))
cg.add(var.set_range_to(config[CONF_RANGE_TO]))
@@ -0,0 +1,71 @@
#pragma once
#include "../common.h"
#include "../front_panel_hal.h"
#include "esphome/components/sensor/sensor.h"
#include <cmath>
#include <algorithm>
namespace esphome {
namespace xiaomi {
namespace bslamp2 {
/**
* A sensor for the touch slider on the front panel of the
* Xiaomi Mijia 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 illumination (this is implemented by the slider output component).
*/
class XiaomiBslamp2SliderSensor : public sensor::Sensor, public Component {
public:
void set_parent(FrontPanelHAL *front_panel) { front_panel_ = front_panel; }
void set_range_from(float from) { range_from_ = from; }
void set_range_to(float to) { range_to_ = to; }
void setup() {
slope_ = (range_to_ - range_from_) / 19.0f;
front_panel_->add_on_event_callback([this](EVENT ev) {
if ((ev & FLAG_PART_MASK) == FLAG_PART_SLIDER) {
float 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. The resulting range of levels is 0-19.
float corrected_level = std::max(0.0f, level - 2.0f);
float final_level = range_from_ + (slope_ * corrected_level);
// Accomodate for rounding errors that might push the result
// value just past the "range to" value.
if (final_level > range_to_) {
final_level = range_to_;
}
this->publish_state(final_level);
}
});
}
void dump_config() {
ESP_LOGCONFIG(TAG, "Front panel slider sensor:");
ESP_LOGCONFIG(TAG, " Range from: %f", range_from_);
ESP_LOGCONFIG(TAG, " Range to: %f", range_to_);
}
protected:
FrontPanelHAL *front_panel_;
float range_from_;
float range_to_;
float slope_;
};
} // namespace bslamp2
} // namespace xiaomi
} // namespace esphome