ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
seeed_multi_channel_relay.cpp
Go to the documentation of this file.
3#include "esphome/core/log.h"
4
6
7ESPHOME_LOG_TAG(TAG, "seeed_multi_channel_relay");
8
9static constexpr uint8_t CMD_CHANNEL_CTRL = 0x10;
10static constexpr uint8_t CMD_SAVE_I2C_ADDR = 0x11;
11static constexpr uint8_t CMD_READ_FIRMWARE_VER = 0x13;
12
14 if (!this->read_byte(CMD_READ_FIRMWARE_VER, &this->firmware_version_)) {
15 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
16 return;
17 }
18#ifdef USE_SWITCH
19 // channels_ owns all 8 relays and the board has no read-back: after an ESP-only reboot the board keeps its
20 // relays, so every channel is set from the switches' restore modes in one write, and a channel already on
21 // never drops out while the others are written; a power cycle starts with all off. A switch with restore_mode
22 // DISABLED is driven off too, so its state matches what the next write does
23 uint8_t value = 0;
24 for (uint8_t channel = 0; channel != this->switches_.size(); channel++) {
25 switch_::Switch *sw = this->switches_[channel];
26 if (sw == nullptr)
27 continue;
28 const bool on = sw->get_initial_write_state().value_or(false);
29 assign_bits(value, 1 << channel, on);
30 }
31 if (!this->status_comm_result(this->write_byte(CMD_CHANNEL_CTRL, value)))
32 return;
33 this->channels_ = value;
34 for (uint8_t channel = 0; channel != this->switches_.size(); channel++) {
35 PUBLISH_STATE(this->switches_[channel], (value >> channel) & 1);
36 }
37#endif
38}
39
41 if (this->is_failed())
42 return;
43 // A lambda bypasses the schema range; a reserved address would strand the board
45 ESP_LOGW(TAG, "I2C address 0x%02X is reserved; not stored", address);
46 return;
47 }
48 if (!this->status_comm_result(this->write_byte(CMD_SAVE_I2C_ADDR, address)))
49 return;
50 this->set_i2c_address(address);
51 ESP_LOGI(TAG, "Board now answers at 0x%02X; update address: in the configuration", address);
52}
53
55 ESP_LOGCONFIG(TAG,
56 "Seeed Multi Channel Relay:\n"
57 " Firmware version: %u",
58 this->firmware_version_);
59 LOG_I2C_DEVICE(this);
60}
61
62bool SeeedMultiChannelRelay::set_relay(uint8_t channel, bool on) {
63 if (this->is_failed())
64 return false;
65 uint8_t value = this->channels_;
66 assign_bits(value, 1 << channel, on);
67 if (!this->status_comm_result(this->write_byte(CMD_CHANNEL_CTRL, value)))
68 return false;
69 this->channels_ = value;
70#ifdef USE_SWITCH
71 PUBLISH_STATE(this->switches_[channel], on);
72#endif
73 return true;
74}
75
76} // namespace esphome::seeed_multi_channel_relay
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:151
void change_address(uint8_t address)
Store a new I2C address in the board's EEPROM and talk to it there from now on.
bool set_relay(uint8_t channel, bool on)
Switch relay channel (0-7) on or off; returns false when the write failed.
Base class for all switches.
Definition switch.h:43
optional< bool > get_initial_write_state()
get_initial_state_with_restore_mode() with inverted applied: the level write_state() takes.
Definition switch.cpp:63
ESPHOME_LOG_TAG(TAG, "seeed_multi_channel_relay")
uint32_t value
Definition hal.cpp:41
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1150