Further clean up garage-gate
This commit is contained in:
@@ -1,73 +0,0 @@
|
|||||||
import esphome.codegen as cg
|
|
||||||
from esphome.components import binary_sensor
|
|
||||||
import esphome.config_validation as cv
|
|
||||||
from esphome.const import CONF_ID
|
|
||||||
|
|
||||||
from .. import (
|
|
||||||
RATGDO_CLIENT_SCHMEA,
|
|
||||||
ratgdo_ns,
|
|
||||||
register_ratgdo_child,
|
|
||||||
subscribe_vehicle_arriving,
|
|
||||||
subscribe_vehicle_detected,
|
|
||||||
subscribe_vehicle_leaving,
|
|
||||||
)
|
|
||||||
|
|
||||||
DEPENDENCIES = ["ratgdo"]
|
|
||||||
|
|
||||||
# Track which sensor types have been used
|
|
||||||
USED_TYPES: set[str] = set()
|
|
||||||
|
|
||||||
RATGDOBinarySensor = ratgdo_ns.class_(
|
|
||||||
"RATGDOBinarySensor", binary_sensor.BinarySensor, cg.Component
|
|
||||||
)
|
|
||||||
SensorType = ratgdo_ns.enum("SensorType")
|
|
||||||
|
|
||||||
CONF_TYPE = "type"
|
|
||||||
TYPES = {
|
|
||||||
"vehicle_detected": SensorType.RATGDO_SENSOR_VEHICLE_DETECTED,
|
|
||||||
"vehicle_arriving": SensorType.RATGDO_SENSOR_VEHICLE_ARRIVING,
|
|
||||||
"vehicle_leaving": SensorType.RATGDO_SENSOR_VEHICLE_LEAVING,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Sensor types that require vehicle sensor support
|
|
||||||
VEHICLE_SENSOR_TYPES = {"vehicle_detected", "vehicle_arriving", "vehicle_leaving"}
|
|
||||||
|
|
||||||
|
|
||||||
def validate_unique_type(config):
|
|
||||||
"""Validate that each sensor type is only used once."""
|
|
||||||
sensor_type = config[CONF_TYPE]
|
|
||||||
if sensor_type in USED_TYPES:
|
|
||||||
raise cv.Invalid(f"Only one binary sensor of type '{sensor_type}' is allowed")
|
|
||||||
USED_TYPES.add(sensor_type)
|
|
||||||
return config
|
|
||||||
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
|
||||||
binary_sensor.binary_sensor_schema(RATGDOBinarySensor)
|
|
||||||
.extend(
|
|
||||||
{
|
|
||||||
cv.Required(CONF_TYPE): cv.enum(TYPES, lower=True),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.extend(RATGDO_CLIENT_SCHMEA),
|
|
||||||
validate_unique_type,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
|
||||||
await binary_sensor.register_binary_sensor(var, config)
|
|
||||||
await cg.register_component(var, config)
|
|
||||||
cg.add(var.set_binary_sensor_type(config[CONF_TYPE]))
|
|
||||||
await register_ratgdo_child(var, config)
|
|
||||||
|
|
||||||
# Add defines for enabled features and register observable subscriptions
|
|
||||||
sensor_type = config[CONF_TYPE]
|
|
||||||
if sensor_type in VEHICLE_SENSOR_TYPES:
|
|
||||||
cg.add_define("RATGDO_USE_VEHICLE_SENSORS")
|
|
||||||
if sensor_type == "vehicle_detected":
|
|
||||||
subscribe_vehicle_detected()
|
|
||||||
elif sensor_type == "vehicle_arriving":
|
|
||||||
subscribe_vehicle_arriving()
|
|
||||||
elif sensor_type == "vehicle_leaving":
|
|
||||||
subscribe_vehicle_leaving()
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
#include "ratgdo_binary_sensor.h"
|
|
||||||
#include "../ratgdo_state.h"
|
|
||||||
#include "esphome/core/log.h"
|
|
||||||
|
|
||||||
namespace esphome::ratgdo {
|
|
||||||
|
|
||||||
static const char* const TAG = "ratgdo.binary_sensor";
|
|
||||||
|
|
||||||
void RATGDOBinarySensor::setup()
|
|
||||||
{
|
|
||||||
// Initialize all sensors to false
|
|
||||||
this->publish_initial_state(false);
|
|
||||||
|
|
||||||
switch (this->binary_sensor_type_) {
|
|
||||||
#ifdef RATGDO_USE_VEHICLE_SENSORS
|
|
||||||
case SensorType::RATGDO_SENSOR_VEHICLE_DETECTED:
|
|
||||||
this->parent_->subscribe_vehicle_detected_state([this](VehicleDetectedState state) {
|
|
||||||
this->publish_state(state == VehicleDetectedState::YES);
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case SensorType::RATGDO_SENSOR_VEHICLE_ARRIVING:
|
|
||||||
this->parent_->subscribe_vehicle_arriving_state([this](VehicleArrivingState state) {
|
|
||||||
this->publish_state(state == VehicleArrivingState::YES);
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case SensorType::RATGDO_SENSOR_VEHICLE_LEAVING:
|
|
||||||
this->parent_->subscribe_vehicle_leaving_state([this](VehicleLeavingState state) {
|
|
||||||
this->publish_state(state == VehicleLeavingState::YES);
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RATGDOBinarySensor::dump_config()
|
|
||||||
{
|
|
||||||
LOG_BINARY_SENSOR("", "RATGDO BinarySensor", this);
|
|
||||||
switch (this->binary_sensor_type_) {
|
|
||||||
#ifdef RATGDO_USE_VEHICLE_SENSORS
|
|
||||||
case SensorType::RATGDO_SENSOR_VEHICLE_DETECTED:
|
|
||||||
ESP_LOGCONFIG(TAG, " Type: VehicleDetected");
|
|
||||||
break;
|
|
||||||
case SensorType::RATGDO_SENSOR_VEHICLE_ARRIVING:
|
|
||||||
ESP_LOGCONFIG(TAG, " Type: VehicleArriving");
|
|
||||||
break;
|
|
||||||
case SensorType::RATGDO_SENSOR_VEHICLE_LEAVING:
|
|
||||||
ESP_LOGCONFIG(TAG, " Type: VehicleLeaving");
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace esphome::ratgdo
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "../ratgdo.h"
|
|
||||||
#include "../ratgdo_state.h"
|
|
||||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
|
||||||
#include "esphome/core/component.h"
|
|
||||||
#include "esphome/core/defines.h"
|
|
||||||
|
|
||||||
namespace esphome::ratgdo {
|
|
||||||
|
|
||||||
enum SensorType : uint8_t {
|
|
||||||
#ifdef RATGDO_USE_VEHICLE_SENSORS
|
|
||||||
RATGDO_SENSOR_VEHICLE_DETECTED = 4,
|
|
||||||
RATGDO_SENSOR_VEHICLE_ARRIVING,
|
|
||||||
RATGDO_SENSOR_VEHICLE_LEAVING,
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
class RATGDOBinarySensor : public binary_sensor::BinarySensor, public RATGDOClient, public Component {
|
|
||||||
public:
|
|
||||||
void setup() override;
|
|
||||||
void dump_config() override;
|
|
||||||
void set_binary_sensor_type(SensorType binary_sensor_type) { this->binary_sensor_type_ = binary_sensor_type; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
SensorType binary_sensor_type_;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace esphome::ratgdo
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
import esphome.codegen as cg
|
|
||||||
from esphome.components import rtttl
|
|
||||||
import esphome.config_validation as cv
|
|
||||||
from esphome.const import CONF_ID
|
|
||||||
|
|
||||||
from .. import (
|
|
||||||
RATGDO_CLIENT_SCHMEA,
|
|
||||||
ratgdo_ns,
|
|
||||||
register_ratgdo_child,
|
|
||||||
subscribe_door_action_delayed,
|
|
||||||
)
|
|
||||||
|
|
||||||
CONF_RTTTL = "rtttl"
|
|
||||||
CONF_SONG = "song"
|
|
||||||
|
|
||||||
DEPENDENCIES = ["esp32", "ratgdo", "rtttl"]
|
|
||||||
|
|
||||||
RATGDOOutput = ratgdo_ns.class_("RATGDOOutput", cg.Component)
|
|
||||||
OutputType = ratgdo_ns.enum("OutputType")
|
|
||||||
|
|
||||||
CONF_TYPE = "type"
|
|
||||||
TYPES = {"beeper": OutputType.RATGDO_BEEPER}
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.Schema(
|
|
||||||
{
|
|
||||||
cv.Required(CONF_ID): cv.declare_id(RATGDOOutput),
|
|
||||||
cv.Required(CONF_TYPE): cv.enum(TYPES, lower=True),
|
|
||||||
cv.Required(CONF_RTTTL): cv.use_id(rtttl),
|
|
||||||
cv.Required(CONF_SONG): cv.string,
|
|
||||||
}
|
|
||||||
).extend(RATGDO_CLIENT_SCHMEA)
|
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
|
||||||
await cg.register_component(var, config)
|
|
||||||
rtttl = await cg.get_variable(config[CONF_RTTTL])
|
|
||||||
cg.add(var.set_rtttl(rtttl))
|
|
||||||
cg.add(var.set_song(config[CONF_SONG]))
|
|
||||||
await register_ratgdo_child(var, config)
|
|
||||||
subscribe_door_action_delayed()
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
#include "ratgdo_output.h"
|
|
||||||
#include "../ratgdo_state.h"
|
|
||||||
#include "esphome/core/log.h"
|
|
||||||
|
|
||||||
namespace esphome::ratgdo {
|
|
||||||
|
|
||||||
static const char* TAG = "ratgdo.output";
|
|
||||||
|
|
||||||
void RATGDOOutput::setup()
|
|
||||||
{
|
|
||||||
ESP_LOGD(TAG, "Output was setup");
|
|
||||||
|
|
||||||
if (this->output_type_ == OutputType::RATGDO_BEEPER) {
|
|
||||||
this->beeper_->add_on_finished_playback_callback([this] { this->finished_playback(); });
|
|
||||||
|
|
||||||
this->parent_->subscribe_door_action_delayed([this](DoorActionDelayed state) {
|
|
||||||
if (state == DoorActionDelayed::YES) {
|
|
||||||
this->play();
|
|
||||||
this->repeat_ = true;
|
|
||||||
} else if (state == DoorActionDelayed::NO) {
|
|
||||||
this->repeat_ = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RATGDOOutput::play()
|
|
||||||
{
|
|
||||||
this->beeper_->play(this->rtttlSong_);
|
|
||||||
}
|
|
||||||
|
|
||||||
void RATGDOOutput::finished_playback()
|
|
||||||
{
|
|
||||||
if (this->repeat_)
|
|
||||||
this->play();
|
|
||||||
}
|
|
||||||
|
|
||||||
void RATGDOOutput::dump_config()
|
|
||||||
{
|
|
||||||
if (this->output_type_ == OutputType::RATGDO_BEEPER) {
|
|
||||||
ESP_LOGCONFIG(TAG, " Type: Beeper");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RATGDOOutput::set_output_type(OutputType output_type_)
|
|
||||||
{
|
|
||||||
this->output_type_ = output_type_;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace esphome::ratgdo
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "../ratgdo.h"
|
|
||||||
#include "esphome/components/rtttl/rtttl.h"
|
|
||||||
#include "esphome/core/component.h"
|
|
||||||
|
|
||||||
namespace esphome::ratgdo {
|
|
||||||
|
|
||||||
enum OutputType {
|
|
||||||
RATGDO_BEEPER
|
|
||||||
};
|
|
||||||
|
|
||||||
class RATGDOOutput : public RATGDOClient, public Component {
|
|
||||||
public:
|
|
||||||
void setup() override;
|
|
||||||
void play();
|
|
||||||
void finished_playback();
|
|
||||||
void dump_config() override;
|
|
||||||
void set_output_type(OutputType output_type);
|
|
||||||
void set_song(std::string rtttlSong) { this->rtttlSong_ = rtttlSong; }
|
|
||||||
void set_rtttl(rtttl::Rtttl* output) { this->beeper_ = output; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
OutputType output_type_;
|
|
||||||
rtttl::Rtttl* beeper_;
|
|
||||||
std::string rtttlSong_;
|
|
||||||
bool repeat_;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace esphome::ratgdo
|
|
||||||
@@ -18,7 +18,6 @@
|
|||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
#include "esphome/core/preferences.h"
|
#include "esphome/core/preferences.h"
|
||||||
|
|
||||||
#include <bitset>
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
@@ -59,7 +58,6 @@ public:
|
|||||||
float start_closing { -1 };
|
float start_closing { -1 };
|
||||||
single_observable<float> closing_duration { 0 };
|
single_observable<float> closing_duration { 0 };
|
||||||
|
|
||||||
std::bitset<256> in_range; // the length of this bitset determines how many out of range readings are required for presence detection to change states
|
|
||||||
observable<int16_t, RATGDO_MAX_DISTANCE_SUBSCRIBERS> last_distance_measurement { 0 };
|
observable<int16_t, RATGDO_MAX_DISTANCE_SUBSCRIBERS> last_distance_measurement { 0 };
|
||||||
|
|
||||||
single_observable<uint16_t> openings { 0 }; // number of times the door has been opened
|
single_observable<uint16_t> openings { 0 }; // number of times the door has been opened
|
||||||
|
|||||||
@@ -26,10 +26,6 @@ ENUM(DoorState, uint8_t,
|
|||||||
(OPENING, 4),
|
(OPENING, 4),
|
||||||
(CLOSING, 5))
|
(CLOSING, 5))
|
||||||
|
|
||||||
ENUM(DoorActionDelayed, uint8_t,
|
|
||||||
(NO, 0),
|
|
||||||
(YES, 1))
|
|
||||||
|
|
||||||
/// Enum for all states a the light can be in.
|
/// Enum for all states a the light can be in.
|
||||||
ENUM(LightState, uint8_t,
|
ENUM(LightState, uint8_t,
|
||||||
(OFF, 0),
|
(OFF, 0),
|
||||||
|
|||||||
Reference in New Issue
Block a user