Created seeedmultichanelle relay

This commit is contained in:
KoenBreeman 2024-08-09 16:15:53 +02:00
parent a7167ec3bf
commit aecca5eea6
14 changed files with 536 additions and 0 deletions

View File

@ -334,6 +334,7 @@ esphome/components/sdl/* @clydebarrow
esphome/components/sdm_meter/* @jesserockz @polyfaces
esphome/components/sdp3x/* @Azimath
esphome/components/seeed_mr24hpc1/* @limengdu
esphome/components/seeedmultichannelrelay/* @KoenBreeman
esphome/components/selec_meter/* @sourabhjaiswal
esphome/components/select/* @esphome/core
esphome/components/sen0321/* @notjj

View File

@ -0,0 +1,38 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c
from esphome.const import CONF_ID
CODEOWNERS = ["@KoenBreeman"]
DEPENDENCIES = ["i2c"]
MULTI_CONF = True
CONF_I2C_ADDR = 0x11
CONF_SEEEDMULTICHANNELRELAY_ID = "seeedmultichannelrelay_id"
CONF_CHANGE_ADDRESS_TO = "change_address_to"
seeedmultichannelrelay_ns = cg.esphome_ns.namespace("seeedmultichannelrelay")
SeeedMultiChannelRelay = seeedmultichannelrelay_ns.class_(
"SeeedMultiChannelRelay", cg.Component, i2c.I2CDevice
)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(SeeedMultiChannelRelay),
cv.Optional(CONF_CHANGE_ADDRESS_TO): cv.hex_int_range(min=0x00, max=0x7F),
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(i2c.i2c_device_schema(CONF_I2C_ADDR))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
if CONF_CHANGE_ADDRESS_TO in config:
cg.add(var.change_i2c_address(config[CONF_CHANGE_ADDRESS_TO]))

View File

@ -0,0 +1,82 @@
#include "seeedmultichannelrelay.h"
#include "esphome/core/log.h"
#include "seeedmultichannelrelay.h"
namespace esphome {
namespace seeedmultichannelrelay {
static const char *const TAG = "SeeedMultiChannelRelay";
void SeeedMultiChannelRelay::channel_ctrl_(uint8_t state) {
this->channel_state_ = state;
this->write1_byte_(CMD_CHANNEL_CTRL, state);
}
void SeeedMultiChannelRelay::turn_on_channel_(uint8_t channel) {
this->channel_state_ |= (1 << (channel - 1));
this->channel_ctrl_(channel_state_);
}
void SeeedMultiChannelRelay::turn_off_channel_(uint8_t channel) {
this->channel_state_ &= ~(1 << (channel - 1));
this->channel_ctrl_(channel_state_);
}
void SeeedMultiChannelRelay::dump_config() {
ESP_LOGCONFIG(TAG, "Seeed Multi Channel Relays:");
LOG_I2C_DEVICE(this);
}
/*! @brief Read a certain length of data to the specified register address. */
uint8_t SeeedMultiChannelRelay::read1_byte_(uint8_t register_address) {
uint8_t data;
if (!this->read_byte(register_address, &data)) {
ESP_LOGW(TAG, "Read from relay failed!");
this->status_set_warning();
return uint8_t(0);
}
return data;
}
/*! @brief Control the on/off of the specified relay.
* @param number Bit number of relay (0~3).
@param state OFF = 0, ON = 1 . */
void SeeedMultiChannelRelay::relay_write(uint8_t number, bool state) {
if (state) {
this->turn_on_channel_(number);
} else {
this->turn_off_channel_(number);
}
}
void SeeedMultiChannelRelay::setup() {
ESP_LOGCONFIG(TAG, "Setting up Seeed Multi Channel Relay...");
ESP_LOGCONFIG(TAG, "Firmware version of the Seeed Multi Channel Relay %u", this->get_firmware_version());
if (this->address_changed_) {
this->write1_byte_(CMD_SAVE_I2C_ADDR, this->new_addr_);
this->set_i2c_address(this->new_addr_);
ESP_LOGCONFIG(TAG, "I2C address of control changed to %u", this->new_addr_);
}
}
void SeeedMultiChannelRelay::change_i2c_address(uint8_t new_addr) {
this->new_addr_ = new_addr;
address_changed_ = true;
}
uint8_t SeeedMultiChannelRelay::get_firmware_version() {
uint8_t firmware_from_device = this->read1_byte_(CMD_READ_FIRMWARE_VER);
return firmware_from_device;
}
/*! @brief Write a certain length of data to the specified register address. */
void SeeedMultiChannelRelay::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();
return;
}
}
} // namespace seeedmultichannelrelay
} // namespace esphome

View File

@ -0,0 +1,78 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace seeedmultichannelrelay {
static constexpr uint8_t CMD_CHANNEL_CTRL = 0x10;
static constexpr uint8_t CMD_SAVE_I2C_ADDR = 0x11;
static constexpr uint8_t CMD_READ_I2C_ADDR = 0x12;
static constexpr uint8_t CMD_READ_FIRMWARE_VER = 0x13;
enum class RelayBit : uint8_t {
RELAY1 = 1,
RELAY2 = 2,
RELAY3 = 3,
RELAY4 = 4,
RELAY5 = 5,
RELAY6 = 6,
RELAY7 = 7,
RELAY8 = 8
};
class SeeedMultiChannelRelay : public Component, public i2c::I2CDevice {
public:
void relay_write(uint8_t number, bool state);
/*
@brief Change device address from old_addr to new_addr.
@param new_addr, the address to use.
old_addr, the original address
@return None
*/
void change_i2c_address(uint8_t new_addr);
/*
@brief Read firmware version from on board MCU
@param
@return Firmware version in byte
*/
uint8_t get_firmware_version();
protected:
void write1_byte_(uint8_t register_address, uint8_t data);
uint8_t read1_byte_(uint8_t register_address);
uint8_t channel_state_; // Value to save channel state
uint8_t new_addr_;
bool address_changed_ = false;
/*
@brief Control relay channels
@param state, use one Byte to represent 8 channel
@return None
*/
void channel_ctrl_(uint8_t state);
/*
@brief Turn on one of 8 channels
@param channel, channel to control with (range form 1 to 8)
@return None
*/
void turn_on_channel_(uint8_t channel);
/*
@brief Turn off on of 8 channels
@param channel, channel to control with (range form 1 to 8)
@return None
*/
void turn_off_channel_(uint8_t channel);
void dump_config() override;
void setup() override;
};
} // namespace seeedmultichannelrelay
} // namespace esphome

View File

@ -0,0 +1,75 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, switch
from esphome.const import CONF_CHANNEL, CONF_INTERLOCK
from .. import (
seeedmultichannelrelay_ns,
SeeedMultiChannelRelay,
CONF_SEEEDMULTICHANNELRELAY_ID,
)
DEPENDENCIES = ["seeedmultichannelrelay"]
SeeedMultiChannelRelaySwitch = seeedmultichannelrelay_ns.class_(
"SeeedMultiChannelRelaySwitch", 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
CONF_Relay_5 = 5
CONF_Relay_6 = 6
CONF_Relay_7 = 7
CONF_Relay_8 = 8
RelayBit_ = seeedmultichannelrelay_ns.enum("RelayBit", is_class=True)
SWITCH_MAP = {
CONF_Relay_1: RelayBit_.RELAY1,
CONF_Relay_2: RelayBit_.RELAY2,
CONF_Relay_3: RelayBit_.RELAY3,
CONF_Relay_4: RelayBit_.RELAY4,
CONF_Relay_5: RelayBit_.RELAY5,
CONF_Relay_6: RelayBit_.RELAY6,
CONF_Relay_7: RelayBit_.RELAY7,
CONF_Relay_8: RelayBit_.RELAY8,
}
CONFIG_SCHEMA = (
switch.switch_schema(SeeedMultiChannelRelaySwitch)
.extend(
{
cv.GenerateID(): cv.declare_id(SeeedMultiChannelRelaySwitch),
cv.GenerateID(CONF_SEEEDMULTICHANNELRELAY_ID): cv.use_id(
SeeedMultiChannelRelay
),
cv.Required(CONF_CHANNEL): cv.enum(SWITCH_MAP),
cv.Optional(CONF_INTERLOCK): cv.ensure_list(cv.use_id(switch.Switch)),
cv.Optional(
CONF_INTERLOCK_WAIT_TIME, default="0ms"
): cv.positive_time_period_milliseconds,
}
)
.extend(cv.COMPONENT_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_SEEEDMULTICHANNELRELAY_ID])
cg.add(var.set_channel(config[CONF_CHANNEL]))
if CONF_INTERLOCK in config:
interlock = []
for it in config[CONF_INTERLOCK]:
lock = await cg.get_variable(it)
interlock.append(lock)
cg.add(var.set_interlock(interlock))
cg.add(var.set_interlock_wait_time(config[CONF_INTERLOCK_WAIT_TIME]))

View File

@ -0,0 +1,77 @@
#include "esphome/core/log.h"
#include "seeedmultichannelrelay_switch.h"
namespace esphome {
namespace seeedmultichannelrelay {
static const char *const TAG = "switch.SeeedMultiChannelRelay";
float SeeedMultiChannelRelaySwitch::get_setup_priority() const { return setup_priority::HARDWARE; }
void SeeedMultiChannelRelaySwitch::setup() {
ESP_LOGCONFIG(TAG, "Setting up SeeedMultiChannelRelay Switch '%s'...", this->name_.c_str());
bool initial_state = this->get_initial_state_with_restore_mode().value_or(false);
// write state before setup
if (initial_state) {
this->turn_on();
} else {
this->turn_off();
}
}
void SeeedMultiChannelRelaySwitch::dump_config() {
LOG_SWITCH("", "SeeedMultiChannelRelay Switch", this);
if (!this->interlock_.empty()) {
ESP_LOGCONFIG(TAG, " Interlocks:");
for (auto *lock : this->interlock_) {
if (lock == this)
continue;
ESP_LOGCONFIG(TAG, " %s", lock->get_name().c_str());
}
}
}
void SeeedMultiChannelRelaySwitch::write_state(bool state) {
if (state != this->inverted_) {
// Turning ON, check interlocking
bool found = false;
for (auto *lock : this->interlock_) {
if (lock == this)
continue;
if (lock->state) {
lock->turn_off();
found = true;
}
}
if (found && this->interlock_wait_time_ != 0) {
this->set_timeout("interlock", this->interlock_wait_time_, [this, state] {
// Don't write directly, call the function again
// (some other switch may have changed state while we were waiting)
this->write_state(state);
});
return;
}
} else if (this->interlock_wait_time_ != 0) {
// If we are switched off during the interlock wait time, cancel any pending
// re-activations
this->cancel_timeout("interlock");
}
// This will be called every time the user requests a state change.
this->parent_->relay_write(this->channel_, state);
// Acknowledge new state by publishing it
this->publish_state(state);
}
void SeeedMultiChannelRelaySwitch::set_interlock(const std::vector<Switch *> &interlock) {
this->interlock_ = interlock;
}
} // namespace seeedmultichannelrelay
} // namespace esphome

View File

@ -0,0 +1,31 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/switch/switch.h"
#include "esphome/components/seeedmultichannelrelay/seeedmultichannelrelay.h"
namespace esphome {
namespace seeedmultichannelrelay {
class SeeedMultiChannelRelaySwitch : public Component, public switch_::Switch, public Parented<SeeedMultiChannelRelay> {
public:
float get_setup_priority() const override;
void setup() override;
void dump_config() override;
void write_state(bool state) override;
void set_channel(RelayBit channel) { this->channel_ = (uint8_t) channel; }
void set_interlock(const std::vector<Switch *> &interlock);
void set_interlock_wait_time(uint32_t interlock_wait_time) { interlock_wait_time_ = interlock_wait_time; }
protected:
uint8_t channel_;
std::vector<Switch *> interlock_;
uint32_t interlock_wait_time_{0};
};
} // namespace seeedmultichannelrelay
} // namespace esphome

View File

@ -0,0 +1,22 @@
seeedmultichannelrelay:
id: SeeedMultiChannelRelay_ID
change_address_to: 0x01 # changes the address of the seeedmultichannelrelay (only runs once to update into the EPROM) then you have to change the i2c_address
switch:
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_1
name: seeedmultichannelrelay1
channel: 1
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_2
name: seeedmultichannelrelay2
channel: 2
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_8
name: seeedmultichannelrelay8
channel: 8
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID

View File

@ -0,0 +1,22 @@
seeedmultichannelrelay:
id: SeeedMultiChannelRelay_ID
change_address_to: 0x01 # changes the address of the seeedmultichannelrelay (only runs once to update into the EPROM) then you have to change the i2c_address
switch:
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_1
name: seeedmultichannelrelay1
channel: 1
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_2
name: seeedmultichannelrelay2
channel: 2
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_8
name: seeedmultichannelrelay8
channel: 8
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID

View File

@ -0,0 +1,22 @@
seeedmultichannelrelay:
id: SeeedMultiChannelRelay_ID
change_address_to: 0x01 # changes the address of the seeedmultichannelrelay (only runs once to update into the EPROM) then you have to change the i2c_address
switch:
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_1
name: seeedmultichannelrelay1
channel: 1
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_2
name: seeedmultichannelrelay2
channel: 2
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_8
name: seeedmultichannelrelay8
channel: 8
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID

View File

@ -0,0 +1,22 @@
seeedmultichannelrelay:
id: SeeedMultiChannelRelay_ID
change_address_to: 0x01 # changes the address of the seeedmultichannelrelay (only runs once to update into the EPROM) then you have to change the i2c_address
switch:
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_1
name: seeedmultichannelrelay1
channel: 1
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_2
name: seeedmultichannelrelay2
channel: 2
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_8
name: seeedmultichannelrelay8
channel: 8
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID

View File

@ -0,0 +1,22 @@
seeedmultichannelrelay:
id: SeeedMultiChannelRelay_ID
change_address_to: 0x01 # changes the address of the seeedmultichannelrelay (only runs once to update into the EPROM) then you have to change the i2c_address
switch:
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_1
name: seeedmultichannelrelay1
channel: 1
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_2
name: seeedmultichannelrelay2
channel: 2
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_8
name: seeedmultichannelrelay8
channel: 8
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID

View File

@ -0,0 +1,22 @@
seeedmultichannelrelay:
id: SeeedMultiChannelRelay_ID
change_address_to: 0x01 # changes the address of the seeedmultichannelrelay (only runs once to update into the EPROM) then you have to change the i2c_address
switch:
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_1
name: seeedmultichannelrelay1
channel: 1
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_2
name: seeedmultichannelrelay2
channel: 2
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_8
name: seeedmultichannelrelay8
channel: 8
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID

View File

@ -0,0 +1,22 @@
seeedmultichannelrelay:
id: SeeedMultiChannelRelay_ID
change_address_to: 0x01 # changes the address of the seeedmultichannelrelay (only runs once to update into the EPROM) then you have to change the i2c_address
switch:
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_1
name: seeedmultichannelrelay1
channel: 1
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_2
name: seeedmultichannelrelay2
channel: 2
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID
- platform: seeedmultichannelrelay
id: seeedmultichannelrelay_8
name: seeedmultichannelrelay8
channel: 8
seeedmultichannelrelay_id: SeeedMultiChannelRelay_ID