Add command switches
This commit is contained in:
parent
16ed3a216e
commit
ce09d85bd3
|
@ -11,6 +11,7 @@ AUTO_LOAD = ["binary_sensor", "sensor", "text_sensor"]
|
|||
tw7100_ns = cg.esphome_ns.namespace("tw7100")
|
||||
tw7100 = tw7100_ns.class_("tw7100Component", cg.PollingComponent, uart.UARTDevice)
|
||||
|
||||
CONF_TW7100_ID = "tw7100_id"
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
cv.GenerateID(): cv.declare_id(tw7100),
|
||||
}).extend(cv.polling_component_schema("60s")).extend(uart.UART_DEVICE_SCHEMA)
|
||||
|
|
29
switch/__init__.py
Normal file
29
switch/__init__.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import switch
|
||||
from esphome.const import CONF_POWER, ENTITY_CATEGORY_CONFIG, CONF_ID
|
||||
|
||||
from .. import tw7100, tw7100_ns, CONF_TW7100_ID
|
||||
|
||||
DEPENDENCIES = ["tw7100"]
|
||||
CODEOWNERS = ["@sqozz"]
|
||||
|
||||
tw7100Switch = tw7100_ns.class_("tw7100Switch", switch.Switch, cg.Component)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(tw7100),
|
||||
cv.Optional("PWR"): switch.switch_schema(
|
||||
tw7100Switch
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
parent = await cg.get_variable(config[CONF_ID])
|
||||
|
||||
if "PWR" in config:
|
||||
switch_ = await switch.new_switch(config["PWR"])
|
||||
cg.add(switch_.set_cmd("PWR"))
|
||||
cg.add(switch_.set_tw7100_parent(parent))
|
26
switch/tw7100_switch.cpp
Normal file
26
switch/tw7100_switch.cpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include "tw7100_switch.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace tw7100 {
|
||||
|
||||
void tw7100Switch::dump_config() {
|
||||
static const char *const TAG = "dump_config()";
|
||||
ESP_LOGCONFIG(TAG, "TW7100:");
|
||||
LOG_SWITCH(TAG, "tw7100Switch", this);
|
||||
publish_state(state);
|
||||
}
|
||||
|
||||
void tw7100Switch::write_state(bool state) {
|
||||
static const char *const TAG = "write_state()";
|
||||
ESP_LOGV(TAG, "write switch state for cmd: %s", this->cmd_.c_str());
|
||||
parent_->push_cmd("PWR?");
|
||||
// TODO: actually send something!
|
||||
// TODO: check response before publishing state
|
||||
publish_state(state);
|
||||
}
|
||||
|
||||
} // namespace tw7100
|
||||
} // namespace esphome
|
24
switch/tw7100_switch.h
Normal file
24
switch/tw7100_switch.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/switch/switch.h"
|
||||
#include "../tw7100.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace tw7100 {
|
||||
|
||||
class tw7100Switch : public switch_::Switch, public Component {
|
||||
public:
|
||||
void dump_config() override;
|
||||
void set_tw7100_parent(tw7100Component *parent) { this->parent_ = parent; }
|
||||
void set_cmd(std::string cmd) { this->cmd_ = cmd; }
|
||||
|
||||
protected:
|
||||
void write_state(bool state) override;
|
||||
|
||||
std::string cmd_;
|
||||
tw7100Component *parent_;
|
||||
};
|
||||
|
||||
} // namespace tw7100
|
||||
} // namespace esphome
|
|
@ -159,6 +159,12 @@ void tw7100Component::dump_config() {
|
|||
this->check_uart_settings(9600);
|
||||
};
|
||||
|
||||
void tw7100Component::push_cmd(std::string cmd) {
|
||||
static const char *const TAG = "push_cmd()";
|
||||
cmd_queue.push_front(cmd);
|
||||
ESP_LOGV(TAG, "pushing priority cmd (%s) from external component", cmd.c_str());
|
||||
}
|
||||
|
||||
void tw7100Component::update_sensor(std::pair<std::string, std::string> data) {
|
||||
static const char *const TAG = "update_sensor()";
|
||||
std::string cmd = data.first;
|
||||
|
|
3
tw7100.h
3
tw7100.h
|
@ -19,6 +19,7 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice {
|
|||
tw7100Component() = default;
|
||||
|
||||
std::string readStringUntil(char terminator);
|
||||
void push_cmd(std::string cmd);
|
||||
void update_sensor(std::pair<std::string, std::string> data);
|
||||
std::pair<std::string, std::string> parse_response(std::string data);
|
||||
int timedRead(void);
|
||||
|
@ -34,6 +35,7 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice {
|
|||
void set_signal_status(sensor::Sensor *signal_status) { this->signal_status_ = signal_status; }
|
||||
void set_luminance_level(sensor::Sensor *luminance_level) { this->luminance_level_ = luminance_level; }
|
||||
void set_serial(text_sensor::TextSensor *serial) { this->serial_ = serial; }
|
||||
//void set_set_power(tw7100::tw7100Switch *switch_) { this->power_switch_ = switch_; }
|
||||
|
||||
protected:
|
||||
binary_sensor::BinarySensor *powered_{nullptr};
|
||||
|
@ -43,6 +45,7 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice {
|
|||
sensor::Sensor *signal_status_{nullptr};
|
||||
sensor::Sensor *luminance_level_{nullptr};
|
||||
text_sensor::TextSensor *serial_{nullptr};
|
||||
//tw7100::tw7100Switch *power_switch_{nullptr};
|
||||
};
|
||||
|
||||
} // namespace tw7100
|
||||
|
|
Loading…
Reference in a new issue