This commit is contained in:
J. Nick Koston 2023-06-09 18:04:29 -05:00
parent a30cacabd6
commit 1bf07caf3a
No known key found for this signature in database
7 changed files with 51 additions and 3 deletions

View File

@ -17,6 +17,7 @@ TYPES = {
"motion": SensorType.RATGDO_SENSOR_MOTION,
"obstruction": SensorType.RATGDO_SENSOR_OBSTRUCTION,
"motor": SensorType.RATGDO_SENSOR_MOTOR,
"button": SensorType.RATGDO_SENSOR_BUTTON,
}

View File

@ -22,6 +22,8 @@ namespace ratgdo {
ESP_LOGCONFIG(TAG, " Type: Obstruction");
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_MOTOR) {
ESP_LOGCONFIG(TAG, " Type: Motor");
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_BUTTON) {
ESP_LOGCONFIG(TAG, " Type: Button");
}
}
void RATGDOBinarySensor::on_motion_state(MotionState state)
@ -45,6 +47,13 @@ namespace ratgdo {
ESP_LOGD(TAG, "name: %s this->type_:%d on_motor_state: %d", this->get_name(), this->binary_sensor_type_, state);
this->publish_state(state == MotorState::MOTOR_STATE_ON);
}
void RATGDOBinarySensor::on_button_state(ButtonState state)
{
if (this->binary_sensor_type_ != SensorType::RATGDO_SENSOR_BUTTON)
return;
ESP_LOGD(TAG, "name: %s this->type_:%d on_button_state: %d", this->get_name(), this->binary_sensor_type_, state);
this->publish_state(state == ButtonState::BUTTON_STATE_PRESSED);
}
} // namespace ratgdo
} // namespace esphome

View File

@ -24,6 +24,7 @@ namespace ratgdo {
void on_motion_state(MotionState state) override;
void on_obstruction_state(ObstructionState state) override;
void on_motor_state(MotorState state) override;
void on_button_state(ButtonState state) override;
protected:
SensorType binary_sensor_type_;

View File

@ -74,7 +74,7 @@ namespace ratgdo {
ESP_LOGCONFIG(TAG, " Rolling Code Counter: %d", this->rollingCodeCounter);
}
void RATGDOComponent::readRollingCode(bool& isStatus, uint8_t& door, uint8_t& light, uint8_t& lock, uint8_t& motion, uint8_t& obstruction, uint8_t& motor, uint16_t& openings)
void RATGDOComponent::readRollingCode(bool& isStatus, uint8_t& door, uint8_t& light, uint8_t& lock, uint8_t& motion, uint8_t& obstruction, uint8_t& motor, uint16_t& openings, uint8_t& button)
{
uint32_t rolling = 0;
uint64_t fixed = 0;
@ -112,6 +112,7 @@ namespace ratgdo {
} else if (cmd == 0x284) {
motor = 1;
} else if (cmd == 0x280) {
button = bytes1 == 1
ESP_LOGD(TAG, "Pressed: %s", byte1 == 1 ? "pressed" : "released");
} else if (cmd == 0x48c) {
openings = (byte1 << 8) | byte2;
@ -294,6 +295,10 @@ namespace ratgdo {
sendMotionStatus();
this->motionState = MotionState::MOTION_STATE_CLEAR;
}
if (this->buttonState != this->previousButtonState) {
sendButtonStatus();
this->previousButtonState = this->buttonState;
}
if (this->openings != this->previousOpenings) {
sendOpenings();
this->previousOpenings = this->openings;
@ -350,6 +355,15 @@ namespace ratgdo {
}
}
void RATGDOComponent::sendButtonStatus()
{
ButtonState val = static_cast<ButtonState>(this->buttonState);
ESP_LOGD(TAG, "Button state %s", button_state_to_string(val));
for (auto* child : this->children_) {
child->on_button_state(val);
}
}
void RATGDOComponent::sendMotorStatus()
{
MotorState val = static_cast<MotorState>(this->motorState);

View File

@ -94,6 +94,7 @@ namespace ratgdo {
uint8_t previousLockState { LockState::LOCK_STATE_UNKNOWN };
uint8_t previousObstructionState { ObstructionState::OBSTRUCTION_STATE_UNKNOWN };
uint8_t previousMotorState { MotorState::MOTOR_STATE_UNKNOWN };
uint8_t previousButtonState { ButtonState::BUTTON_STATE_UNKNOWN };
uint8_t obstructionState { ObstructionState::OBSTRUCTION_STATE_UNKNOWN };
uint8_t motionState { MotionState::MOTION_STATE_CLEAR };
@ -101,6 +102,7 @@ namespace ratgdo {
uint8_t lockState { LockState::LOCK_STATE_UNKNOWN };
uint8_t lightState { LightState::LIGHT_STATE_UNKNOWN };
uint8_t doorState { DoorState::DOOR_STATE_UNKNOWN };
uint8_t buttonState { ButtonState::BUTTON_STATE_UNKNOWN };
void set_output_gdo_pin(InternalGPIOPin* pin) { this->output_gdo_pin_ = pin; };
void set_input_gdo_pin(InternalGPIOPin* pin) { this->input_gdo_pin_ = pin; };
@ -132,7 +134,7 @@ namespace ratgdo {
void lock();
void unlock();
void sendLockStatus();
void sendButtonStatus();
void sendMotionStatus();
void sendMotorStatus();
void query();
@ -141,7 +143,7 @@ namespace ratgdo {
void getRollingCode(cmd command);
void gdoStateLoop();
void statusUpdateLoop();
void readRollingCode(bool& isStatus, uint8_t& door, uint8_t& light, uint8_t& lock, uint8_t& motion, uint8_t& obstruction, uint8_t& motor, uint16_t& openings);
void readRollingCode(bool& isStatus, uint8_t& door, uint8_t& light, uint8_t& lock, uint8_t& motion, uint8_t& obstruction, uint8_t& motor, uint16_t& openings, uint8_t& button);
void incrementRollingCodeCounter();
void sendRollingCodeChanged();
void setRollingCodeCounter(uint32_t counter);

View File

@ -87,5 +87,18 @@ namespace ratgdo {
}
}
const char* button_state_to_string(ButtonState state)
{
switch (state) {
case BUTTON_STATE_PRESSED:
return "PRESSED";
case BUTTON_STATE_RELEASED:
return "RELEASED";
case BUTTON_STATE_UNKNOWN:
default:
return "UNKNOWN";
}
}
} // namespace ratgdo
} // namespace esphome

View File

@ -70,5 +70,13 @@ namespace ratgdo {
};
const char* motor_state_to_string(MotorState state);
/// Enum for all states the button can be in.
enum ButtonState : uint8_t {
BUTTON_STATE_PRESSED = 0,
BUTTON_STATE_RELEASED = 1,
BUTTON_STATE_UNKNOWN = 2,
};
const char* button_state_to_string(ButtonState state);
} // namespace ratgdo
} // namespace esphome