Added TableSoccer as esphome... It's finally no longer an ill-configure Homie

This commit is contained in:
2024-05-02 12:19:08 +02:00
parent 8ed0efd32c
commit 264bcb66f6
3 changed files with 201 additions and 0 deletions

View File

@ -0,0 +1,41 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import mqtt, gpio
from esphome.const import CONF_ID
DEPENDENCIES=["mqtt"]
tablesoccer_helper_ns = cg.esphome_ns.namespace('tablesoccer_helper')
BtnHelperClass = tablesoccer_helper_ns.class_('BtnHelperClass', mqtt.MQTTComponent, cg.Component)
#CONF_WS2812_PIN = "pin"
#CONF_WS2812_RGB_MODE = "rgb_mode"
CONF_SENSOR_RED = "sensor_blue"
CONF_SENSOR_BLUE = "sender_red"
CONF_BTN_RESET = "reset_pin"
CONF_BTN_UNDO = "undo_pin"
CONF_KEEP_ALIVE_OUTPUT = "keep_alive"
CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({
cv.GenerateID(CONF_ID): cv.declare_id(BtnHelperClass),
cv.Required(CONF_BTN_RESET): pins.gpio_output_pin_schema,
cv.Required(CONF_BTN_UNDO): pins.gpio_output_pin_schema,
cv.Required(CONF_KEEP_ALIVE_OUTPUT): cv.use_id(gpio.output.GPIOBinaryOutput),
})
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
#yield cg.register_component(var, config)
yield mqtt.register_mqtt_component(var, config)
btnReset = yield cg.gpio_pin_expression(config[CONF_BTN_RESET])
cg.add(var.set_reset_pin(btnReset))
btnUndo = yield cg.gpio_pin_expression(config[CONF_BTN_UNDO])
cg.add(var.set_undo_pin(btnUndo))
keep_alive = yield cg.get_variable(config[CONF_KEEP_ALIVE_OUTPUT])
cg.add(var.set_keep_alive_output(keep_alive))

View File

@ -0,0 +1,73 @@
#include "esphome.h"
namespace esphome {
namespace tablesoccer_helper {
class BtnHelperClass : public Component, public mqtt::CustomMQTTDevice {
protected:
InternalGPIOPin *resetBtn;
InternalGPIOPin *undoBtn;
gpio::GPIOBinaryOutput *keep_alive;
unsigned long lastThingDone = 0;
unsigned long enableFrom = 0;
public:
void set_reset_pin(InternalGPIOPin *resetBtn){
this->resetBtn = resetBtn;
}
void set_undo_pin(InternalGPIOPin *undoBtn){
this->undoBtn = undoBtn;
}
void set_keep_alive_output(gpio::GPIOBinaryOutput *keep_alive){
this->keep_alive = keep_alive;
}
void setup() override {
lastThingDone = millis();
enableFrom = millis();
ESP_LOGD("Tablesoccer", "Ready");
}
void reset_last_thing_done(){
lastThingDone = millis();
}
void send_score(std::string side){
publish("tabletenniscounter/control/score/count", side, 2, false);
lastThingDone = millis();
enableFrom = lastThingDone + 200;
}
void loop() override{
if(millis() < enableFrom) return;
if(resetBtn->digital_read() == LOW && undoBtn->digital_read() == LOW){
ESP_LOGD("Tablesoccer", "User requested shutdown, Shutting Down.");
keep_alive->set_state(false);
}
if(resetBtn->digital_read() == LOW){
publish("tabletenniscounter/control/score/reset", "", 2, false);
lastThingDone = millis();
enableFrom = lastThingDone + 500;
}
else if(undoBtn->digital_read() == LOW){
publish("tabletenniscounter/control/score/undo", "", 2, false);
lastThingDone = millis();
enableFrom = lastThingDone + 500;
}
else if((millis() - lastThingDone) >= 300000){
ESP_LOGD("Tablesoccer", "Table soccer table has't been used in over 15 minutes, Shutting Down.");
keep_alive->set_state(false);
}
}
};
}
}