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,69 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import CONF_ID, CONF_FOR
from .. import bslamp2_ns, CODEOWNERS, CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL
__all__ = ["CODEOWNERS"]
DEPENDENCIES = ["xiaomi_bslamp2"]
CONF_PART = "part"
# The identifier values match the bit values of the events as defined
# in ../front_panel_hal.h.
PARTS = {
"POWER_BUTTON": 0b001 << 1,
"POWER": 0b001 << 1,
"COLOR_BUTTON": 0b010 << 1,
"COLOR": 0b010 << 1,
"SLIDER": 0b100 << 1,
}
XiaomiBslamp2TouchBinarySensor = bslamp2_ns.class_(
"XiaomiBslamp2TouchBinarySensor", binary_sensor.BinarySensor, cg.Component
)
def validate_for(value):
value = cv.string(value)
return cv.enum(PARTS, upper=True, space="_")(value)
def validate_binary_sensor(conf):
if CONF_PART in conf and CONF_FOR in conf:
raise cv.Invalid("Specify only one of [part] or [for]")
if CONF_PART in conf and CONF_FOR not in conf:
# Backward compatibility.
conf[CONF_FOR] = conf[CONF_PART]
if CONF_FOR not in conf:
raise cv.Invalid("'for' is a required option for [binary_sensor.xiaomi_bslamp2]")
return conf
CONFIG_SCHEMA = cv.All(
binary_sensor.binary_sensor_schema()
.extend(
{
cv.GenerateID(): cv.declare_id(XiaomiBslamp2TouchBinarySensor),
cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.use_id(FrontPanelHAL),
# This option is not advertised in the documentation. It must be
# considered deprecated. I'm not announcing it as such yet. Not
# sure if it's useful to do so.
cv.Optional(CONF_PART): validate_for,
cv.Optional(CONF_FOR): validate_for,
}
)
.extend(cv.COMPONENT_SCHEMA),
validate_binary_sensor,
)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield binary_sensor.register_binary_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_for(config[CONF_FOR]))
@@ -0,0 +1,56 @@
#pragma once
#include "../common.h"
#include "../front_panel_hal.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
namespace esphome {
namespace xiaomi {
namespace bslamp2 {
/**
* This class implements a binary sensor for the touch buttons
* and touch slider on the front panel of the Xiaomi Mijia Bedside Lamp 2.
*/
class XiaomiBslamp2TouchBinarySensor : public binary_sensor::BinarySensor, public Component {
public:
void set_parent(FrontPanelHAL *front_panel) { front_panel_ = front_panel; }
void set_for(int part) { for_ = part; }
void setup() {
front_panel_->add_on_event_callback([this](EVENT ev) {
auto part_in_event = ev & FLAG_PART_MASK;
if (for_ == 0 || part_in_event == for_) {
auto new_state = (ev & FLAG_TYPE_MASK) == FLAG_TYPE_TOUCH;
this->publish_state(new_state);
}
});
}
void dump_config() {
ESP_LOGCONFIG(TAG, "Front panel binary_sensor:");
ESP_LOGCONFIG(TAG, " For: %s", format_part());
}
protected:
FrontPanelHAL *front_panel_;
EVENT for_ = 0;
const char *format_part() {
switch (for_) {
case FLAG_PART_POWER:
return "power button";
case FLAG_PART_COLOR:
return "color button";
case FLAG_PART_SLIDER:
return "slider";
default:
return "ERR";
}
}
};
} // namespace bslamp2
} // namespace xiaomi
} // namespace esphome