From a6586c7ae64fab6c1c72b9a1c25a291a11ab7021 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:50:03 +1300 Subject: [PATCH] Tidy up and move tests to common --- esphome/components/max7219digit/automation.h | 40 +++++++++++ esphome/components/max7219digit/display.py | 66 ++++++++++--------- .../components/max7219digit/max7219digit.h | 40 ----------- tests/components/max7219digit/common.yaml | 28 ++++++++ .../max7219digit/test.esp32-ard.yaml | 40 ++--------- .../max7219digit/test.esp32-c3-ard.yaml | 40 ++--------- .../max7219digit/test.esp32-c3-idf.yaml | 40 ++--------- .../max7219digit/test.esp32-idf.yaml | 40 ++--------- .../max7219digit/test.esp8266-ard.yaml | 40 ++--------- .../max7219digit/test.rp2040-ard.yaml | 40 ++--------- 10 files changed, 139 insertions(+), 275 deletions(-) create mode 100644 esphome/components/max7219digit/automation.h create mode 100644 tests/components/max7219digit/common.yaml diff --git a/esphome/components/max7219digit/automation.h b/esphome/components/max7219digit/automation.h new file mode 100644 index 0000000000..4a21529ea9 --- /dev/null +++ b/esphome/components/max7219digit/automation.h @@ -0,0 +1,40 @@ +#pragma once + +#include "esphome/core/automation.h" +#include "esphome/core/helpers.h" + +#include "max7219digit.h" + +namespace esphome { +namespace max7219digit { + +template class DisplayInvertAction : public Action, public Parented { + public: + TEMPLATABLE_VALUE(bool, state) + + void play(Ts... x) override { this->parent_->invert_on_off(this->state_.optional_value(x...)); } +}; + +template class DisplayVisiblityAction : public Action, public Parented { + public: + TEMPLATABLE_VALUE(bool, state) + + void play(Ts... x) override { this->parent_->turn_on_off(this->state_.optional_value(x...)); } +}; + +template class DisplayReverseAction : public Action, public Parented { + public: + TEMPLATABLE_VALUE(bool, state) + + void play(Ts... x) override { this->parent_->set_reverse(this->state_.optional_value(x...)); } +}; + +template class DisplayIntensityAction : public Action, public Parented { + public: + TEMPLATABLE_VALUE(uint8_t, state) + + void play(Ts... x) override { this->parent_->intensity(this->state_.optional_value(x...)); } +}; + +} // namespace max7219digit +} // namespace esphome diff --git a/esphome/components/max7219digit/display.py b/esphome/components/max7219digit/display.py index 35ff19782f..608272ad49 100644 --- a/esphome/components/max7219digit/display.py +++ b/esphome/components/max7219digit/display.py @@ -119,71 +119,75 @@ DisplayIntensityAction = max7219_ns.class_("DisplayIntensityAction", automation. MAX7219_OFF_ACTION_SCHEMA = automation.maybe_simple_id( { - cv.Required(CONF_ID): cv.use_id(MAX7219Component), + cv.GenerateID(): cv.use_id(MAX7219Component), cv.Optional(CONF_STATE, default=False): False, } ) MAX7219_ON_ACTION_SCHEMA = automation.maybe_simple_id( { - cv.Required(CONF_ID): cv.use_id(MAX7219Component), + cv.GenerateID(): cv.use_id(MAX7219Component), cv.Optional(CONF_STATE, default=True): True, } ) @automation.register_action( - "MAX7219.invert_off", DisplayInvertAction, MAX7219_OFF_ACTION_SCHEMA + "max7129digit.invert_off", DisplayInvertAction, MAX7219_OFF_ACTION_SCHEMA ) @automation.register_action( - "MAX7219.invert_on", DisplayInvertAction, MAX7219_ON_ACTION_SCHEMA + "max7129digit.invert_on", DisplayInvertAction, MAX7219_ON_ACTION_SCHEMA ) -async def MAX7219_inveert_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - var = cg.new_Pvariable(action_id, template_arg, paren) - template_ = await cg.templatable(config[CONF_STATE], args, bool) - cg.add(var.set_state(template_)) +async def max7129digit_invert_to_code(config, action_id, template_arg, args): + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + cg.add(var.set_state(config[CONF_STATE])) + return var @automation.register_action( - "MAX7219.turn_off", DisplayVisibilityAction, MAX7219_OFF_ACTION_SCHEMA + "max7129digit.turn_off", DisplayVisibilityAction, MAX7219_OFF_ACTION_SCHEMA ) @automation.register_action( - "MAX7219.turn_on", DisplayVisibilityAction, MAX7219_ON_ACTION_SCHEMA + "max7129digit.turn_on", DisplayVisibilityAction, MAX7219_ON_ACTION_SCHEMA ) -async def MAX7219_visible_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - var = cg.new_Pvariable(action_id, template_arg, paren) - template_ = await cg.templatable(config[CONF_STATE], args, bool) - cg.add(var.set_state(template_)) +async def max7129digit_visible_to_code(config, action_id, template_arg, args): + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + cg.add(var.set_state(config[CONF_STATE])) + return var @automation.register_action( - "MAX7219.reverse_off", DisplayReverseAction, MAX7219_OFF_ACTION_SCHEMA + "max7129digit.reverse_off", DisplayReverseAction, MAX7219_OFF_ACTION_SCHEMA ) @automation.register_action( - "MAX7219.reverse_on", DisplayReverseAction, MAX7219_ON_ACTION_SCHEMA + "max7129digit.reverse_on", DisplayReverseAction, MAX7219_ON_ACTION_SCHEMA ) -async def MAX7219_reverse_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - var = cg.new_Pvariable(action_id, template_arg, paren) - template_ = await cg.templatable(config[CONF_STATE], args, bool) - cg.add(var.set_state(template_)) +async def max7129digit_reverse_to_code(config, action_id, template_arg, args): + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + cg.add(var.set_state(config[CONF_STATE])) + return var -MAX7219_INTENSITY_SCHEMA = automation.maybe_simple_id( +MAX7219_INTENSITY_SCHEMA = cv.maybe_simple_value( { - cv.Required(CONF_ID): cv.use_id(MAX7219Component), - cv.Optional(CONF_INTENSITY, default=15): cv.int_range(min=0, max=15), - } + cv.GenerateID(): cv.use_id(MAX7219Component), + cv.Optional(CONF_INTENSITY, default=15): cv.templatable( + cv.int_range(min=0, max=15) + ), + }, + key=CONF_INTENSITY, ) @automation.register_action( - "MAX7219.intensity", DisplayIntensityAction, MAX7219_INTENSITY_SCHEMA + "max7129digit.intensity", DisplayIntensityAction, MAX7219_INTENSITY_SCHEMA ) -async def MAX7219_intensity_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - var = cg.new_Pvariable(action_id, template_arg, paren) +async def max7129digit_intensity_to_code(config, action_id, template_arg, args): + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) template_ = await cg.templatable(config[CONF_INTENSITY], args, cg.int_) cg.add(var.set_state(template_)) + return var diff --git a/esphome/components/max7219digit/max7219digit.h b/esphome/components/max7219digit/max7219digit.h index 1e8d0c657c..ead8033803 100644 --- a/esphome/components/max7219digit/max7219digit.h +++ b/esphome/components/max7219digit/max7219digit.h @@ -120,45 +120,5 @@ class MAX7219Component : public display::DisplayBuffer, optional writer_local_{}; }; -template class DisplayInvertAction : public Action { - public: - DisplayInvertAction(MAX7219Component *buffer) : buffer_(buffer) {} - TEMPLATABLE_VALUE(bool, state) - - void play(Ts... x) override { this->buffer_->invert_on_off(this->state_.optional_value(x...)); } - - MAX7219Component *buffer_; -}; - -template class DisplayVisiblityAction : public Action { - public: - DisplayVisiblityAction(MAX7219Component *buffer) : buffer_(buffer) {} - TEMPLATABLE_VALUE(bool, state) - - void play(Ts... x) override { this->buffer_->turn_on_off(this->state_.optional_value(x...)); } - - MAX7219Component *buffer_; -}; - -template class DisplayReverseAction : public Action { - public: - DisplayReverseAction(MAX7219Component *buffer) : buffer_(buffer) {} - TEMPLATABLE_VALUE(bool, state) - - void play(Ts... x) override { this->buffer_->set_reverse(this->state_.optional_value(x...)); } - - MAX7219Component *buffer_; -}; - -template class DisplayIntensityAction : public Action { - public: - DisplayIntensityAction(MAX7219Component *buffer) : buffer_(buffer) {} - TEMPLATABLE_VALUE(uint8_t, state) - - void play(Ts... x) override { this->buffer_->intensity(this->state_.optional_value(x...)); } - - MAX7219Component *buffer_; -}; - } // namespace max7219digit } // namespace esphome diff --git a/tests/components/max7219digit/common.yaml b/tests/components/max7219digit/common.yaml new file mode 100644 index 0000000000..84edc7eb3d --- /dev/null +++ b/tests/components/max7219digit/common.yaml @@ -0,0 +1,28 @@ +spi: + - id: spi_max7219digit + clk_pin: ${clk_pin} + mosi_pin: ${mosi_pin} + miso_pin: ${miso_pin} + +display: + - platform: max7219digit + cs_pin: ${cs_pin} + num_chips: 4 + rotate_chip: 0 + intensity: 10 + scroll_mode: STOP + id: my_matrix + lambda: |- + it.printdigit("hello"); + +esphome: + on_boot: + - priority: 100 + then: + - max7129digit.invert_off: + - max7129digit.invert_on: + - max7129digit.turn_on: + - max7129digit.turn_off: + - max7129digit.reverse_on: + - max7129digit.reverse_off: + - max7129digit.intensity: 10 diff --git a/tests/components/max7219digit/test.esp32-ard.yaml b/tests/components/max7219digit/test.esp32-ard.yaml index 6ea6e4b566..602ecb5328 100644 --- a/tests/components/max7219digit/test.esp32-ard.yaml +++ b/tests/components/max7219digit/test.esp32-ard.yaml @@ -1,35 +1,7 @@ -spi: - - id: spi_max7219digit - clk_pin: 16 - mosi_pin: 17 - miso_pin: 15 +substitutions: + cs_pin: "12" + clk_pin: "16" + mosi_pin: "17" + miso_pin: "15" -display: - - platform: max7219digit - cs_pin: 12 - num_chips: 4 - rotate_chip: 0 - intensity: 10 - scroll_mode: STOP - id: my_matrix - lambda: |- - it.printdigit("hello"); - -binary_sensor: - - platform: gpio - pin: GPIO00 - name: None - on_press: - then: - - MAX7219.invert_off: my_matrix - - MAX7219.invert_on: my_matrix - - - MAX7219.turn_off: my_matrix - - MAX7219.turn_on: my_matrix - - - MAX7219.reverse_off: my_matrix - - MAX7219.reverse_on: my_matrix - - - MAX7219.intensity: - id: my_matrix - intensity: 4 +<<: !include common.yaml diff --git a/tests/components/max7219digit/test.esp32-c3-ard.yaml b/tests/components/max7219digit/test.esp32-c3-ard.yaml index c369489038..a19db91332 100644 --- a/tests/components/max7219digit/test.esp32-c3-ard.yaml +++ b/tests/components/max7219digit/test.esp32-c3-ard.yaml @@ -1,35 +1,7 @@ -spi: - - id: spi_max7219digit - clk_pin: 6 - mosi_pin: 7 - miso_pin: 5 +substitions: + cs_pin: "8" + clk_pin: "6" + mosi_pin: "7" + miso_pin: "5" -display: - - platform: max7219digit - cs_pin: 8 - num_chips: 4 - rotate_chip: 0 - intensity: 10 - scroll_mode: STOP - id: my_matrix - lambda: |- - it.printdigit("hello"); - -binary_sensor: - - platform: gpio - pin: GPIO00 - name: None - on_press: - then: - - MAX7219.invert_off: my_matrix - - MAX7219.invert_on: my_matrix - - - MAX7219.turn_off: my_matrix - - MAX7219.turn_on: my_matrix - - - MAX7219.reverse_off: my_matrix - - MAX7219.reverse_on: my_matrix - - - MAX7219.intensity: - id: my_matrix - intensity: 4 +<<: !include common.yaml diff --git a/tests/components/max7219digit/test.esp32-c3-idf.yaml b/tests/components/max7219digit/test.esp32-c3-idf.yaml index c369489038..22b02e462e 100644 --- a/tests/components/max7219digit/test.esp32-c3-idf.yaml +++ b/tests/components/max7219digit/test.esp32-c3-idf.yaml @@ -1,35 +1,7 @@ -spi: - - id: spi_max7219digit - clk_pin: 6 - mosi_pin: 7 - miso_pin: 5 +substitutions: + cs_pin: "8" + clk_pin: "6" + mosi_pin: "7" + miso_pin: "5" -display: - - platform: max7219digit - cs_pin: 8 - num_chips: 4 - rotate_chip: 0 - intensity: 10 - scroll_mode: STOP - id: my_matrix - lambda: |- - it.printdigit("hello"); - -binary_sensor: - - platform: gpio - pin: GPIO00 - name: None - on_press: - then: - - MAX7219.invert_off: my_matrix - - MAX7219.invert_on: my_matrix - - - MAX7219.turn_off: my_matrix - - MAX7219.turn_on: my_matrix - - - MAX7219.reverse_off: my_matrix - - MAX7219.reverse_on: my_matrix - - - MAX7219.intensity: - id: my_matrix - intensity: 4 +<<: !include common.yaml diff --git a/tests/components/max7219digit/test.esp32-idf.yaml b/tests/components/max7219digit/test.esp32-idf.yaml index 6ea6e4b566..602ecb5328 100644 --- a/tests/components/max7219digit/test.esp32-idf.yaml +++ b/tests/components/max7219digit/test.esp32-idf.yaml @@ -1,35 +1,7 @@ -spi: - - id: spi_max7219digit - clk_pin: 16 - mosi_pin: 17 - miso_pin: 15 +substitutions: + cs_pin: "12" + clk_pin: "16" + mosi_pin: "17" + miso_pin: "15" -display: - - platform: max7219digit - cs_pin: 12 - num_chips: 4 - rotate_chip: 0 - intensity: 10 - scroll_mode: STOP - id: my_matrix - lambda: |- - it.printdigit("hello"); - -binary_sensor: - - platform: gpio - pin: GPIO00 - name: None - on_press: - then: - - MAX7219.invert_off: my_matrix - - MAX7219.invert_on: my_matrix - - - MAX7219.turn_off: my_matrix - - MAX7219.turn_on: my_matrix - - - MAX7219.reverse_off: my_matrix - - MAX7219.reverse_on: my_matrix - - - MAX7219.intensity: - id: my_matrix - intensity: 4 +<<: !include common.yaml diff --git a/tests/components/max7219digit/test.esp8266-ard.yaml b/tests/components/max7219digit/test.esp8266-ard.yaml index f96efa4fd8..65a8232655 100644 --- a/tests/components/max7219digit/test.esp8266-ard.yaml +++ b/tests/components/max7219digit/test.esp8266-ard.yaml @@ -1,35 +1,7 @@ -spi: - - id: spi_max7219digit - clk_pin: 14 - mosi_pin: 13 - miso_pin: 12 +substitutions: + cs_pin: "15" + clk_pin: "14" + mosi_pin: "13" + miso_pin: "12" -display: - - platform: max7219digit - cs_pin: 15 - num_chips: 4 - rotate_chip: 0 - intensity: 10 - scroll_mode: STOP - id: my_matrix - lambda: |- - it.printdigit("hello"); - -binary_sensor: - - platform: gpio - pin: GPIO00 - name: None - on_press: - then: - - MAX7219.invert_off: my_matrix - - MAX7219.invert_on: my_matrix - - - MAX7219.turn_off: my_matrix - - MAX7219.turn_on: my_matrix - - - MAX7219.reverse_off: my_matrix - - MAX7219.reverse_on: my_matrix - - - MAX7219.intensity: - id: my_matrix - intensity: 4 +<<: !include common.yaml diff --git a/tests/components/max7219digit/test.rp2040-ard.yaml b/tests/components/max7219digit/test.rp2040-ard.yaml index 6777cf2103..c2694c06e2 100644 --- a/tests/components/max7219digit/test.rp2040-ard.yaml +++ b/tests/components/max7219digit/test.rp2040-ard.yaml @@ -1,35 +1,7 @@ -spi: - - id: spi_max7219digit - clk_pin: 2 - mosi_pin: 3 - miso_pin: 4 +substitutions: + cs_pin: "6" + clk_pin: "2" + mosi_pin: "3" + miso_pin: "4" -display: - - platform: max7219digit - cs_pin: 6 - num_chips: 4 - rotate_chip: 0 - intensity: 10 - scroll_mode: STOP - id: my_matrix - lambda: |- - it.printdigit("hello"); - -binary_sensor: - - platform: gpio - pin: GPIO00 - name: None - on_press: - then: - - MAX7219.invert_off: my_matrix - - MAX7219.invert_on: my_matrix - - - MAX7219.turn_off: my_matrix - - MAX7219.turn_on: my_matrix - - - MAX7219.reverse_off: my_matrix - - MAX7219.reverse_on: my_matrix - - - MAX7219.intensity: - id: my_matrix - intensity: 4 +<<: !include common.yaml