Added lamp
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import output
|
||||
from esphome.const import CONF_ID, CONF_LEVEL
|
||||
from esphome import automation
|
||||
from .. import bslamp2_ns, CODEOWNERS, CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL, FRONT_PANEL_LED_OPTIONS
|
||||
|
||||
__all__ = ["CODEOWNERS"]
|
||||
|
||||
CONF_LEDS = "leds"
|
||||
|
||||
DEPENDENCIES = ["xiaomi_bslamp2"]
|
||||
|
||||
XiaomiBslamp2FrontPanelOutput = bslamp2_ns.class_("XiaomiBslamp2FrontPanelOutput", output.FloatOutput, cg.Component)
|
||||
SetLEDsAction = bslamp2_ns.class_("SetLEDsAction", automation.Action)
|
||||
SetLevelAction = bslamp2_ns.class_("SetLevelAction", automation.Action)
|
||||
UpdateLEDsAction = bslamp2_ns.class_("UpdateLEDsAction", automation.Action)
|
||||
|
||||
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(XiaomiBslamp2FrontPanelOutput),
|
||||
cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.use_id(FrontPanelHAL),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
yield output.register_output(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))
|
||||
|
||||
|
||||
def maybe_simple_level_value(schema):
|
||||
def validator(value):
|
||||
if isinstance(value, dict):
|
||||
return schema(value)
|
||||
return schema({"level": value})
|
||||
|
||||
return validator
|
||||
|
||||
|
||||
def maybe_simple_leds_value(schema):
|
||||
def validator(value):
|
||||
if isinstance(value, dict):
|
||||
return schema(value)
|
||||
return schema({"leds": value})
|
||||
|
||||
return validator
|
||||
|
||||
|
||||
FRONT_PANEL_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(CONF_ID): cv.use_id(XiaomiBslamp2FrontPanelOutput),
|
||||
}
|
||||
)
|
||||
|
||||
FRONT_PANEL_LEVEL_SCHEMA = cv.Schema(
|
||||
maybe_simple_level_value(
|
||||
FRONT_PANEL_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_LEVEL): cv.templatable(cv.percentage),
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
FRONT_PANEL_LED_SCHEMA = cv.Schema(
|
||||
maybe_simple_leds_value(
|
||||
FRONT_PANEL_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_LEDS): cv.ensure_list(cv.enum(FRONT_PANEL_LED_OPTIONS, upper=True)),
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"front_panel.set_level",
|
||||
SetLevelAction,
|
||||
FRONT_PANEL_LEVEL_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
async def set_level_to_code(config, action_id, template_arg, args):
|
||||
output_var = await cg.get_variable(config[CONF_ID])
|
||||
action_var = cg.new_Pvariable(action_id, template_arg, output_var)
|
||||
template_ = await cg.templatable(config[CONF_LEVEL], args, float)
|
||||
cg.add(action_var.set_level(template_))
|
||||
return action_var
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"front_panel.set_leds",
|
||||
SetLEDsAction,
|
||||
FRONT_PANEL_LED_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
async def set_leds_to_code(config, action_id, template_arg, args):
|
||||
output_var = await cg.get_variable(config[CONF_ID])
|
||||
action_var = cg.new_Pvariable(action_id, template_arg, output_var)
|
||||
bits = [FRONT_PANEL_LED_OPTIONS["NONE"]] + [FRONT_PANEL_LED_OPTIONS[led] for led in config[CONF_LEDS]]
|
||||
value = cg.RawExpression("|".join(map(str, bits)))
|
||||
mode_template = await cg.templatable(2, args, cg.int_)
|
||||
cg.add(action_var.set_mode(mode_template))
|
||||
leds_template = await cg.templatable(value, args, cg.uint16)
|
||||
cg.add(action_var.set_leds(leds_template))
|
||||
return action_var
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"front_panel.turn_on_leds",
|
||||
SetLEDsAction,
|
||||
FRONT_PANEL_LED_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
async def turn_on_leds_to_code(config, action_id, template_arg, args):
|
||||
output_var = await cg.get_variable(config[CONF_ID])
|
||||
action_var = cg.new_Pvariable(action_id, template_arg, output_var)
|
||||
bits = [FRONT_PANEL_LED_OPTIONS["NONE"]] + [FRONT_PANEL_LED_OPTIONS[led] for led in config[CONF_LEDS]]
|
||||
value = cg.RawExpression("|".join(map(str, bits)))
|
||||
mode_template = await cg.templatable(1, args, cg.int_)
|
||||
cg.add(action_var.set_mode(mode_template))
|
||||
leds_template = await cg.templatable(value, args, cg.uint16)
|
||||
cg.add(action_var.set_leds(leds_template))
|
||||
return action_var
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"front_panel.turn_off_leds",
|
||||
SetLEDsAction,
|
||||
FRONT_PANEL_LED_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
async def turn_off_leds_to_code(config, action_id, template_arg, args):
|
||||
output_var = await cg.get_variable(config[CONF_ID])
|
||||
action_var = cg.new_Pvariable(action_id, template_arg, output_var)
|
||||
bits = [FRONT_PANEL_LED_OPTIONS["NONE"]] + [FRONT_PANEL_LED_OPTIONS[led] for led in config[CONF_LEDS]]
|
||||
value = cg.RawExpression("|".join(map(str, bits)))
|
||||
mode_template = await cg.templatable(0, args, cg.int_)
|
||||
cg.add(action_var.set_mode(mode_template))
|
||||
leds_template = await cg.templatable(value, args, cg.uint16)
|
||||
cg.add(action_var.set_leds(leds_template))
|
||||
return action_var
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"front_panel.update_leds",
|
||||
UpdateLEDsAction,
|
||||
FRONT_PANEL_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
async def update_leds_to_code(config, action_id, template_arg, args):
|
||||
output_var = await cg.get_variable(config[CONF_ID])
|
||||
action_var = cg.new_Pvariable(action_id, template_arg, output_var)
|
||||
return action_var
|
||||
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "../front_panel_hal.h"
|
||||
#include "output.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace esphome {
|
||||
namespace xiaomi {
|
||||
namespace bslamp2 {
|
||||
|
||||
template<typename... Ts> class SetLEDsAction : public Action<Ts...> {
|
||||
public:
|
||||
explicit SetLEDsAction(XiaomiBslamp2FrontPanelOutput *parent) : parent_(parent) {}
|
||||
|
||||
TEMPLATABLE_VALUE(int, mode)
|
||||
TEMPLATABLE_VALUE(uint16_t, leds)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
uint16_t mode = this->mode_.value(x...);
|
||||
uint16_t value = this->leds_.value(x...);
|
||||
switch (mode) {
|
||||
case 0:
|
||||
parent_->turn_off_leds(value);
|
||||
break;
|
||||
case 1:
|
||||
parent_->turn_on_leds(value);
|
||||
break;
|
||||
case 2:
|
||||
parent_->set_leds(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
XiaomiBslamp2FrontPanelOutput *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetLevelAction : public Action<Ts...> {
|
||||
public:
|
||||
explicit SetLevelAction(XiaomiBslamp2FrontPanelOutput *parent) : parent_(parent) {}
|
||||
|
||||
TEMPLATABLE_VALUE(float, level)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
parent_->set_level(this->level_.value(x...));
|
||||
parent_->update_leds();
|
||||
}
|
||||
|
||||
protected:
|
||||
XiaomiBslamp2FrontPanelOutput *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class UpdateLEDsAction : public Action<Ts...> {
|
||||
public:
|
||||
explicit UpdateLEDsAction(XiaomiBslamp2FrontPanelOutput *parent) : parent_(parent) {}
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
parent_->update_leds();
|
||||
}
|
||||
|
||||
protected:
|
||||
XiaomiBslamp2FrontPanelOutput *parent_;
|
||||
};
|
||||
|
||||
} // namespace bslamp2
|
||||
} // namespace xiaomi
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include "../common.h"
|
||||
#include "../front_panel_hal.h"
|
||||
#include "esphome/components/output/float_output.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace esphome {
|
||||
namespace xiaomi {
|
||||
namespace bslamp2 {
|
||||
|
||||
/**
|
||||
* An output, used for controlling the front panel illumination and
|
||||
* level indicator on the Xiaomi Mijia Bedside Lamp 2 front panel.
|
||||
*/
|
||||
class XiaomiBslamp2FrontPanelOutput : public output::FloatOutput, public Component {
|
||||
public:
|
||||
void set_parent(FrontPanelHAL *front_panel) {
|
||||
front_panel_ = front_panel;
|
||||
}
|
||||
|
||||
void write_state(float level) {
|
||||
if (level > 0) {
|
||||
turn_on_leds(LED_POWER | LED_COLOR);
|
||||
set_level(level);
|
||||
} else {
|
||||
turn_off_leds(LED_ALL);
|
||||
}
|
||||
}
|
||||
|
||||
void set_level(float level) {
|
||||
front_panel_->set_slider_level(level);
|
||||
}
|
||||
|
||||
void set_leds(uint16_t leds) {
|
||||
front_panel_->set_leds(leds);
|
||||
}
|
||||
|
||||
void turn_on_leds(uint16_t leds) {
|
||||
front_panel_->turn_on_leds(leds);
|
||||
}
|
||||
|
||||
void turn_off_leds(uint16_t leds) {
|
||||
front_panel_->turn_off_leds(leds);
|
||||
}
|
||||
|
||||
void update_leds() {
|
||||
front_panel_->update_leds();
|
||||
}
|
||||
|
||||
protected:
|
||||
FrontPanelHAL *front_panel_;
|
||||
};
|
||||
|
||||
} // namespace bslamp2
|
||||
} // namespace xiaomi
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user