mirror of
https://github.com/esphome/esphome.git
synced 2025-09-16 18:22:22 +01:00
refactor: rename DynamicLamp to DynamicLampComponent for improved clarity and consistency
This commit is contained in:
@@ -3,13 +3,17 @@ from esphome.components import output
|
|||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.const import CONF_ID
|
from esphome.const import CONF_ID
|
||||||
|
|
||||||
|
CODEOWNERS = ["@p1ngb4ck"]
|
||||||
|
MULTI_CONF = False
|
||||||
|
|
||||||
dynamic_lamp_ns = cg.esphome_ns.namespace('dynamic_lamp')
|
dynamic_lamp_ns = cg.esphome_ns.namespace('dynamic_lamp')
|
||||||
DynamicLamp = dynamic_lamp_ns.class_('DynamicLamp', cg.Component)
|
DynamicLampComponent = dynamic_lamp_ns.class_('DynamicLampComponent', cg.Component)
|
||||||
|
CONF_DYNAMIC_LAMP_ID = "dynamic_lamp_id"
|
||||||
|
|
||||||
CONF_SAVE_MODE = 'save_mode'
|
CONF_SAVE_MODE = 'save_mode'
|
||||||
CONF_AVAILABLE_OUTPUTS = 'available_outputs'
|
CONF_AVAILABLE_OUTPUTS = 'available_outputs'
|
||||||
CONFIG_SCHEMA = cv.Schema({
|
CONFIG_SCHEMA = cv.Schema({
|
||||||
cv.GenerateID(): cv.declare_id(DynamicLamp),
|
cv.GenerateID(): cv.declare_id(DynamicLampComponent),
|
||||||
cv.Required(CONF_AVAILABLE_OUTPUTS): [cv.use_id(output.FloatOutput)],
|
cv.Required(CONF_AVAILABLE_OUTPUTS): [cv.use_id(output.FloatOutput)],
|
||||||
cv.Optional(CONF_SAVE_MODE, default=0): cv.int_range(0, 1),
|
cv.Optional(CONF_SAVE_MODE, default=0): cv.int_range(0, 1),
|
||||||
}).extend(cv.COMPONENT_SCHEMA)
|
}).extend(cv.COMPONENT_SCHEMA)
|
||||||
|
@@ -16,11 +16,11 @@ namespace dynamic_lamp {
|
|||||||
|
|
||||||
static const char *TAG = "dynamic_lamp";
|
static const char *TAG = "dynamic_lamp";
|
||||||
|
|
||||||
void DynamicLamp::setup() {
|
void DynamicLampComponent::setup() {
|
||||||
this->begin();
|
this->begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::begin() {
|
void DynamicLampComponent::begin() {
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
||||||
bool valid = true;
|
bool valid = true;
|
||||||
if(this->save_mode_ == 0) {
|
if(this->save_mode_ == 0) {
|
||||||
@@ -41,7 +41,7 @@ void DynamicLamp::begin() {
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::loop() {
|
void DynamicLampComponent::loop() {
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
||||||
for (i = 0; i < this->lamp_count_; i++) {
|
for (i = 0; i < this->lamp_count_; i++) {
|
||||||
if (this->active_lamps_[i].active) {
|
if (this->active_lamps_[i].active) {
|
||||||
@@ -73,7 +73,7 @@ void DynamicLamp::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::dump_config() {
|
void DynamicLampComponent::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "Dynamic Lamp feature loaded");
|
ESP_LOGCONFIG(TAG, "Dynamic Lamp feature loaded");
|
||||||
switch(this->save_mode_) {
|
switch(this->save_mode_) {
|
||||||
case SAVE_MODE_NONE:
|
case SAVE_MODE_NONE:
|
||||||
@@ -93,11 +93,11 @@ void DynamicLamp::dump_config() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::set_save_mode(uint8_t save_mode) {
|
void DynamicLampComponent::set_save_mode(uint8_t save_mode) {
|
||||||
this->save_mode_ = save_mode;
|
this->save_mode_ = save_mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::add_available_output(output::FloatOutput * output, std::string output_id) {
|
void DynamicLampComponent::add_available_output(output::FloatOutput * output, std::string output_id) {
|
||||||
uint8_t counter = 0;
|
uint8_t counter = 0;
|
||||||
while (this->available_outputs_[counter].available) {
|
while (this->available_outputs_[counter].available) {
|
||||||
counter++;
|
counter++;
|
||||||
@@ -113,7 +113,7 @@ void DynamicLamp::add_available_output(output::FloatOutput * output, std::string
|
|||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::add_lamp(std::string name) {
|
void DynamicLampComponent::add_lamp(std::string name) {
|
||||||
if (this->lamp_count_ < 15) {
|
if (this->lamp_count_ < 15) {
|
||||||
this->lamp_count_++;
|
this->lamp_count_++;
|
||||||
this->active_lamps_[this->lamp_count_].active = true;
|
this->active_lamps_[this->lamp_count_].active = true;
|
||||||
@@ -128,7 +128,7 @@ void DynamicLamp::add_lamp(std::string name) {
|
|||||||
this->status_set_warning();
|
this->status_set_warning();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::remove_lamp(std::string lamp_name) {
|
void DynamicLampComponent::remove_lamp(std::string lamp_name) {
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
||||||
while (i < this->lamp_count_) {
|
while (i < this->lamp_count_) {
|
||||||
if (this->active_lamps_[i].name == lamp_name) {
|
if (this->active_lamps_[i].name == lamp_name) {
|
||||||
@@ -144,7 +144,7 @@ void DynamicLamp::remove_lamp(std::string lamp_name) {
|
|||||||
ESP_LOGW(TAG, "No lamp with name %s defined !", lamp_name.c_str());
|
ESP_LOGW(TAG, "No lamp with name %s defined !", lamp_name.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::add_output_to_lamp(std::string lamp_name, LinkedOutput *output) {
|
void DynamicLampComponent::add_output_to_lamp(std::string lamp_name, LinkedOutput *output) {
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
||||||
while (i < 16) {
|
while (i < 16) {
|
||||||
if (this->active_lamps_[i].name == lamp_name) {
|
if (this->active_lamps_[i].name == lamp_name) {
|
||||||
@@ -159,7 +159,7 @@ void DynamicLamp::add_output_to_lamp(std::string lamp_name, LinkedOutput *output
|
|||||||
ESP_LOGW(TAG, "No lamp with name %s defined !", lamp_name.c_str());
|
ESP_LOGW(TAG, "No lamp with name %s defined !", lamp_name.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::remove_output_from_lamp(std::string lamp_name, LinkedOutput *output) {
|
void DynamicLampComponent::remove_output_from_lamp(std::string lamp_name, LinkedOutput *output) {
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
||||||
while (i < 16) {
|
while (i < 16) {
|
||||||
if (this->active_lamps_[i].name == lamp_name) {
|
if (this->active_lamps_[i].name == lamp_name) {
|
||||||
@@ -174,7 +174,7 @@ void DynamicLamp::remove_output_from_lamp(std::string lamp_name, LinkedOutput *o
|
|||||||
ESP_LOGW(TAG, "No lamp with name %s defined !", lamp_name.c_str());
|
ESP_LOGW(TAG, "No lamp with name %s defined !", lamp_name.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::array<bool, 16> DynamicLamp::get_lamp_outputs(uint8_t lamp_number) {
|
std::array<bool, 16> DynamicLampComponent::get_lamp_outputs(uint8_t lamp_number) {
|
||||||
std::array<bool, 16> bool_array;
|
std::array<bool, 16> bool_array;
|
||||||
for (uint8_t i = 0; i < 16; i++) {
|
for (uint8_t i = 0; i < 16; i++) {
|
||||||
bool_array[i] = this->active_lamps_[lamp_number].used_outputs[i];
|
bool_array[i] = this->active_lamps_[lamp_number].used_outputs[i];
|
||||||
@@ -182,7 +182,7 @@ std::array<bool, 16> DynamicLamp::get_lamp_outputs(uint8_t lamp_number) {
|
|||||||
return bool_array;
|
return bool_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::array<bool, 16> DynamicLamp::get_lamp_outputs_by_name(std::string lamp_name) {
|
std::array<bool, 16> DynamicLampComponent::get_lamp_outputs_by_name(std::string lamp_name) {
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
||||||
std::array<bool, 16> bool_array;
|
std::array<bool, 16> bool_array;
|
||||||
for (i = 0; i < this->lamp_count_; i++) {
|
for (i = 0; i < this->lamp_count_; i++) {
|
||||||
@@ -195,11 +195,15 @@ std::array<bool, 16> DynamicLamp::get_lamp_outputs_by_name(std::string lamp_name
|
|||||||
return bool_array;
|
return bool_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::set_lamp_values_(uint8_t lamp_number, bool active, uint16_t selected_outputs, uint8_t mode, uint8_t mode_value) {
|
void DynamicLampComponent::set_lamp_level(std::string lamp_name, float state) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::restore_lamp_values_(uint8_t lamp_number) {
|
void DynamicLampComponent::set_lamp_values_(uint8_t lamp_number, bool active, uint16_t selected_outputs, uint8_t mode, uint8_t mode_value) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void DynamicLampComponent::restore_lamp_values_(uint8_t lamp_number) {
|
||||||
this->active_lamps_[lamp_number].active = false;
|
this->active_lamps_[lamp_number].active = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -31,14 +31,36 @@ struct LinkedOutput {
|
|||||||
bool update_level = false;
|
bool update_level = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum DynamicLampIdx : uint8_t {
|
||||||
|
LAMP_1 = 0,
|
||||||
|
LAMP_2 = 1,
|
||||||
|
LAMP_3 = 2,
|
||||||
|
LAMP_4 = 3,
|
||||||
|
LAMP_5 = 4,
|
||||||
|
LAMP_6 = 5,
|
||||||
|
LAMP_7 = 6,
|
||||||
|
LAMP_8 = 7,
|
||||||
|
LAMP_9 = 8,
|
||||||
|
LAMP_10 = 9,
|
||||||
|
LAMP_11 = 10,
|
||||||
|
LAMP_12 = 11,
|
||||||
|
LAMP_13 = 12,
|
||||||
|
LAMP_14 = 13,
|
||||||
|
LAMP_15 = 14,
|
||||||
|
LAMP_16 = 15,
|
||||||
|
};
|
||||||
|
|
||||||
struct CombinedLamp {
|
struct CombinedLamp {
|
||||||
bool active = false;
|
bool active = false;
|
||||||
std::string name = "";
|
std::string name = "";
|
||||||
uint8_t lamp_index;
|
uint8_t lamp_index;
|
||||||
|
float state;
|
||||||
bool used_outputs[16];
|
bool used_outputs[16];
|
||||||
};
|
};
|
||||||
|
|
||||||
class DynamicLamp : public Component {
|
class DynamicLamp;
|
||||||
|
|
||||||
|
class DynamicLampComponent : public Component {
|
||||||
public:
|
public:
|
||||||
void setup() override;
|
void setup() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
@@ -54,6 +76,8 @@ class DynamicLamp : public Component {
|
|||||||
std::array<bool, 16> get_lamp_outputs_by_name(std::string lamp_name);
|
std::array<bool, 16> get_lamp_outputs_by_name(std::string lamp_name);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
friend class DynamicLamp;
|
||||||
|
void set_lamp_level(std::string lamp_name, float state);
|
||||||
void restore_lamp_values_(uint8_t lamp_number);
|
void restore_lamp_values_(uint8_t lamp_number);
|
||||||
void set_lamp_values_(uint8_t lamp_number, bool active, uint16_t selected_outputs, uint8_t mode, uint8_t mode_value);
|
void set_lamp_values_(uint8_t lamp_number, bool active, uint16_t selected_outputs, uint8_t mode, uint8_t mode_value);
|
||||||
|
|
||||||
|
50
esphome/components/dynamic_lamp/output/__init__.py
Normal file
50
esphome/components/dynamic_lamp/output/__init__.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import esphome.codegen as cg # noqa: I001
|
||||||
|
from esphome.components import output
|
||||||
|
import esphome.config_validation as cv
|
||||||
|
from esphome.const import CONF_CHANNEL, CONF_ID
|
||||||
|
from .. import CONF_DYNAMIC_LAMP_ID, DynamicLampComponent, dynamic_lamp_ns
|
||||||
|
|
||||||
|
DEPENDENCIES = ["dynamic_lamp"]
|
||||||
|
|
||||||
|
DynamicLamp = dynamic_lamp_ns.class_(
|
||||||
|
"DynamicLamp", output.FloatOutput, cg.Parented.template(DynamicLampComponent)
|
||||||
|
)
|
||||||
|
|
||||||
|
DynamicLampIdx = dynamic_lamp_ns.enum("DynamicLampIdx")
|
||||||
|
CHANNEL_OPTIONS = {
|
||||||
|
"A": DynamicLampIdx.LAMP_1,
|
||||||
|
"B": DynamicLampIdx.LAMP_2,
|
||||||
|
"C": DynamicLampIdx.LAMP_3,
|
||||||
|
"D": DynamicLampIdx.LAMP_4,
|
||||||
|
"E": DynamicLampIdx.LAMP_5,
|
||||||
|
"F": DynamicLampIdx.LAMP_6,
|
||||||
|
"G": DynamicLampIdx.LAMP_7,
|
||||||
|
"H": DynamicLampIdx.LAMP_8,
|
||||||
|
"I": DynamicLampIdx.LAMP_9,
|
||||||
|
"J": DynamicLampIdx.LAMP_10,
|
||||||
|
"K": DynamicLampIdx.LAMP_11,
|
||||||
|
"L": DynamicLampIdx.LAMP_12,
|
||||||
|
"M": DynamicLampIdx.LAMP_13,
|
||||||
|
"N": DynamicLampIdx.LAMP_14,
|
||||||
|
"O": DynamicLampIdx.LAMP_15,
|
||||||
|
"P": DynamicLampIdx.LAMP_16,
|
||||||
|
}
|
||||||
|
|
||||||
|
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||||
|
{
|
||||||
|
cv.Required(CONF_ID): cv.declare_id(DynamicLamp),
|
||||||
|
cv.GenerateID(CONF_DYNAMIC_LAMP_ID): cv.use_id(DynamicLampComponent),
|
||||||
|
cv.Required(CONF_CHANNEL): cv.enum(CHANNEL_OPTIONS, upper=True),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def to_code(config):
|
||||||
|
parent = await cg.get_variable(config[CONF_DYNAMIC_LAMP_ID])
|
||||||
|
var = cg.new_Pvariable(
|
||||||
|
config[CONF_ID],
|
||||||
|
parent,
|
||||||
|
config[CONF_CHANNEL],
|
||||||
|
)
|
||||||
|
await output.register_output(var, config)
|
||||||
|
await cg.register_parented(var, config[CONF_DYNAMIC_LAMP_ID])
|
@@ -0,0 +1,15 @@
|
|||||||
|
#include "dynamic_lamp_output.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace dynamic_lamp {
|
||||||
|
|
||||||
|
static const char *const TAG = "dynamic_lamp.output";
|
||||||
|
|
||||||
|
void DynamicLamp::write_state(float state) {
|
||||||
|
this->state_ = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dynamic_lamp
|
||||||
|
} // namespace esphome
|
22
esphome/components/dynamic_lamp/output/dynamic_lamp_output.h
Normal file
22
esphome/components/dynamic_lamp/output/dynamic_lamp_output.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../dynamic_lamp.h"
|
||||||
|
#include "esphome/core/component.h"
|
||||||
|
#include "esphome/components/output/float_output.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace dnamic_lamp {
|
||||||
|
|
||||||
|
class DynamicLamp : public output::FloatOutput, public Parented<DynamicLampComponent> {
|
||||||
|
public:
|
||||||
|
DynamicLamp(DynamicLampComponent *parent, DynamicLampIdx lamp) : parent_(parent), lamp_(lamp) {}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void write_state(float state) override;
|
||||||
|
DynamicLampComponent *parent_;
|
||||||
|
DynamicLampIdx lamp_;
|
||||||
|
float state_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace dynamic_lamp
|
||||||
|
} // namespace esphome
|
Reference in New Issue
Block a user