Tidy up and move tests to common

This commit is contained in:
Jesse Hills 2024-11-13 11:50:03 +13:00
parent 5a47088c5e
commit a6586c7ae6
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
10 changed files with 139 additions and 275 deletions

View file

@ -0,0 +1,40 @@
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/helpers.h"
#include "max7219digit.h"
namespace esphome {
namespace max7219digit {
template<typename... Ts> class DisplayInvertAction : public Action<Ts...>, public Parented<MAX7219Component> {
public:
TEMPLATABLE_VALUE(bool, state)
void play(Ts... x) override { this->parent_->invert_on_off(this->state_.optional_value(x...)); }
};
template<typename... Ts> class DisplayVisiblityAction : public Action<Ts...>, public Parented<MAX7219Component> {
public:
TEMPLATABLE_VALUE(bool, state)
void play(Ts... x) override { this->parent_->turn_on_off(this->state_.optional_value(x...)); }
};
template<typename... Ts> class DisplayReverseAction : public Action<Ts...>, public Parented<MAX7219Component> {
public:
TEMPLATABLE_VALUE(bool, state)
void play(Ts... x) override { this->parent_->set_reverse(this->state_.optional_value(x...)); }
};
template<typename... Ts> class DisplayIntensityAction : public Action<Ts...>, public Parented<MAX7219Component> {
public:
TEMPLATABLE_VALUE(uint8_t, state)
void play(Ts... x) override { this->parent_->intensity(this->state_.optional_value(x...)); }
};
} // namespace max7219digit
} // namespace esphome

View file

@ -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

View file

@ -120,45 +120,5 @@ class MAX7219Component : public display::DisplayBuffer,
optional<max7219_writer_t> writer_local_{};
};
template<typename... Ts> class DisplayInvertAction : public Action<Ts...> {
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<typename... Ts> class DisplayVisiblityAction : public Action<Ts...> {
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<typename... Ts> class DisplayReverseAction : public Action<Ts...> {
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<typename... Ts> class DisplayIntensityAction : public Action<Ts...> {
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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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