diff --git a/components/ratgdo/binary_sensor/__init__.py b/components/ratgdo/binary_sensor/__init__.py index 6e924a3..91a215c 100644 --- a/components/ratgdo/binary_sensor/__init__.py +++ b/components/ratgdo/binary_sensor/__init__.py @@ -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, } diff --git a/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp b/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp index cb235f2..abb14dd 100644 --- a/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp +++ b/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp @@ -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 diff --git a/components/ratgdo/binary_sensor/ratgdo_binary_sensor.h b/components/ratgdo/binary_sensor/ratgdo_binary_sensor.h index f331cb7..f9989f7 100644 --- a/components/ratgdo/binary_sensor/ratgdo_binary_sensor.h +++ b/components/ratgdo/binary_sensor/ratgdo_binary_sensor.h @@ -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_; diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index a31aa5b..16a3a53 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -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(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(this->motorState); diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index 219cfed..792473c 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -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); diff --git a/components/ratgdo/ratgdo_state.cpp b/components/ratgdo/ratgdo_state.cpp index df04284..d985e82 100644 --- a/components/ratgdo/ratgdo_state.cpp +++ b/components/ratgdo/ratgdo_state.cpp @@ -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 diff --git a/components/ratgdo/ratgdo_state.h b/components/ratgdo/ratgdo_state.h index 169dc8a..294f25d 100644 --- a/components/ratgdo/ratgdo_state.h +++ b/components/ratgdo/ratgdo_state.h @@ -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 \ No newline at end of file