Added lamp
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import text_sensor
|
||||
from esphome.const import CONF_ID, CONF_OUTPUT_ID
|
||||
from .. import bslamp2_ns, CODEOWNERS
|
||||
from ..light import XiaomiBslamp2LightOutput
|
||||
|
||||
__all__ = ["CODEOWNERS"]
|
||||
|
||||
DEPENDENCIES = ["xiaomi_bslamp2"]
|
||||
|
||||
CONF_LIGHT_ID = "light_id"
|
||||
|
||||
XiaomiBslamp2LightModeTextSensor = bslamp2_ns.class_(
|
||||
"XiaomiBslamp2LightModeTextSensor", text_sensor.TextSensor, cg.Component
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
text_sensor.text_sensor_schema()
|
||||
.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(XiaomiBslamp2LightModeTextSensor),
|
||||
cv.GenerateID(CONF_OUTPUT_ID): cv.use_id(XiaomiBslamp2LightOutput),
|
||||
}
|
||||
)
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
yield text_sensor.register_text_sensor(var, config)
|
||||
|
||||
parent_var = yield cg.get_variable(config[CONF_OUTPUT_ID])
|
||||
cg.add(var.set_parent(parent_var))
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/text_sensor/text_sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace xiaomi {
|
||||
namespace bslamp2 {
|
||||
|
||||
/**
|
||||
* A text sensor, used for propagating the active light mode on the
|
||||
* Xiaomi Mijia Bedside Lamp 2.
|
||||
*
|
||||
* The possible light modes are "off", "rgb", "white" and "night".
|
||||
*/
|
||||
class XiaomiBslamp2LightModeTextSensor : public text_sensor::TextSensor, public Component {
|
||||
public:
|
||||
void set_parent(XiaomiBslamp2LightOutput *light) { light_ = light; }
|
||||
|
||||
void setup() {
|
||||
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:
|
||||
XiaomiBslamp2LightOutput *light_;
|
||||
std::string last_light_mode_ = LIGHT_MODE_UNKNOWN;
|
||||
};
|
||||
|
||||
} // namespace bslamp2
|
||||
} // namespace xiaomi
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user