Changed proposed updates

This commit is contained in:
KoenBreeman 2024-08-16 10:40:23 +02:00
parent 7226e89d8d
commit 6d78756b0c
18 changed files with 70 additions and 210 deletions

View File

@ -8,15 +8,15 @@ DEPENDENCIES = ["i2c"]
MULTI_CONF = True
CONF_M5STACK4RELAY_ID = "m5stack4relay_id"
CONF_M5STACK_4RELAY_ID = "m5stack4relay_id"
m5stack4relay_ns = cg.esphome_ns.namespace("m5stack4relay")
M5Stack4Relay = m5stack4relay_ns.class_("M5Stack4Relay", cg.Component, i2c.I2CDevice)
m5stack_4relay_ns = cg.esphome_ns.namespace("m5stack_4relay")
M5Stack_4Relay = m5stack4relay_ns.class_("M5Stack_4Relay", cg.Component, i2c.I2CDevice)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(M5Stack4Relay),
cv.GenerateID(): cv.declare_id(M5Stack_4Relay),
}
)
.extend(cv.COMPONENT_SCHEMA)

View File

@ -1,25 +1,25 @@
#include "m5stack4relay.h"
#include "m5stack_4relay.h"
#include "esphome/core/log.h"
namespace esphome {
namespace m5stack4relay {
namespace m5stack_4relay {
static const char *const TAG = "m5stack_4_relay";
void M5Stack4Relay::dump_config() {
void M5Stack_4Relay::dump_config() {
ESP_LOGCONFIG(TAG, "M5Stack 4 Relays:");
LOG_I2C_DEVICE(this);
}
/*! @brief Setting the mode of the device, and turn off all relays.
/*! @brief Setting the mode of the device, and turn off all relays, restore the switches with restore mode.
* @param mode Async = 0, Sync = 1. */
void M5Stack4Relay::init_(bool mode) {
void M5Stack_4Relay::init_(bool mode) {
this->write1_byte_(UNIT_4RELAY_REG, mode);
this->write1_byte_(UNIT_4RELAY_RELAY_REG, 0);
}
/*! @brief Read a certain length of data to the specified register address. */
uint8_t M5Stack4Relay::read1_byte_(uint8_t register_address) {
uint8_t M5Stack_4Relay::read1_byte_(uint8_t register_address) {
uint8_t data;
if (!this->read_byte(register_address, &data)) {
ESP_LOGW(TAG, "Read from relay failed!");
@ -32,7 +32,7 @@ uint8_t M5Stack4Relay::read1_byte_(uint8_t register_address) {
/*! @brief Control the on/off of the specified relay.
* @param number Bit number of relay (0~3).
@param state OFF = 0, ON = 1 . */
void M5Stack4Relay::relay_write(uint8_t number, bool state) {
void M5Stack_4Relay::relay_write(uint8_t number, bool state) {
uint8_t state_from_device = this->read1_byte_(UNIT_4RELAY_RELAY_REG);
if (state == 0) {
state_from_device &= ~(0x01 << number);
@ -42,7 +42,7 @@ void M5Stack4Relay::relay_write(uint8_t number, bool state) {
this->write1_byte_(UNIT_4RELAY_RELAY_REG, state_from_device);
}
void M5Stack4Relay::setup() {
void M5Stack_4Relay::setup() {
ESP_LOGCONFIG(TAG, "Setting up M5Stack_4_Relays...");
uint8_t setupmode = 1;
this->init_(setupmode);
@ -50,10 +50,10 @@ void M5Stack4Relay::setup() {
/*! @brief Setting the mode of the device.
* @param mode Async = 0, Sync = 1. */
void M5Stack4Relay::set_switch_mode(bool mode) { this->write1_byte_(UNIT_4RELAY_REG, mode); }
void M5Stack_4Relay::set_switch_mode(bool mode) { this->write1_byte_(UNIT_4RELAY_REG, mode); }
/*! @brief Write a certain length of data to the specified register address. */
void M5Stack4Relay::write1_byte_(uint8_t register_address, uint8_t data) {
void M5Stack_4Relay::write1_byte_(uint8_t register_address, uint8_t data) {
if (!this->write_byte(register_address, data)) {
ESP_LOGW(TAG, "Write to relay failed!");
this->status_set_warning();
@ -61,5 +61,5 @@ void M5Stack4Relay::write1_byte_(uint8_t register_address, uint8_t data) {
}
}
} // namespace m5stack4relay
} // namespace m5stack_4relay
} // namespace esphome

View File

@ -4,13 +4,13 @@
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace m5stack4relay {
namespace m5stack_4relay {
static constexpr uint8_t UNIT_4RELAY_REG = 0X10;
static constexpr uint8_t UNIT_4RELAY_RELAY_REG = 0X11;
class M5Stack4Relay : public Component, public i2c::I2CDevice {
class M5Stack_4Relay : public Component, public i2c::I2CDevice {
public:
void set_switch_mode(bool mode);
@ -27,5 +27,5 @@ class M5Stack4Relay : public Component, public i2c::I2CDevice {
void setup() override;
};
} // namespace m5stack4relay
} // namespace m5stack_4relay
} // namespace esphome

View File

@ -3,29 +3,22 @@ import esphome.config_validation as cv
from esphome.components import i2c, switch
from esphome.const import CONF_CHANNEL, CONF_INTERLOCK # , CONF_ID,
from .. import m5stack4relay_ns, M5Stack4Relay, CONF_M5STACK4RELAY_ID
from .. import m5stack_4relay_ns, M5Stack_4Relay, CONF_M5STACK_4RELAY_ID
DEPENDENCIES = ["m5stack4relay"]
DEPENDENCIES = ["m5stack_4relay"]
M5StackSwitch = m5stack4relay_ns.class_(
"M5Stack4RelaySwitch", cg.Component, i2c.I2CDevice, switch.Switch
M5StackSwitch = m5stack_4relay_ns.class_(
"M5Stack_4RelaySwitch", cg.Component, i2c.I2CDevice, switch.Switch
)
CONF_INTERLOCK_WAIT_TIME = "interlock_wait_time"
CONF_Relay_1 = 1
CONF_Relay_2 = 2
CONF_Relay_3 = 3
CONF_Relay_4 = 4
CONFIG_SCHEMA = (
switch.switch_schema(M5StackSwitch)
.extend(
{
cv.GenerateID(): cv.declare_id(M5StackSwitch),
cv.GenerateID(CONF_M5STACK4RELAY_ID): cv.use_id(M5Stack4Relay),
cv.GenerateID(CONF_M5STACK_4RELAY_ID): cv.use_id(M5Stack_4Relay),
cv.Required(CONF_CHANNEL): cv.int_range(min=1, max=4),
}
)
@ -36,7 +29,7 @@ CONFIG_SCHEMA = (
async def to_code(config):
var = await switch.new_switch(config)
await cg.register_component(var, config)
await cg.register_parented(var, config[CONF_M5STACK4RELAY_ID])
await cg.register_parented(var, config[CONF_M5STACK_4RELAY_ID])
cg.add(var.set_channel(config[CONF_CHANNEL]))

View File

@ -1,14 +1,14 @@
#include "esphome/core/log.h"
#include "m5stack4relay_switch.h"
#include "m5stack_4relay_switch.h"
namespace esphome {
namespace m5stack4relay {
namespace m5stack_4relay {
static const char *const TAG = "switch.M5Stack_4_Relay";
float M5Stack4RelaySwitch::get_setup_priority() const { return setup_priority::HARDWARE; }
float M5Stack_4RelaySwitch::get_setup_priority() const { return setup_priority::HARDWARE; }
void M5Stack4RelaySwitch::setup() {
void M5Stack_4RelaySwitch::setup() {
ESP_LOGCONFIG(TAG, "Setting up M5Stack_4_relay Switch '%s'...", this->name_.c_str());
bool initial_state = this->get_initial_state_with_restore_mode().value_or(false);
@ -21,9 +21,9 @@ void M5Stack4RelaySwitch::setup() {
}
}
void M5Stack4RelaySwitch::dump_config() {
LOG_SWITCH("", "M5Stack4Relay Switch", this);
ESP_LOGCONFIG(TAG, " Channel: %u", this->channel_);
void M5Stack_4RelaySwitch::dump_config() {
LOG_SWITCH("", "M5Stack_4Relay Switch", this);
ESP_LOGCONFIG(TAG, " Channel: %u", this->channel_);
if (!this->interlock_.empty()) {
ESP_LOGCONFIG(TAG, " Interlocks:");
@ -35,7 +35,7 @@ ESP_LOGCONFIG(TAG, " Channel: %u", this->channel_);
}
}
void M5Stack4RelaySwitch::write_state(bool state) {
void M5Stack_4RelaySwitch::write_state(bool state) {
if (state != this->inverted_) {
// Turning ON, check interlocking
@ -70,7 +70,7 @@ void M5Stack4RelaySwitch::write_state(bool state) {
this->publish_state(state);
}
void M5Stack4RelaySwitch::set_interlock(const std::vector<Switch *> &interlock) { this->interlock_ = interlock; }
void M5Stack_4RelaySwitch::set_interlock(const std::vector<Switch *> &interlock) { this->interlock_ = interlock; }
} // namespace m5stack4relay
} // namespace m5stack_4relay
} // namespace esphome

View File

@ -3,12 +3,12 @@
#include "esphome/core/component.h"
#include "esphome/components/switch/switch.h"
#include "esphome/components/m5stack4relay/m5stack4relay.h"
#include "esphome/components/m5stack_4relay/m5stack_4relay.h"
namespace esphome {
namespace m5stack4relay {
namespace m5stack_4relay {
class M5Stack4RelaySwitch : public Component, public switch_::Switch, public Parented<M5Stack4Relay> {
class M5Stack_4RelaySwitch : public Component, public switch_::Switch, public Parented<M5Stack4Relay> {
public:
float get_setup_priority() const override;
@ -27,5 +27,5 @@ class M5Stack4RelaySwitch : public Component, public switch_::Switch, public Par
uint32_t interlock_wait_time_{0};
};
} // namespace m5stack4relay
} // namespace m5stack_4relay
} // namespace esphome

View File

@ -1,33 +0,0 @@
i2c:
- id: i2c_m5stack4relay
scl: 5
sda: 4
m5stack4relay:
id: M5stackrelay_ID1
switch:
- platform: m5stack4relay
id: m5stack4relay_1
name: m5stack4relay1
channel: 1
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_2
name: m5stack4relay2
channel: 2
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_3
name: m5stack4relay3
channel: 3
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_4
name: m5stack4relay4
channel: 4
m5stack4relay_id: M5stackrelay_ID1

View File

@ -1,33 +0,0 @@
i2c:
- id: i2c_m5stack4relay
scl: 5
sda: 4
m5stack4relay:
id: M5stackrelay_ID1
switch:
- platform: m5stack4relay
id: m5stack4relay_1
name: m5stack4relay1
channel: 1
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_2
name: m5stack4relay2
channel: 2
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_3
name: m5stack4relay3
channel: 3
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_4
name: m5stack4relay4
channel: 4
m5stack4relay_id: M5stackrelay_ID1

View File

@ -1,33 +0,0 @@
i2c:
- id: i2c_m5stack4relay
scl: 16
sda: 17
m5stack4relay:
id: M5stackrelay_ID1
switch:
- platform: m5stack4relay
id: m5stack4relay_1
name: m5stack4relay1
channel: 1
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_2
name: m5stack4relay2
channel: 2
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_3
name: m5stack4relay3
channel: 3
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_4
name: m5stack4relay4
channel: 4
m5stack4relay_id: M5stackrelay_ID1

View File

@ -1,32 +0,0 @@
i2c:
- id: i2c_m5stack4relay
scl: 5
sda: 4
m5stack4relay:
id: M5stackrelay_ID1
switch:
- platform: m5stack4relay
id: m5stack4relay_1
name: m5stack4relay1
channel: 1
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_2
name: m5stack4relay2
channel: 2
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_3
name: m5stack4relay3
channel: 3
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_4
name: m5stack4relay4
channel: 4
m5stack4relay_id: M5stackrelay_ID1

View File

@ -1,32 +0,0 @@
i2c:
- id: i2c_m5stack4relay
scl: 5
sda: 4
m5stack4relay:
id: M5stackrelay_ID1
switch:
- platform: m5stack4relay
id: m5stack4relay_1
name: m5stack4relay1
channel: 1
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_2
name: m5stack4relay2
channel: 2
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_3
name: m5stack4relay3
channel: 3
m5stack4relay_id: M5stackrelay_ID1
- platform: m5stack4relay
id: m5stack4relay_4
name: m5stack4relay4
channel: 4
m5stack4relay_id: M5stackrelay_ID1

View File

@ -1,7 +1,7 @@
i2c:
- id: i2c_m5stack4relay
scl: 16
sda: 17
scl: ${scl_pin}
sda: ${sda_pin}
m5stack4relay:
id: M5stackrelay_ID1

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: 16
sda_pin: 17
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: 5
sda_pin: 4
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: 5
sda_pin: 4
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: 16
sda_pin: 17
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: 5
sda_pin: 4
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: 5
sda_pin: 4
<<: !include common.yaml