From 511df0f25407469538b5ab9128e9dd776f09faee Mon Sep 17 00:00:00 2001 From: Braven-agents Date: Tue, 1 Dec 2026 18:05:05 +0800 Subject: [PATCH 1/3] [m5_unit_roller485] Add the M5Stack Unit Roller485 Co-Authored-By: Claude Opus 5.5 --- CODEOWNERS | 1 + .../components/m5_unit_roller485/__init__.py | 26 ++ .../m5_unit_roller485/button/__init__.py | 30 ++ .../button/m5_unit_roller485_button.h | 14 + .../m5_unit_roller485/light/__init__.py | 27 ++ .../light/m5_unit_roller485_light.h | 30 ++ .../m5_unit_roller485/m5_unit_roller485.cpp | 266 ++++++++++++++++++ .../m5_unit_roller485/m5_unit_roller485.h | 131 +++++++++ .../m5_unit_roller485/number/__init__.py | 61 ++++ .../number/m5_unit_roller485_number.h | 23 ++ .../m5_unit_roller485/select/__init__.py | 29 ++ .../select/m5_unit_roller485_select.h | 15 + .../m5_unit_roller485/sensor/__init__.py | 82 ++++++ .../m5_unit_roller485/switch/__init__.py | 42 +++ .../switch/m5_unit_roller485_switch.h | 20 ++ .../m5_unit_roller485/text_sensor/__init__.py | 25 ++ .../components/m5_unit_roller485/common.yaml | 56 ++++ .../number/test_m5_unit_roller485_number.cpp | 45 +++ .../components/m5_unit_roller485/roller_bus.h | 40 +++ .../select/test_m5_unit_roller485_select.cpp | 41 +++ .../sensor/test_m5_unit_roller485_sensor.cpp | 36 +++ .../switch/test_m5_unit_roller485_switch.cpp | 88 ++++++ .../m5_unit_roller485/test.esp32-idf.yaml | 3 + .../test_m5_unit_roller485.cpp | 101 +++++++ .../test_m5_unit_roller485_text_sensor.cpp | 38 +++ 25 files changed, 1270 insertions(+) create mode 100644 esphome/components/m5_unit_roller485/__init__.py create mode 100644 esphome/components/m5_unit_roller485/button/__init__.py create mode 100644 esphome/components/m5_unit_roller485/button/m5_unit_roller485_button.h create mode 100644 esphome/components/m5_unit_roller485/light/__init__.py create mode 100644 esphome/components/m5_unit_roller485/light/m5_unit_roller485_light.h create mode 100644 esphome/components/m5_unit_roller485/m5_unit_roller485.cpp create mode 100644 esphome/components/m5_unit_roller485/m5_unit_roller485.h create mode 100644 esphome/components/m5_unit_roller485/number/__init__.py create mode 100644 esphome/components/m5_unit_roller485/number/m5_unit_roller485_number.h create mode 100644 esphome/components/m5_unit_roller485/select/__init__.py create mode 100644 esphome/components/m5_unit_roller485/select/m5_unit_roller485_select.h create mode 100644 esphome/components/m5_unit_roller485/sensor/__init__.py create mode 100644 esphome/components/m5_unit_roller485/switch/__init__.py create mode 100644 esphome/components/m5_unit_roller485/switch/m5_unit_roller485_switch.h create mode 100644 esphome/components/m5_unit_roller485/text_sensor/__init__.py create mode 100644 tests/components/m5_unit_roller485/common.yaml create mode 100644 tests/components/m5_unit_roller485/number/test_m5_unit_roller485_number.cpp create mode 100644 tests/components/m5_unit_roller485/roller_bus.h create mode 100644 tests/components/m5_unit_roller485/select/test_m5_unit_roller485_select.cpp create mode 100644 tests/components/m5_unit_roller485/sensor/test_m5_unit_roller485_sensor.cpp create mode 100644 tests/components/m5_unit_roller485/switch/test_m5_unit_roller485_switch.cpp create mode 100644 tests/components/m5_unit_roller485/test.esp32-idf.yaml create mode 100644 tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp create mode 100644 tests/components/m5_unit_roller485/text_sensor/test_m5_unit_roller485_text_sensor.cpp diff --git a/CODEOWNERS b/CODEOWNERS index ba6526c49586..3259d9f3f108 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -366,6 +366,7 @@ esphome/components/ltr501/* @latonita esphome/components/ltr_als_ps/* @latonita esphome/components/lvgl/* @clydebarrow esphome/components/m5_unit_acssr/* @m5stack +esphome/components/m5_unit_roller485/* @m5stack esphome/components/m5stack_8angle/* @rnauber esphome/components/mapping/* @clydebarrow esphome/components/matrix_keypad/* @ssieb diff --git a/esphome/components/m5_unit_roller485/__init__.py b/esphome/components/m5_unit_roller485/__init__.py new file mode 100644 index 000000000000..2544f3ab2d48 --- /dev/null +++ b/esphome/components/m5_unit_roller485/__init__.py @@ -0,0 +1,26 @@ +import esphome.codegen as cg +from esphome.components import i2c +import esphome.config_validation as cv +from esphome.types import ConfigType + +CODEOWNERS = ["@m5stack"] +DEPENDENCIES = ["i2c"] +DOMAIN = "m5_unit_roller485" +MULTI_CONF = True + +CONF_M5_UNIT_ROLLER485_ID = "m5_unit_roller485_id" + +m5_unit_roller485_ns = cg.esphome_ns.namespace("m5_unit_roller485") +M5UnitRoller485 = m5_unit_roller485_ns.class_( + "M5UnitRoller485", i2c.I2CDevice, cg.PollingComponent +) + +CONFIG_SCHEMA = ( + cv.Schema({cv.GenerateID(): cv.declare_id(M5UnitRoller485)}) + .extend(cv.polling_component_schema("1s")) + .extend(i2c.i2c_device_schema(0x64)) +) + + +async def to_code(config: ConfigType) -> None: + await i2c.new_i2c_component(config) diff --git a/esphome/components/m5_unit_roller485/button/__init__.py b/esphome/components/m5_unit_roller485/button/__init__.py new file mode 100644 index 000000000000..dc1ede867746 --- /dev/null +++ b/esphome/components/m5_unit_roller485/button/__init__.py @@ -0,0 +1,30 @@ +import esphome.codegen as cg +from esphome.components import button +import esphome.config_validation as cv +from esphome.types import ConfigType + +from .. import CONF_M5_UNIT_ROLLER485_ID, M5UnitRoller485, m5_unit_roller485_ns + +DEPENDENCIES = ["m5_unit_roller485"] + +CONF_RESET_STALL = "reset_stall" + +M5UnitRoller485ResetStallButton = m5_unit_roller485_ns.class_( + "M5UnitRoller485ResetStallButton", + button.Button, + cg.Parented.template(M5UnitRoller485), +) + +CONFIG_SCHEMA = { + cv.GenerateID(CONF_M5_UNIT_ROLLER485_ID): cv.use_id(M5UnitRoller485), + # Clears a stall, so the motor can run again + cv.Optional(CONF_RESET_STALL): button.button_schema( + M5UnitRoller485ResetStallButton, icon="mdi:restart-alert" + ), +} + + +async def to_code(config: ConfigType) -> None: + if (conf := config.get(CONF_RESET_STALL)) is not None: + var = await button.new_button(conf) + await cg.register_parented(var, config[CONF_M5_UNIT_ROLLER485_ID]) diff --git a/esphome/components/m5_unit_roller485/button/m5_unit_roller485_button.h b/esphome/components/m5_unit_roller485/button/m5_unit_roller485_button.h new file mode 100644 index 000000000000..01c21851299e --- /dev/null +++ b/esphome/components/m5_unit_roller485/button/m5_unit_roller485_button.h @@ -0,0 +1,14 @@ +#pragma once + +#include "esphome/components/button/button.h" +#include "esphome/components/m5_unit_roller485/m5_unit_roller485.h" +#include "esphome/core/helpers.h" + +namespace esphome::m5_unit_roller485 { + +class M5UnitRoller485ResetStallButton : public button::Button, public Parented { + protected: + void press_action() override { this->parent_->reset_stall(); } +}; + +} // namespace esphome::m5_unit_roller485 diff --git a/esphome/components/m5_unit_roller485/light/__init__.py b/esphome/components/m5_unit_roller485/light/__init__.py new file mode 100644 index 000000000000..9e95c97fd999 --- /dev/null +++ b/esphome/components/m5_unit_roller485/light/__init__.py @@ -0,0 +1,27 @@ +import esphome.codegen as cg +from esphome.components import light +import esphome.config_validation as cv +from esphome.const import CONF_OUTPUT_ID +from esphome.types import ConfigType + +from .. import CONF_M5_UNIT_ROLLER485_ID, M5UnitRoller485, m5_unit_roller485_ns + +DEPENDENCIES = ["m5_unit_roller485"] + +M5UnitRoller485Light = m5_unit_roller485_ns.class_( + "M5UnitRoller485Light", light.LightOutput, cg.Parented.template(M5UnitRoller485) +) + +# Without this light the LED shows the unit's own status colours +CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend( + { + cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(M5UnitRoller485Light), + cv.GenerateID(CONF_M5_UNIT_ROLLER485_ID): cv.use_id(M5UnitRoller485), + } +) + + +async def to_code(config: ConfigType) -> None: + var = cg.new_Pvariable(config[CONF_OUTPUT_ID]) + await light.register_light(var, config) + await cg.register_parented(var, config[CONF_M5_UNIT_ROLLER485_ID]) diff --git a/esphome/components/m5_unit_roller485/light/m5_unit_roller485_light.h b/esphome/components/m5_unit_roller485/light/m5_unit_roller485_light.h new file mode 100644 index 000000000000..a721cd0510ef --- /dev/null +++ b/esphome/components/m5_unit_roller485/light/m5_unit_roller485_light.h @@ -0,0 +1,30 @@ +#pragma once + +#include + +#include "esphome/components/light/light_output.h" +#include "esphome/components/light/light_state.h" +#include "esphome/components/m5_unit_roller485/m5_unit_roller485.h" +#include "esphome/core/helpers.h" + +namespace esphome::m5_unit_roller485 { + +class M5UnitRoller485Light : public light::LightOutput, public Parented { + public: + light::LightTraits get_traits() override { + auto traits = light::LightTraits(); + traits.set_supported_color_modes({light::ColorMode::RGB}); + return traits; + } + + void write_state(light::LightState *state) override { + float red, green, blue; + state->current_values_as_rgb(&red, &green, &blue); + this->parent_->write_led(to_byte_(red), to_byte_(green), to_byte_(blue)); + } + + protected: + static uint8_t to_byte_(float value) { return static_cast(lroundf(value * 255.0f)); } +}; + +} // namespace esphome::m5_unit_roller485 diff --git a/esphome/components/m5_unit_roller485/m5_unit_roller485.cpp b/esphome/components/m5_unit_roller485/m5_unit_roller485.cpp new file mode 100644 index 000000000000..146230c94fd9 --- /dev/null +++ b/esphome/components/m5_unit_roller485/m5_unit_roller485.cpp @@ -0,0 +1,266 @@ +#include "m5_unit_roller485.h" + +#include + +#include "esphome/core/helpers.h" +#include "esphome/core/log.h" +#include "esphome/core/progmem.h" + +namespace esphome::m5_unit_roller485 { + +ESPHOME_LOG_TAG(TAG, "m5_unit_roller485"); + +// Indexed by the status the hub works out from REG_SYSTEM_STATUS and REG_ERROR_CODE +PROGMEM_STRING_TABLE(StatusStrings, "Standby", "Running", "Over voltage", "Stalled", "Error"); +enum : uint8_t { STATUS_STANDBY, STATUS_RUNNING, STATUS_OVER_VOLTAGE, STATUS_STALLED, STATUS_ERROR }; + +// REG_OUTPUT to REG_STALL_PROTECTION, read in one transfer +static constexpr size_t SETTINGS_LEN = REG_STALL_PROTECTION + 1; +static constexpr float SCALE = 100.0f; + +void M5UnitRoller485::setup() { + if (!this->connect_()) + this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); +} + +bool M5UnitRoller485::connect_() { + if (!this->read_bytes(REG_FIRMWARE_VERSION, &this->firmware_version_, 1)) + return false; +#ifdef USE_SWITCH + // Only on the first connection: later the switches already hold what was last set + if (!this->connected_once_) { + if (this->motor_switch_ != nullptr) { + auto initial = this->motor_switch_->get_initial_state_with_restore_mode(); + if (initial.has_value() && !this->write_u8_(REG_OUTPUT, *initial != this->motor_switch_->is_inverted())) + return false; + } + if (this->stall_protection_switch_ != nullptr) { + auto initial = this->stall_protection_switch_->get_initial_state_with_restore_mode(); + if (initial.has_value() && + !this->write_u8_(REG_STALL_PROTECTION, *initial != this->stall_protection_switch_->is_inverted())) + return false; + } + } +#endif + this->led_mode_set_ = false; + if (this->led_set_ && !this->write_led_()) + return false; + // Publish every setting again, in case the unit changed while it was away + this->output_ = this->mode_ = this->stall_protection_ = this->status_ = 0xFF; + if (!this->read_settings_()) + return false; +#ifdef USE_NUMBER + for (auto &target : this->target_numbers_) { + int32_t value; + if (!this->read_i32_(target.reg, &value)) + return false; + target.number->publish_state(value / SCALE); + } +#endif + this->connected_ = true; + this->connected_once_ = true; + this->status_clear_warning(); + return true; +} + +void M5UnitRoller485::update() { + if (!this->connected_) { + this->connect_(); + return; + } + this->comm_result_(this->read_settings_() && this->read_readbacks_()); +} + +bool M5UnitRoller485::read_settings_() { + uint8_t data[SETTINGS_LEN]; + if (!this->read_bytes(REG_OUTPUT, data, sizeof(data))) + return false; + // The button on the unit switches mode, and a stall switches the motor off, so these change on their own +#ifdef USE_SWITCH + if (data[REG_OUTPUT] != this->output_ && this->motor_switch_ != nullptr) + this->motor_switch_->publish_state(data[REG_OUTPUT] != 0); + if (data[REG_STALL_PROTECTION] != this->stall_protection_ && this->stall_protection_switch_ != nullptr) + this->stall_protection_switch_->publish_state(data[REG_STALL_PROTECTION] != 0); +#endif +#ifdef USE_SELECT + const uint8_t mode = data[REG_MODE]; + if (mode != this->mode_ && this->mode_select_ != nullptr && mode >= 1 && mode <= this->mode_select_->size()) + this->mode_select_->publish_state(mode - 1); +#endif + this->output_ = data[REG_OUTPUT]; + this->mode_ = data[REG_MODE]; + this->stall_protection_ = data[REG_STALL_PROTECTION]; + + uint8_t status = STATUS_ERROR; + if (data[REG_SYSTEM_STATUS] == 0) { + status = STATUS_STANDBY; + } else if (data[REG_SYSTEM_STATUS] == 1) { + status = STATUS_RUNNING; + } else if (data[REG_ERROR_CODE] & 0x01) { + status = STATUS_OVER_VOLTAGE; + } else if (data[REG_ERROR_CODE] & 0x02) { + status = STATUS_STALLED; + } + if (status != this->status_) { + this->status_ = status; + ESP_LOGD(TAG, "Status %s (system status %u, error code %u)", + LOG_STR_ARG(StatusStrings::get_log_str(status, StatusStrings::LAST_INDEX)), data[REG_SYSTEM_STATUS], + data[REG_ERROR_CODE]); +#ifdef USE_TEXT_SENSOR + if (this->status_text_sensor_ != nullptr) { + char text[StatusStrings::MAX_LENGTH + 1]; + ESPHOME_strncpy_P( + text, reinterpret_cast(StatusStrings::get_progmem_str(status, StatusStrings::LAST_INDEX)), + sizeof(text)); + this->status_text_sensor_->publish_state(text); + } +#endif + } + return true; +} + +bool M5UnitRoller485::read_readbacks_() { +#ifdef USE_SENSOR + struct Readback { + sensor::Sensor *sensor; + uint8_t reg; + float scale; + }; + const Readback readbacks[] = { + {this->speed_sensor_, REG_SPEED_READBACK, SCALE}, {this->position_sensor_, REG_POSITION_READBACK, SCALE}, + {this->current_sensor_, REG_CURRENT_READBACK, SCALE}, {this->voltage_sensor_, REG_VOLTAGE, SCALE}, + {this->temperature_sensor_, REG_TEMPERATURE, 1.0f}, {this->encoder_sensor_, REG_ENCODER, 1.0f}, + }; + for (const auto &readback : readbacks) { + if (readback.sensor == nullptr) + continue; + int32_t value; + if (!this->read_i32_(readback.reg, &value)) + return false; + readback.sensor->publish_state(value / readback.scale); + } +#endif + return true; +} + +bool M5UnitRoller485::write_motor(bool on) { + if (!this->connected_ || !this->comm_result_(this->write_u8_(REG_OUTPUT, on))) + return false; + this->output_ = on; +#ifdef USE_SWITCH + if (this->motor_switch_ != nullptr) + this->motor_switch_->publish_state(on); +#endif + return true; +} + +bool M5UnitRoller485::write_stall_protection(bool on) { + if (!this->connected_ || !this->comm_result_(this->write_u8_(REG_STALL_PROTECTION, on))) + return false; + this->stall_protection_ = on; +#ifdef USE_SWITCH + if (this->stall_protection_switch_ != nullptr) + this->stall_protection_switch_->publish_state(on); +#endif + return true; +} + +bool M5UnitRoller485::write_mode(Mode mode) { + const auto value = static_cast(mode); + if (!this->connected_ || !this->comm_result_(this->write_u8_(REG_MODE, value))) + return false; + this->mode_ = value; +#ifdef USE_SELECT + if (this->mode_select_ != nullptr) + this->mode_select_->publish_state(value - 1); +#endif + return true; +} + +bool M5UnitRoller485::write_target(uint8_t reg, float value) { + return this->connected_ && this->comm_result_(this->write_i32_(reg, static_cast(lroundf(value * SCALE)))); +} + +bool M5UnitRoller485::write_led(uint8_t red, uint8_t green, uint8_t blue) { + // Held while the unit is away, then written when it answers again + this->led_[0] = red; + this->led_[1] = green; + this->led_[2] = blue; + this->led_set_ = true; + return this->connected_ && this->comm_result_(this->write_led_()); +} + +bool M5UnitRoller485::reset_stall() { + return this->connected_ && this->comm_result_(this->write_u8_(REG_STALL_RESET, 1)); +} + +#ifdef USE_NUMBER +void M5UnitRoller485::add_target_number(uint8_t reg, number::Number *number) { + this->target_numbers_.push_back({reg, number}); +} +#endif + +bool M5UnitRoller485::write_led_() { + // B, G, R, then 1 to show this colour instead of the unit's own status colours + const uint8_t rgb[4] = {this->led_[2], this->led_[1], this->led_[0], 1}; + if (this->led_mode_set_) + return this->write_bytes(REG_RGB, rgb, 3); + // The light scales the colour itself, so the unit's brightness stays at full + this->led_mode_set_ = this->write_u8_(REG_RGB_BRIGHTNESS, 100) && this->write_bytes(REG_RGB, rgb, sizeof(rgb)); + return this->led_mode_set_; +} + +bool M5UnitRoller485::write_u8_(uint8_t reg, uint8_t value) { return this->write_bytes(reg, &value, 1); } + +bool M5UnitRoller485::write_i32_(uint8_t reg, int32_t value) { + const auto raw = static_cast(value); + const uint8_t data[4] = {static_cast(raw), static_cast(raw >> 8), static_cast(raw >> 16), + static_cast(raw >> 24)}; + return this->write_bytes(reg, data, sizeof(data)); +} + +bool M5UnitRoller485::read_i32_(uint8_t reg, int32_t *value) { + uint8_t data[4]; + if (!this->read_bytes(reg, data, sizeof(data))) + return false; + *value = encode_value_le(data); + return true; +} + +bool M5UnitRoller485::comm_result_(bool ok) { + if (!ok) { + // update() reconnects and publishes the unit's settings again + this->connected_ = false; + this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); + } + return ok; +} + +void M5UnitRoller485::dump_config() { + ESP_LOGCONFIG(TAG, + "M5Stack Unit Roller485:\n" + " Firmware version: %u", + this->firmware_version_); + LOG_I2C_DEVICE(this); + LOG_UPDATE_INTERVAL(this); +#ifdef USE_SENSOR + LOG_SENSOR(" ", "Speed", this->speed_sensor_); + LOG_SENSOR(" ", "Position", this->position_sensor_); + LOG_SENSOR(" ", "Current", this->current_sensor_); + LOG_SENSOR(" ", "Voltage", this->voltage_sensor_); + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); + LOG_SENSOR(" ", "Encoder", this->encoder_sensor_); +#endif +#ifdef USE_TEXT_SENSOR + LOG_TEXT_SENSOR(" ", "Status", this->status_text_sensor_); +#endif +#ifdef USE_SWITCH + LOG_SWITCH(" ", "Motor", this->motor_switch_); + LOG_SWITCH(" ", "Stall protection", this->stall_protection_switch_); +#endif +#ifdef USE_SELECT + LOG_SELECT(" ", "Mode", this->mode_select_); +#endif +} + +} // namespace esphome::m5_unit_roller485 diff --git a/esphome/components/m5_unit_roller485/m5_unit_roller485.h b/esphome/components/m5_unit_roller485/m5_unit_roller485.h new file mode 100644 index 000000000000..dd5281cad292 --- /dev/null +++ b/esphome/components/m5_unit_roller485/m5_unit_roller485.h @@ -0,0 +1,131 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/defines.h" +#include "esphome/core/helpers.h" +#include "esphome/components/i2c/i2c.h" +#ifdef USE_SENSOR +#include "esphome/components/sensor/sensor.h" +#endif +#ifdef USE_TEXT_SENSOR +#include "esphome/components/text_sensor/text_sensor.h" +#endif +#ifdef USE_SWITCH +#include "esphome/components/switch/switch.h" +#endif +#ifdef USE_SELECT +#include "esphome/components/select/select.h" +#endif +#ifdef USE_NUMBER +#include "esphome/components/number/number.h" +#endif + +namespace esphome::m5_unit_roller485 { + +// I2C register map, from the M5Unit-Roller library (https://github.com/m5stack/M5Unit-Roller). +// Multi-byte values are little endian. Speeds are in 0.01 rpm, positions in 0.01 degree, currents in 0.01 mA. +enum : uint8_t { + REG_OUTPUT = 0x00, // 0 motor off, 1 on + REG_MODE = 0x01, // Mode + REG_STALL_RESET = 0x0B, // Write 1 to clear a stall + REG_SYSTEM_STATUS = 0x0C, // 0 standby, 1 running, 2 error + REG_ERROR_CODE = 0x0D, // Bit 0 over voltage, bit 1 stalled + REG_STALL_PROTECTION = 0x0F, // 0 off, 1 on + REG_RGB_BRIGHTNESS = 0x12, // 0 to 100 + REG_POSITION_MAX_CURRENT = 0x20, + REG_RGB = 0x30, // B, G, R, then the LED mode + REG_RGB_MODE = 0x33, + REG_VOLTAGE = 0x34, // 0.01 V + REG_TEMPERATURE = 0x38, + REG_ENCODER = 0x3C, + REG_SPEED = 0x40, + REG_SPEED_MAX_CURRENT = 0x50, + REG_SPEED_READBACK = 0x60, + REG_POSITION = 0x80, + REG_POSITION_READBACK = 0x90, + REG_CURRENT = 0xB0, + REG_CURRENT_READBACK = 0xC0, + REG_FIRMWARE_VERSION = 0xFE, +}; + +// What REG_MODE holds; the mode select lists them in this order, from 1 +enum class Mode : uint8_t { + MODE_SPEED = 1, + MODE_POSITION = 2, + MODE_CURRENT = 3, + MODE_ENCODER = 4, +}; + +/// Hub for one M5Stack Unit Roller485: a brushless motor with its own driver, driven over I2C. +/// Numbers set the targets of each mode, and the unit holds them while the motor is switched off. +class M5UnitRoller485 : public i2c::I2CDevice, public PollingComponent { + public: + void setup() override; + void update() override; + void dump_config() override; + +#ifdef USE_SENSOR + SUB_SENSOR(speed) + SUB_SENSOR(position) + SUB_SENSOR(current) + SUB_SENSOR(voltage) + SUB_SENSOR(temperature) + SUB_SENSOR(encoder) +#endif +#ifdef USE_TEXT_SENSOR + SUB_TEXT_SENSOR(status) +#endif +#ifdef USE_SWITCH + SUB_SWITCH(motor) + SUB_SWITCH(stall_protection) +#endif +#ifdef USE_SELECT + SUB_SELECT(mode) +#endif +#ifdef USE_NUMBER + /// Register a number that sets the 32-bit target at reg. + void add_target_number(uint8_t reg, number::Number *number); +#endif + + bool write_motor(bool on); + bool write_stall_protection(bool on); + bool write_mode(Mode mode); + /// Write a speed, position or current target, given in rpm, degrees or mA. + bool write_target(uint8_t reg, float value); + bool write_led(uint8_t red, uint8_t green, uint8_t blue); + bool reset_stall(); + + protected: + /// Check the unit answers, then publish its settings and apply the ones the hub holds. + bool connect_(); + bool read_settings_(); + bool read_readbacks_(); + bool write_u8_(uint8_t reg, uint8_t value); + bool write_i32_(uint8_t reg, int32_t value); + bool read_i32_(uint8_t reg, int32_t *value); + bool write_led_(); + bool comm_result_(bool ok); + +#ifdef USE_NUMBER + struct TargetNumber { + uint8_t reg; + number::Number *number; + }; + StaticVector target_numbers_; +#endif + + uint8_t led_[3]{}; // R, G, B + uint8_t firmware_version_{0}; + // Settings as last read, to publish only what changed; 0xFF before the first read + uint8_t output_{0xFF}; + uint8_t mode_{0xFF}; + uint8_t stall_protection_{0xFF}; + uint8_t status_{0xFF}; + bool connected_{false}; + bool connected_once_{false}; + bool led_set_{false}; + // The LED mode and brightness are written once per connection, then only the colour + bool led_mode_set_{false}; +}; + +} // namespace esphome::m5_unit_roller485 diff --git a/esphome/components/m5_unit_roller485/number/__init__.py b/esphome/components/m5_unit_roller485/number/__init__.py new file mode 100644 index 000000000000..e4b61a9e3904 --- /dev/null +++ b/esphome/components/m5_unit_roller485/number/__init__.py @@ -0,0 +1,61 @@ +import esphome.codegen as cg +from esphome.components import number +import esphome.config_validation as cv +from esphome.const import ( + CONF_CURRENT, + CONF_POSITION, + CONF_SPEED, + DEVICE_CLASS_CURRENT, + UNIT_DEGREES, + UNIT_MILLIAMP, + UNIT_REVOLUTIONS_PER_MINUTE, +) +from esphome.types import ConfigType + +from .. import CONF_M5_UNIT_ROLLER485_ID, M5UnitRoller485, m5_unit_roller485_ns + +DEPENDENCIES = ["m5_unit_roller485"] + +CONF_POSITION_MAX_CURRENT = "position_max_current" +CONF_SPEED_MAX_CURRENT = "speed_max_current" + +M5UnitRoller485Number = m5_unit_roller485_ns.class_( + "M5UnitRoller485Number", number.Number, cg.Parented.template(M5UnitRoller485) +) + +_SPEED = {"unit_of_measurement": UNIT_REVOLUTIONS_PER_MINUTE, "icon": "mdi:speedometer"} +_POSITION = {"unit_of_measurement": UNIT_DEGREES, "icon": "mdi:angle-acute"} +_CURRENT = {"unit_of_measurement": UNIT_MILLIAMP, "device_class": DEVICE_CLASS_CURRENT} + +# key: (register, schema arguments, min, max, step); the unit holds each value in hundredths +NUMBERS = { + CONF_SPEED: (0x40, _SPEED, -21000, 21000, 1), + CONF_SPEED_MAX_CURRENT: (0x50, _CURRENT, 0, 1200, 1), + CONF_POSITION: (0x80, _POSITION, -21474836, 21474836, 0.01), + CONF_POSITION_MAX_CURRENT: (0x20, _CURRENT, 0, 1200, 1), + CONF_CURRENT: (0xB0, _CURRENT, -1200, 1200, 1), +} + +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(CONF_M5_UNIT_ROLLER485_ID): cv.use_id(M5UnitRoller485), + **{ + cv.Optional(key): number.number_schema(M5UnitRoller485Number, **schema) + for key, (_, schema, _, _, _) in NUMBERS.items() + }, + } +) + + +async def to_code(config: ConfigType) -> None: + hub = await cg.get_variable(config[CONF_M5_UNIT_ROLLER485_ID]) + numbers = number.sub_numbers(config, parent=hub) + for key, (register, _, min_value, max_value, step) in NUMBERS.items(): + await numbers( + key, + lambda var, register=register: hub.add_target_number(register, var), + register, + min_value=min_value, + max_value=max_value, + step=step, + ) diff --git a/esphome/components/m5_unit_roller485/number/m5_unit_roller485_number.h b/esphome/components/m5_unit_roller485/number/m5_unit_roller485_number.h new file mode 100644 index 000000000000..5a27e166184f --- /dev/null +++ b/esphome/components/m5_unit_roller485/number/m5_unit_roller485_number.h @@ -0,0 +1,23 @@ +#pragma once + +#include "esphome/components/m5_unit_roller485/m5_unit_roller485.h" +#include "esphome/components/number/number.h" +#include "esphome/core/helpers.h" + +namespace esphome::m5_unit_roller485 { + +/// A speed, position or current target, or a mode's current limit. +class M5UnitRoller485Number : public number::Number, public Parented { + public: + explicit M5UnitRoller485Number(uint8_t reg) : reg_(reg) {} + + protected: + void control(float value) override { + if (this->parent_->write_target(this->reg_, value)) + this->publish_state(value); + } + + const uint8_t reg_; +}; + +} // namespace esphome::m5_unit_roller485 diff --git a/esphome/components/m5_unit_roller485/select/__init__.py b/esphome/components/m5_unit_roller485/select/__init__.py new file mode 100644 index 000000000000..4bc0f989b58f --- /dev/null +++ b/esphome/components/m5_unit_roller485/select/__init__.py @@ -0,0 +1,29 @@ +import esphome.codegen as cg +from esphome.components import select +import esphome.config_validation as cv +from esphome.const import CONF_MODE +from esphome.types import ConfigType + +from .. import CONF_M5_UNIT_ROLLER485_ID, M5UnitRoller485, m5_unit_roller485_ns + +DEPENDENCIES = ["m5_unit_roller485"] + +M5UnitRoller485ModeSelect = m5_unit_roller485_ns.class_( + "M5UnitRoller485ModeSelect", select.Select, cg.Parented.template(M5UnitRoller485) +) + +# In the order of the unit's mode numbers, from 1 +MODES = ["Speed", "Position", "Current", "Encoder"] + +CONFIG_SCHEMA = { + cv.GenerateID(CONF_M5_UNIT_ROLLER485_ID): cv.use_id(M5UnitRoller485), + cv.Optional(CONF_MODE): select.select_schema( + M5UnitRoller485ModeSelect, icon="mdi:cog" + ), +} + + +async def to_code(config: ConfigType) -> None: + hub = await cg.get_variable(config[CONF_M5_UNIT_ROLLER485_ID]) + selects = select.sub_selects(config, parent=hub) + await selects(CONF_MODE, hub.set_mode_select, options=MODES) diff --git a/esphome/components/m5_unit_roller485/select/m5_unit_roller485_select.h b/esphome/components/m5_unit_roller485/select/m5_unit_roller485_select.h new file mode 100644 index 000000000000..bfb0a86e135f --- /dev/null +++ b/esphome/components/m5_unit_roller485/select/m5_unit_roller485_select.h @@ -0,0 +1,15 @@ +#pragma once + +#include "esphome/components/m5_unit_roller485/m5_unit_roller485.h" +#include "esphome/components/select/select.h" +#include "esphome/core/helpers.h" + +namespace esphome::m5_unit_roller485 { + +class M5UnitRoller485ModeSelect : public select::Select, public Parented { + protected: + // The hub publishes the mode once the unit has it + void control(size_t index) override { this->parent_->write_mode(static_cast(index + 1)); } +}; + +} // namespace esphome::m5_unit_roller485 diff --git a/esphome/components/m5_unit_roller485/sensor/__init__.py b/esphome/components/m5_unit_roller485/sensor/__init__.py new file mode 100644 index 000000000000..667503471fc8 --- /dev/null +++ b/esphome/components/m5_unit_roller485/sensor/__init__.py @@ -0,0 +1,82 @@ +import esphome.codegen as cg +from esphome.components import sensor +import esphome.config_validation as cv +from esphome.const import ( + CONF_CURRENT, + CONF_POSITION, + CONF_SPEED, + CONF_TEMPERATURE, + CONF_VOLTAGE, + DEVICE_CLASS_CURRENT, + DEVICE_CLASS_TEMPERATURE, + DEVICE_CLASS_VOLTAGE, + ENTITY_CATEGORY_DIAGNOSTIC, + STATE_CLASS_MEASUREMENT, + UNIT_CELSIUS, + UNIT_DEGREES, + UNIT_MILLIAMP, + UNIT_REVOLUTIONS_PER_MINUTE, + UNIT_VOLT, +) +from esphome.types import ConfigType + +from .. import CONF_M5_UNIT_ROLLER485_ID, M5UnitRoller485 + +DEPENDENCIES = ["m5_unit_roller485"] + +CONF_ENCODER = "encoder" + +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(CONF_M5_UNIT_ROLLER485_ID): cv.use_id(M5UnitRoller485), + cv.Optional(CONF_SPEED): sensor.sensor_schema( + unit_of_measurement=UNIT_REVOLUTIONS_PER_MINUTE, + icon="mdi:rotate-right", + accuracy_decimals=0, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_POSITION): sensor.sensor_schema( + unit_of_measurement=UNIT_DEGREES, + icon="mdi:angle-acute", + accuracy_decimals=1, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_CURRENT): sensor.sensor_schema( + unit_of_measurement=UNIT_MILLIAMP, + device_class=DEVICE_CLASS_CURRENT, + accuracy_decimals=0, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_VOLTAGE): sensor.sensor_schema( + unit_of_measurement=UNIT_VOLT, + device_class=DEVICE_CLASS_VOLTAGE, + accuracy_decimals=2, + state_class=STATE_CLASS_MEASUREMENT, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), + cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( + unit_of_measurement=UNIT_CELSIUS, + device_class=DEVICE_CLASS_TEMPERATURE, + accuracy_decimals=0, + state_class=STATE_CLASS_MEASUREMENT, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), + # The dial count, which follows the shaft in encoder mode + cv.Optional(CONF_ENCODER): sensor.sensor_schema( + icon="mdi:counter", + accuracy_decimals=0, + state_class=STATE_CLASS_MEASUREMENT, + ), + } +) + + +async def to_code(config: ConfigType) -> None: + hub = await cg.get_variable(config[CONF_M5_UNIT_ROLLER485_ID]) + sensors = sensor.sub_sensors(config) + await sensors(CONF_SPEED, hub.set_speed_sensor) + await sensors(CONF_POSITION, hub.set_position_sensor) + await sensors(CONF_CURRENT, hub.set_current_sensor) + await sensors(CONF_VOLTAGE, hub.set_voltage_sensor) + await sensors(CONF_TEMPERATURE, hub.set_temperature_sensor) + await sensors(CONF_ENCODER, hub.set_encoder_sensor) diff --git a/esphome/components/m5_unit_roller485/switch/__init__.py b/esphome/components/m5_unit_roller485/switch/__init__.py new file mode 100644 index 000000000000..3541440f6b2a --- /dev/null +++ b/esphome/components/m5_unit_roller485/switch/__init__.py @@ -0,0 +1,42 @@ +import esphome.codegen as cg +from esphome.components import switch +import esphome.config_validation as cv +from esphome.const import ENTITY_CATEGORY_CONFIG +from esphome.types import ConfigType + +from .. import CONF_M5_UNIT_ROLLER485_ID, M5UnitRoller485, m5_unit_roller485_ns + +DEPENDENCIES = ["m5_unit_roller485"] + +CONF_MOTOR = "motor" +CONF_STALL_PROTECTION = "stall_protection" + +M5UnitRoller485MotorSwitch = m5_unit_roller485_ns.class_( + "M5UnitRoller485MotorSwitch", switch.Switch, cg.Parented.template(M5UnitRoller485) +) +M5UnitRoller485StallProtectionSwitch = m5_unit_roller485_ns.class_( + "M5UnitRoller485StallProtectionSwitch", + switch.Switch, + cg.Parented.template(M5UnitRoller485), +) + +CONFIG_SCHEMA = { + cv.GenerateID(CONF_M5_UNIT_ROLLER485_ID): cv.use_id(M5UnitRoller485), + # Off at boot unless restore_mode says otherwise; DISABLED keeps the unit's own state + cv.Optional(CONF_MOTOR): switch.switch_schema( + M5UnitRoller485MotorSwitch, icon="mdi:engine" + ), + cv.Optional(CONF_STALL_PROTECTION): switch.switch_schema( + M5UnitRoller485StallProtectionSwitch, + icon="mdi:shield-alert", + entity_category=ENTITY_CATEGORY_CONFIG, + default_restore_mode="DISABLED", + ), +} + + +async def to_code(config: ConfigType) -> None: + hub = await cg.get_variable(config[CONF_M5_UNIT_ROLLER485_ID]) + switches = switch.sub_switches(config, parent=hub) + await switches(CONF_MOTOR, hub.set_motor_switch) + await switches(CONF_STALL_PROTECTION, hub.set_stall_protection_switch) diff --git a/esphome/components/m5_unit_roller485/switch/m5_unit_roller485_switch.h b/esphome/components/m5_unit_roller485/switch/m5_unit_roller485_switch.h new file mode 100644 index 000000000000..b4beec197dcd --- /dev/null +++ b/esphome/components/m5_unit_roller485/switch/m5_unit_roller485_switch.h @@ -0,0 +1,20 @@ +#pragma once + +#include "esphome/components/m5_unit_roller485/m5_unit_roller485.h" +#include "esphome/components/switch/switch.h" +#include "esphome/core/helpers.h" + +namespace esphome::m5_unit_roller485 { + +// The hub publishes the state once the unit has it +class M5UnitRoller485MotorSwitch : public switch_::Switch, public Parented { + protected: + void write_state(bool state) override { this->parent_->write_motor(state); } +}; + +class M5UnitRoller485StallProtectionSwitch : public switch_::Switch, public Parented { + protected: + void write_state(bool state) override { this->parent_->write_stall_protection(state); } +}; + +} // namespace esphome::m5_unit_roller485 diff --git a/esphome/components/m5_unit_roller485/text_sensor/__init__.py b/esphome/components/m5_unit_roller485/text_sensor/__init__.py new file mode 100644 index 000000000000..3ec5fcbd821e --- /dev/null +++ b/esphome/components/m5_unit_roller485/text_sensor/__init__.py @@ -0,0 +1,25 @@ +import esphome.codegen as cg +from esphome.components import text_sensor +import esphome.config_validation as cv +from esphome.const import CONF_STATUS, ENTITY_CATEGORY_DIAGNOSTIC +from esphome.types import ConfigType + +from .. import CONF_M5_UNIT_ROLLER485_ID, M5UnitRoller485 + +DEPENDENCIES = ["m5_unit_roller485"] + +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(CONF_M5_UNIT_ROLLER485_ID): cv.use_id(M5UnitRoller485), + # Standby, Running, Over voltage, Stalled or Error + cv.Optional(CONF_STATUS): text_sensor.text_sensor_schema( + icon="mdi:engine", entity_category=ENTITY_CATEGORY_DIAGNOSTIC + ), + } +) + + +async def to_code(config: ConfigType) -> None: + hub = await cg.get_variable(config[CONF_M5_UNIT_ROLLER485_ID]) + text_sensors = text_sensor.sub_text_sensors(config) + await text_sensors(CONF_STATUS, hub.set_status_text_sensor) diff --git a/tests/components/m5_unit_roller485/common.yaml b/tests/components/m5_unit_roller485/common.yaml new file mode 100644 index 000000000000..fe5b34858a86 --- /dev/null +++ b/tests/components/m5_unit_roller485/common.yaml @@ -0,0 +1,56 @@ +m5_unit_roller485: + id: roller + +sensor: + - platform: m5_unit_roller485 + speed: + name: Roller speed + position: + name: Roller position + current: + name: Roller current + voltage: + name: Roller voltage + temperature: + name: Roller temperature + encoder: + name: Roller encoder + +text_sensor: + - platform: m5_unit_roller485 + status: + name: Roller status + +switch: + - platform: m5_unit_roller485 + motor: + name: Roller motor + stall_protection: + name: Roller stall protection + +select: + - platform: m5_unit_roller485 + mode: + name: Roller mode + +number: + - platform: m5_unit_roller485 + speed: + name: Roller target speed + speed_max_current: + name: Roller speed max current + position: + name: Roller target position + position_max_current: + name: Roller position max current + current: + name: Roller target current + +button: + - platform: m5_unit_roller485 + reset_stall: + name: Roller reset stall + +light: + - platform: m5_unit_roller485 + name: Roller LED diff --git a/tests/components/m5_unit_roller485/number/test_m5_unit_roller485_number.cpp b/tests/components/m5_unit_roller485/number/test_m5_unit_roller485_number.cpp new file mode 100644 index 000000000000..6ef503055f7a --- /dev/null +++ b/tests/components/m5_unit_roller485/number/test_m5_unit_roller485_number.cpp @@ -0,0 +1,45 @@ +#include + +#include "esphome/components/m5_unit_roller485/number/m5_unit_roller485_number.h" +#include "../roller_bus.h" + +namespace esphome::m5_unit_roller485::testing { + +class M5UnitRoller485NumberTest : public ::testing::Test { + protected: + void SetUp() override { + this->hub_.set_i2c_bus(&this->bus_); + this->hub_.set_i2c_address(0x64); + this->speed_.traits.set_min_value(-21000); + this->speed_.traits.set_max_value(21000); + this->speed_.traits.set_step(1); + this->speed_.set_parent(&this->hub_); + this->hub_.add_target_number(REG_SPEED, &this->speed_); + } + + RollerBus bus_; + M5UnitRoller485 hub_; + M5UnitRoller485Number speed_{REG_SPEED}; +}; + +TEST_F(M5UnitRoller485NumberTest, NumberShowsTheUnitsTarget) { + this->bus_.set_i32(REG_SPEED, -150000); + this->hub_.setup(); + EXPECT_FLOAT_EQ(this->speed_.state, -1500.0f); +} + +TEST_F(M5UnitRoller485NumberTest, NumberSetsTheTarget) { + this->hub_.setup(); + this->speed_.make_call().set_value(1200).perform(); + EXPECT_EQ(this->bus_.get_i32(REG_SPEED), 120000); + EXPECT_FLOAT_EQ(this->speed_.state, 1200.0f); +} + +TEST_F(M5UnitRoller485NumberTest, FailedWriteKeepsTheState) { + this->hub_.setup(); + this->bus_.absent = true; + this->speed_.make_call().set_value(1200).perform(); + EXPECT_FLOAT_EQ(this->speed_.state, 0.0f); +} + +} // namespace esphome::m5_unit_roller485::testing diff --git a/tests/components/m5_unit_roller485/roller_bus.h b/tests/components/m5_unit_roller485/roller_bus.h new file mode 100644 index 000000000000..4549d4083d7e --- /dev/null +++ b/tests/components/m5_unit_roller485/roller_bus.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#include +#include "esphome/components/i2c/i2c_bus.h" + +namespace esphome::m5_unit_roller485::testing { + +// The unit's STM32 target: the first byte of a write selects a register and the rest write from there, and a read +// returns bytes from the selected register on. +class RollerBus final : public i2c::I2CBus { + public: + std::array regs{}; + bool absent{false}; + + i2c::ErrorCode write_readv(uint8_t /*address*/, const uint8_t *out, size_t out_len, uint8_t *in, + size_t in_len) override { + if (this->absent) + return i2c::ERROR_NOT_ACKNOWLEDGED; + if (out_len != 0) { + this->selected_ = out[0]; + std::copy(out + 1, out + out_len, this->regs.begin() + out[0]); + } + std::copy_n(this->regs.begin() + this->selected_, in_len, in); + return i2c::ERROR_OK; + } + + void set_i32(uint8_t reg, int32_t value) { std::memcpy(&this->regs[reg], &value, sizeof(value)); } + int32_t get_i32(uint8_t reg) const { + int32_t value; + std::memcpy(&value, &this->regs[reg], sizeof(value)); + return value; + } + + protected: + uint8_t selected_{0}; +}; + +} // namespace esphome::m5_unit_roller485::testing diff --git a/tests/components/m5_unit_roller485/select/test_m5_unit_roller485_select.cpp b/tests/components/m5_unit_roller485/select/test_m5_unit_roller485_select.cpp new file mode 100644 index 000000000000..09c6be7333cd --- /dev/null +++ b/tests/components/m5_unit_roller485/select/test_m5_unit_roller485_select.cpp @@ -0,0 +1,41 @@ +#include + +#include "esphome/components/m5_unit_roller485/select/m5_unit_roller485_select.h" +#include "../roller_bus.h" + +namespace esphome::m5_unit_roller485::testing { + +class M5UnitRoller485SelectTest : public ::testing::Test { + protected: + void SetUp() override { + this->hub_.set_i2c_bus(&this->bus_); + this->hub_.set_i2c_address(0x64); + this->mode_.traits.set_options({"Speed", "Position", "Current", "Encoder"}); + this->mode_.set_parent(&this->hub_); + this->hub_.set_mode_select(&this->mode_); + } + + RollerBus bus_; + M5UnitRoller485 hub_; + M5UnitRoller485ModeSelect mode_; +}; + +TEST_F(M5UnitRoller485SelectTest, SelectShowsTheUnitsMode) { + this->bus_.regs[REG_MODE] = 3; + this->hub_.setup(); + EXPECT_EQ(this->mode_.current_option(), "Current"); + // The button on the unit changes the mode too + this->bus_.regs[REG_MODE] = 4; + this->hub_.update(); + EXPECT_EQ(this->mode_.current_option(), "Encoder"); +} + +TEST_F(M5UnitRoller485SelectTest, SelectSetsTheMode) { + this->bus_.regs[REG_MODE] = 1; + this->hub_.setup(); + this->mode_.make_call().set_option("Position").perform(); + EXPECT_EQ(this->bus_.regs[REG_MODE], 2); + EXPECT_EQ(this->mode_.current_option(), "Position"); +} + +} // namespace esphome::m5_unit_roller485::testing diff --git a/tests/components/m5_unit_roller485/sensor/test_m5_unit_roller485_sensor.cpp b/tests/components/m5_unit_roller485/sensor/test_m5_unit_roller485_sensor.cpp new file mode 100644 index 000000000000..729a23918dc9 --- /dev/null +++ b/tests/components/m5_unit_roller485/sensor/test_m5_unit_roller485_sensor.cpp @@ -0,0 +1,36 @@ +#include + +#include "esphome/components/m5_unit_roller485/m5_unit_roller485.h" +#include "../roller_bus.h" + +namespace esphome::m5_unit_roller485::testing { + +TEST(M5UnitRoller485SensorTest, ReadbacksAreScaled) { + RollerBus bus; + M5UnitRoller485 hub; + sensor::Sensor speed, position, current, voltage, temperature, encoder; + hub.set_i2c_bus(&bus); + hub.set_i2c_address(0x64); + hub.set_speed_sensor(&speed); + hub.set_position_sensor(&position); + hub.set_current_sensor(¤t); + hub.set_voltage_sensor(&voltage); + hub.set_temperature_sensor(&temperature); + hub.set_encoder_sensor(&encoder); + bus.set_i32(REG_SPEED_READBACK, -240000); + bus.set_i32(REG_POSITION_READBACK, 36050); + bus.set_i32(REG_CURRENT_READBACK, 12345); + bus.set_i32(REG_VOLTAGE, 1203); + bus.set_i32(REG_TEMPERATURE, 31); + bus.set_i32(REG_ENCODER, -7); + hub.setup(); + hub.update(); + EXPECT_FLOAT_EQ(speed.state, -2400.0f); + EXPECT_FLOAT_EQ(position.state, 360.5f); + EXPECT_FLOAT_EQ(current.state, 123.45f); + EXPECT_FLOAT_EQ(voltage.state, 12.03f); + EXPECT_FLOAT_EQ(temperature.state, 31.0f); + EXPECT_FLOAT_EQ(encoder.state, -7.0f); +} + +} // namespace esphome::m5_unit_roller485::testing diff --git a/tests/components/m5_unit_roller485/switch/test_m5_unit_roller485_switch.cpp b/tests/components/m5_unit_roller485/switch/test_m5_unit_roller485_switch.cpp new file mode 100644 index 000000000000..5321a6223190 --- /dev/null +++ b/tests/components/m5_unit_roller485/switch/test_m5_unit_roller485_switch.cpp @@ -0,0 +1,88 @@ +#include + +#include "esphome/components/m5_unit_roller485/switch/m5_unit_roller485_switch.h" +#include "../roller_bus.h" + +namespace esphome::m5_unit_roller485::testing { + +class M5UnitRoller485SwitchTest : public ::testing::Test { + protected: + void SetUp() override { + this->hub_.set_i2c_bus(&this->bus_); + this->hub_.set_i2c_address(0x64); + this->motor_.set_parent(&this->hub_); + this->hub_.set_motor_switch(&this->motor_); + this->stall_.set_parent(&this->hub_); + this->stall_.set_restore_mode(switch_::SWITCH_RESTORE_DISABLED); + this->hub_.set_stall_protection_switch(&this->stall_); + } + + RollerBus bus_; + M5UnitRoller485 hub_; + M5UnitRoller485MotorSwitch motor_; + M5UnitRoller485StallProtectionSwitch stall_; +}; + +// A running motor is switched off when the board starts, unless restore_mode says otherwise +TEST_F(M5UnitRoller485SwitchTest, RestoreModeSetsTheMotorAtBoot) { + this->bus_.regs[REG_OUTPUT] = 1; + this->motor_.set_restore_mode(switch_::SWITCH_ALWAYS_OFF); + this->hub_.setup(); + EXPECT_EQ(this->bus_.regs[REG_OUTPUT], 0); + EXPECT_FALSE(this->motor_.state); +} + +TEST_F(M5UnitRoller485SwitchTest, DisabledRestoreKeepsTheUnitsState) { + this->bus_.regs[REG_OUTPUT] = 1; + this->bus_.regs[REG_STALL_PROTECTION] = 1; + this->motor_.set_restore_mode(switch_::SWITCH_RESTORE_DISABLED); + this->hub_.setup(); + EXPECT_EQ(this->bus_.regs[REG_OUTPUT], 1); + EXPECT_TRUE(this->motor_.state); + EXPECT_TRUE(this->stall_.state); +} + +// The restore state applies at boot only, not when the unit comes back after a dropout +TEST_F(M5UnitRoller485SwitchTest, ReconnectKeepsTheMotorAsItIs) { + this->motor_.set_restore_mode(switch_::SWITCH_ALWAYS_OFF); + this->hub_.setup(); + this->motor_.turn_on(); + this->bus_.absent = true; + this->hub_.update(); + this->bus_.absent = false; + this->hub_.update(); + EXPECT_EQ(this->bus_.regs[REG_OUTPUT], 1); + EXPECT_TRUE(this->motor_.state); +} + +TEST_F(M5UnitRoller485SwitchTest, SwitchesDriveTheUnit) { + this->motor_.set_restore_mode(switch_::SWITCH_ALWAYS_OFF); + this->hub_.setup(); + this->motor_.turn_on(); + EXPECT_EQ(this->bus_.regs[REG_OUTPUT], 1); + EXPECT_TRUE(this->motor_.state); + this->stall_.turn_on(); + EXPECT_EQ(this->bus_.regs[REG_STALL_PROTECTION], 1); + EXPECT_TRUE(this->stall_.state); +} + +// A stall switches the motor off on the unit; the switch follows at the next update +TEST_F(M5UnitRoller485SwitchTest, MotorSwitchFollowsTheUnit) { + this->motor_.set_restore_mode(switch_::SWITCH_ALWAYS_OFF); + this->hub_.setup(); + this->motor_.turn_on(); + this->bus_.regs[REG_OUTPUT] = 0; + this->hub_.update(); + EXPECT_FALSE(this->motor_.state); +} + +TEST_F(M5UnitRoller485SwitchTest, FailedWriteKeepsTheState) { + this->motor_.set_restore_mode(switch_::SWITCH_ALWAYS_OFF); + this->hub_.setup(); + this->bus_.absent = true; + this->motor_.turn_on(); + EXPECT_FALSE(this->motor_.state); + EXPECT_TRUE(this->hub_.status_has_warning()); +} + +} // namespace esphome::m5_unit_roller485::testing diff --git a/tests/components/m5_unit_roller485/test.esp32-idf.yaml b/tests/components/m5_unit_roller485/test.esp32-idf.yaml new file mode 100644 index 000000000000..3159f78a8886 --- /dev/null +++ b/tests/components/m5_unit_roller485/test.esp32-idf.yaml @@ -0,0 +1,3 @@ +packages: + i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml + m5_unit_roller485: !include common.yaml diff --git a/tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp b/tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp new file mode 100644 index 000000000000..0527e629fd3c --- /dev/null +++ b/tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp @@ -0,0 +1,101 @@ +#include + +#include "esphome/components/m5_unit_roller485/m5_unit_roller485.h" +#include "roller_bus.h" + +namespace esphome::m5_unit_roller485::testing { + +class M5UnitRoller485Test : public ::testing::Test { + protected: + void SetUp() override { + this->bus_.regs[REG_FIRMWARE_VERSION] = 3; + this->hub_.set_i2c_bus(&this->bus_); + this->hub_.set_i2c_address(0x64); + } + + RollerBus bus_; + M5UnitRoller485 hub_; +}; + +TEST_F(M5UnitRoller485Test, MissingUnitWarnsWithoutFailing) { + this->bus_.absent = true; + this->hub_.setup(); + EXPECT_FALSE(this->hub_.is_failed()); + EXPECT_TRUE(this->hub_.status_has_warning()); + EXPECT_FALSE(this->hub_.write_motor(true)); +} + +TEST_F(M5UnitRoller485Test, UpdateConnectsOnceTheUnitAnswers) { + this->bus_.absent = true; + this->hub_.setup(); + this->bus_.absent = false; + this->hub_.update(); + EXPECT_FALSE(this->hub_.status_has_warning()); + EXPECT_TRUE(this->hub_.write_motor(true)); + EXPECT_EQ(this->bus_.regs[REG_OUTPUT], 1); +} + +TEST_F(M5UnitRoller485Test, LostUnitWarnsAndReconnects) { + this->hub_.setup(); + this->bus_.absent = true; + this->hub_.update(); + EXPECT_TRUE(this->hub_.status_has_warning()); + this->bus_.absent = false; + this->hub_.update(); + EXPECT_FALSE(this->hub_.status_has_warning()); +} + +// The unit holds targets in hundredths, little endian +TEST_F(M5UnitRoller485Test, TargetsAreWrittenInHundredths) { + this->hub_.setup(); + EXPECT_TRUE(this->hub_.write_target(REG_SPEED, 2400.0f)); + EXPECT_EQ(this->bus_.get_i32(REG_SPEED), 240000); + EXPECT_TRUE(this->hub_.write_target(REG_POSITION, -90.25f)); + EXPECT_EQ(this->bus_.get_i32(REG_POSITION), -9025); +} + +TEST_F(M5UnitRoller485Test, ModeIsWritten) { + this->hub_.setup(); + EXPECT_TRUE(this->hub_.write_mode(Mode::MODE_POSITION)); + EXPECT_EQ(this->bus_.regs[REG_MODE], 2); +} + +TEST_F(M5UnitRoller485Test, ResetStallWritesOne) { + this->hub_.setup(); + EXPECT_TRUE(this->hub_.reset_stall()); + EXPECT_EQ(this->bus_.regs[REG_STALL_RESET], 1); +} + +// A colour set while the unit is away is written, in B, G, R order and in user mode, once it answers +TEST_F(M5UnitRoller485Test, LedSetMeanwhileIsWrittenOnConnect) { + this->bus_.absent = true; + this->hub_.setup(); + EXPECT_FALSE(this->hub_.write_led(255, 128, 0)); + this->bus_.absent = false; + this->hub_.update(); + EXPECT_EQ(this->bus_.regs[REG_RGB], 0); + EXPECT_EQ(this->bus_.regs[REG_RGB + 1], 128); + EXPECT_EQ(this->bus_.regs[REG_RGB + 2], 255); + EXPECT_EQ(this->bus_.regs[REG_RGB_MODE], 1); + EXPECT_EQ(this->bus_.regs[REG_RGB_BRIGHTNESS], 100); +} + +// The unit keeps the mode and brightness, so later colours only write the colour +TEST_F(M5UnitRoller485Test, LaterColoursOnlyWriteTheColour) { + this->hub_.setup(); + this->hub_.write_led(1, 2, 3); + this->bus_.regs[REG_RGB_MODE] = 0; + this->hub_.write_led(4, 5, 6); + EXPECT_EQ(this->bus_.regs[REG_RGB], 6); + EXPECT_EQ(this->bus_.regs[REG_RGB + 2], 4); + EXPECT_EQ(this->bus_.regs[REG_RGB_MODE], 0); +} + +// Without a light the LED keeps the unit's own status colours +TEST_F(M5UnitRoller485Test, LedIsLeftAloneWithoutALight) { + this->hub_.setup(); + this->hub_.update(); + EXPECT_EQ(this->bus_.regs[REG_RGB_MODE], 0); +} + +} // namespace esphome::m5_unit_roller485::testing diff --git a/tests/components/m5_unit_roller485/text_sensor/test_m5_unit_roller485_text_sensor.cpp b/tests/components/m5_unit_roller485/text_sensor/test_m5_unit_roller485_text_sensor.cpp new file mode 100644 index 000000000000..cc391ee03d1d --- /dev/null +++ b/tests/components/m5_unit_roller485/text_sensor/test_m5_unit_roller485_text_sensor.cpp @@ -0,0 +1,38 @@ +#include + +#include "esphome/components/m5_unit_roller485/m5_unit_roller485.h" +#include "../roller_bus.h" + +namespace esphome::m5_unit_roller485::testing { + +class M5UnitRoller485StatusTest : public ::testing::Test { + protected: + void SetUp() override { + this->hub_.set_i2c_bus(&this->bus_); + this->hub_.set_i2c_address(0x64); + this->hub_.set_status_text_sensor(&this->status_); + } + + std::string status_after_(uint8_t system_status, uint8_t error_code) { + this->bus_.regs[REG_SYSTEM_STATUS] = system_status; + this->bus_.regs[REG_ERROR_CODE] = error_code; + this->hub_.update(); + return this->status_.state; + } + + RollerBus bus_; + M5UnitRoller485 hub_; + text_sensor::TextSensor status_; +}; + +TEST_F(M5UnitRoller485StatusTest, StatusNamesTheError) { + this->hub_.setup(); + EXPECT_EQ(this->status_.state, "Standby"); + EXPECT_EQ(this->status_after_(1, 0), "Running"); + EXPECT_EQ(this->status_after_(2, 1), "Over voltage"); + EXPECT_EQ(this->status_after_(2, 2), "Stalled"); + EXPECT_EQ(this->status_after_(2, 0), "Error"); + EXPECT_EQ(this->status_after_(0, 0), "Standby"); +} + +} // namespace esphome::m5_unit_roller485::testing From f22d7f3b3c63a129f5f1082dc4306616d0f83b38 Mon Sep 17 00:00:00 2001 From: Braven-agents Date: Wed, 2 Dec 2026 09:53:39 +0800 Subject: [PATCH 2/3] [m5_unit_roller485] Write the LED mode with every colour The menu on the unit can switch the LED back to its own colours, so the mode byte now goes with every colour instead of once per connection. Co-Authored-By: Claude Opus 5.5 --- .../m5_unit_roller485/m5_unit_roller485.cpp | 18 +++++++++++------- .../m5_unit_roller485/m5_unit_roller485.h | 4 ++-- .../test_m5_unit_roller485.cpp | 9 ++++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/esphome/components/m5_unit_roller485/m5_unit_roller485.cpp b/esphome/components/m5_unit_roller485/m5_unit_roller485.cpp index 146230c94fd9..3dbd18631a54 100644 --- a/esphome/components/m5_unit_roller485/m5_unit_roller485.cpp +++ b/esphome/components/m5_unit_roller485/m5_unit_roller485.cpp @@ -42,7 +42,7 @@ bool M5UnitRoller485::connect_() { } } #endif - this->led_mode_set_ = false; + this->led_brightness_set_ = false; if (this->led_set_ && !this->write_led_()) return false; // Publish every setting again, in case the unit changed while it was away @@ -83,6 +83,7 @@ bool M5UnitRoller485::read_settings_() { this->stall_protection_switch_->publish_state(data[REG_STALL_PROTECTION] != 0); #endif #ifdef USE_SELECT + // Firmware V4 adds a fifth mode that the M5Unit-Roller library does not list; the select then keeps its state const uint8_t mode = data[REG_MODE]; if (mode != this->mode_ && this->mode_select_ != nullptr && mode >= 1 && mode <= this->mode_select_->size()) this->mode_select_->publish_state(mode - 1); @@ -201,13 +202,16 @@ void M5UnitRoller485::add_target_number(uint8_t reg, number::Number *number) { #endif bool M5UnitRoller485::write_led_() { - // B, G, R, then 1 to show this colour instead of the unit's own status colours - const uint8_t rgb[4] = {this->led_[2], this->led_[1], this->led_[0], 1}; - if (this->led_mode_set_) - return this->write_bytes(REG_RGB, rgb, 3); // The light scales the colour itself, so the unit's brightness stays at full - this->led_mode_set_ = this->write_u8_(REG_RGB_BRIGHTNESS, 100) && this->write_bytes(REG_RGB, rgb, sizeof(rgb)); - return this->led_mode_set_; + if (!this->led_brightness_set_) { + if (!this->write_u8_(REG_RGB_BRIGHTNESS, 100)) + return false; + this->led_brightness_set_ = true; + } + // B, G, R, then 1 to show this colour instead of the unit's own status colours. The menu on the unit can set the + // mode back to 0, so it goes with every colour. + const uint8_t rgb[4] = {this->led_[2], this->led_[1], this->led_[0], 1}; + return this->write_bytes(REG_RGB, rgb, sizeof(rgb)); } bool M5UnitRoller485::write_u8_(uint8_t reg, uint8_t value) { return this->write_bytes(reg, &value, 1); } diff --git a/esphome/components/m5_unit_roller485/m5_unit_roller485.h b/esphome/components/m5_unit_roller485/m5_unit_roller485.h index dd5281cad292..25fea5084c0f 100644 --- a/esphome/components/m5_unit_roller485/m5_unit_roller485.h +++ b/esphome/components/m5_unit_roller485/m5_unit_roller485.h @@ -124,8 +124,8 @@ class M5UnitRoller485 : public i2c::I2CDevice, public PollingComponent { bool connected_{false}; bool connected_once_{false}; bool led_set_{false}; - // The LED mode and brightness are written once per connection, then only the colour - bool led_mode_set_{false}; + // The LED brightness is written once per connection + bool led_brightness_set_{false}; }; } // namespace esphome::m5_unit_roller485 diff --git a/tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp b/tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp index 0527e629fd3c..becf8d280ae9 100644 --- a/tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp +++ b/tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp @@ -80,15 +80,18 @@ TEST_F(M5UnitRoller485Test, LedSetMeanwhileIsWrittenOnConnect) { EXPECT_EQ(this->bus_.regs[REG_RGB_BRIGHTNESS], 100); } -// The unit keeps the mode and brightness, so later colours only write the colour -TEST_F(M5UnitRoller485Test, LaterColoursOnlyWriteTheColour) { +// The menu on the unit can switch the LED back to its own colours; the next colour switches it back +TEST_F(M5UnitRoller485Test, LaterColoursSetTheModeAgain) { this->hub_.setup(); this->hub_.write_led(1, 2, 3); this->bus_.regs[REG_RGB_MODE] = 0; + this->bus_.regs[REG_RGB_BRIGHTNESS] = 50; this->hub_.write_led(4, 5, 6); EXPECT_EQ(this->bus_.regs[REG_RGB], 6); EXPECT_EQ(this->bus_.regs[REG_RGB + 2], 4); - EXPECT_EQ(this->bus_.regs[REG_RGB_MODE], 0); + EXPECT_EQ(this->bus_.regs[REG_RGB_MODE], 1); + // Brightness is written once per connection + EXPECT_EQ(this->bus_.regs[REG_RGB_BRIGHTNESS], 50); } // Without a light the LED keeps the unit's own status colours From 5f3dbe2ebc146695ec3f69266a79e7068c8386ae Mon Sep 17 00:00:00 2001 From: Braven-agents Date: Wed, 2 Dec 2026 10:07:13 +0800 Subject: [PATCH 3/3] [m5_unit_roller485] Write the LED brightness with every colour The menu on the unit can change the brightness as well as the LED mode, so both now go with every colour. Rename to_byte_ to to_byte, which clang-tidy requires for a static method. Co-Authored-By: Claude Opus 5.5 --- .../light/m5_unit_roller485_light.h | 4 ++-- .../m5_unit_roller485/m5_unit_roller485.cpp | 14 +++++--------- .../m5_unit_roller485/m5_unit_roller485.h | 2 -- .../m5_unit_roller485/test_m5_unit_roller485.cpp | 7 +++---- 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/esphome/components/m5_unit_roller485/light/m5_unit_roller485_light.h b/esphome/components/m5_unit_roller485/light/m5_unit_roller485_light.h index a721cd0510ef..cafc3e9b6be5 100644 --- a/esphome/components/m5_unit_roller485/light/m5_unit_roller485_light.h +++ b/esphome/components/m5_unit_roller485/light/m5_unit_roller485_light.h @@ -20,11 +20,11 @@ class M5UnitRoller485Light : public light::LightOutput, public Parentedcurrent_values_as_rgb(&red, &green, &blue); - this->parent_->write_led(to_byte_(red), to_byte_(green), to_byte_(blue)); + this->parent_->write_led(to_byte(red), to_byte(green), to_byte(blue)); } protected: - static uint8_t to_byte_(float value) { return static_cast(lroundf(value * 255.0f)); } + static uint8_t to_byte(float value) { return static_cast(lroundf(value * 255.0f)); } }; } // namespace esphome::m5_unit_roller485 diff --git a/esphome/components/m5_unit_roller485/m5_unit_roller485.cpp b/esphome/components/m5_unit_roller485/m5_unit_roller485.cpp index 3dbd18631a54..dc5b75377f52 100644 --- a/esphome/components/m5_unit_roller485/m5_unit_roller485.cpp +++ b/esphome/components/m5_unit_roller485/m5_unit_roller485.cpp @@ -42,7 +42,6 @@ bool M5UnitRoller485::connect_() { } } #endif - this->led_brightness_set_ = false; if (this->led_set_ && !this->write_led_()) return false; // Publish every setting again, in case the unit changed while it was away @@ -202,14 +201,11 @@ void M5UnitRoller485::add_target_number(uint8_t reg, number::Number *number) { #endif bool M5UnitRoller485::write_led_() { - // The light scales the colour itself, so the unit's brightness stays at full - if (!this->led_brightness_set_) { - if (!this->write_u8_(REG_RGB_BRIGHTNESS, 100)) - return false; - this->led_brightness_set_ = true; - } - // B, G, R, then 1 to show this colour instead of the unit's own status colours. The menu on the unit can set the - // mode back to 0, so it goes with every colour. + // The menu on the unit can change the brightness and the LED mode, so both go with every colour. + // The light scales the colour itself, so the unit's brightness stays at full. + if (!this->write_u8_(REG_RGB_BRIGHTNESS, 100)) + return false; + // B, G, R, then 1 to show this colour instead of the unit's own status colours const uint8_t rgb[4] = {this->led_[2], this->led_[1], this->led_[0], 1}; return this->write_bytes(REG_RGB, rgb, sizeof(rgb)); } diff --git a/esphome/components/m5_unit_roller485/m5_unit_roller485.h b/esphome/components/m5_unit_roller485/m5_unit_roller485.h index 25fea5084c0f..033665d12114 100644 --- a/esphome/components/m5_unit_roller485/m5_unit_roller485.h +++ b/esphome/components/m5_unit_roller485/m5_unit_roller485.h @@ -124,8 +124,6 @@ class M5UnitRoller485 : public i2c::I2CDevice, public PollingComponent { bool connected_{false}; bool connected_once_{false}; bool led_set_{false}; - // The LED brightness is written once per connection - bool led_brightness_set_{false}; }; } // namespace esphome::m5_unit_roller485 diff --git a/tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp b/tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp index becf8d280ae9..d288f1ad5881 100644 --- a/tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp +++ b/tests/components/m5_unit_roller485/test_m5_unit_roller485.cpp @@ -80,8 +80,8 @@ TEST_F(M5UnitRoller485Test, LedSetMeanwhileIsWrittenOnConnect) { EXPECT_EQ(this->bus_.regs[REG_RGB_BRIGHTNESS], 100); } -// The menu on the unit can switch the LED back to its own colours; the next colour switches it back -TEST_F(M5UnitRoller485Test, LaterColoursSetTheModeAgain) { +// The menu on the unit can change the LED mode and brightness; the next colour sets them back +TEST_F(M5UnitRoller485Test, LaterColoursSetTheModeAndBrightnessAgain) { this->hub_.setup(); this->hub_.write_led(1, 2, 3); this->bus_.regs[REG_RGB_MODE] = 0; @@ -90,8 +90,7 @@ TEST_F(M5UnitRoller485Test, LaterColoursSetTheModeAgain) { EXPECT_EQ(this->bus_.regs[REG_RGB], 6); EXPECT_EQ(this->bus_.regs[REG_RGB + 2], 4); EXPECT_EQ(this->bus_.regs[REG_RGB_MODE], 1); - // Brightness is written once per connection - EXPECT_EQ(this->bus_.regs[REG_RGB_BRIGHTNESS], 50); + EXPECT_EQ(this->bus_.regs[REG_RGB_BRIGHTNESS], 100); } // Without a light the LED keeps the unit's own status colours