From ce09d85bd331b01f8f6620ed3dfab36e56a2c3fc Mon Sep 17 00:00:00 2001 From: sqozz Date: Sun, 15 Sep 2024 18:44:45 +0200 Subject: [PATCH] Add command switches --- __init__.py | 1 + switch/__init__.py | 29 +++++++++++++++++++++++++++++ switch/tw7100_switch.cpp | 26 ++++++++++++++++++++++++++ switch/tw7100_switch.h | 24 ++++++++++++++++++++++++ tw7100.cpp | 6 ++++++ tw7100.h | 3 +++ 6 files changed, 89 insertions(+) create mode 100644 switch/__init__.py create mode 100644 switch/tw7100_switch.cpp create mode 100644 switch/tw7100_switch.h diff --git a/__init__.py b/__init__.py index e494da0..a8553f3 100644 --- a/__init__.py +++ b/__init__.py @@ -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) diff --git a/switch/__init__.py b/switch/__init__.py new file mode 100644 index 0000000..fa02f28 --- /dev/null +++ b/switch/__init__.py @@ -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)) diff --git a/switch/tw7100_switch.cpp b/switch/tw7100_switch.cpp new file mode 100644 index 0000000..1202a1c --- /dev/null +++ b/switch/tw7100_switch.cpp @@ -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 diff --git a/switch/tw7100_switch.h b/switch/tw7100_switch.h new file mode 100644 index 0000000..6531b0c --- /dev/null +++ b/switch/tw7100_switch.h @@ -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 diff --git a/tw7100.cpp b/tw7100.cpp index cdfca72..abfe89f 100644 --- a/tw7100.cpp +++ b/tw7100.cpp @@ -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 data) { static const char *const TAG = "update_sensor()"; std::string cmd = data.first; diff --git a/tw7100.h b/tw7100.h index 7839527..00a8912 100644 --- a/tw7100.h +++ b/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 data); std::pair 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