From ef994dfc4e8ad8dc407bd4686b67625905b97cc0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 7 Jun 2023 21:40:07 -0500 Subject: [PATCH] motor --- base.yml | 6 ++++++ .../binary_sensor/ratgdo_binary_sensor.cpp | 14 +++++++++++++- .../ratgdo/binary_sensor/ratgdo_binary_sensor.h | 1 + components/ratgdo/ratgdo.cpp | 16 ++++++++++++++-- components/ratgdo/ratgdo.h | 1 + components/ratgdo/ratgdo_child.cpp | 1 + components/ratgdo/ratgdo_child.h | 1 + components/ratgdo/ratgdo_state.cpp | 14 ++++++++++++-- components/ratgdo/ratgdo_state.h | 6 ++++++ 9 files changed, 55 insertions(+), 5 deletions(-) diff --git a/base.yml b/base.yml index d0f1717..23b2b59 100644 --- a/base.yml +++ b/base.yml @@ -20,6 +20,12 @@ binary_sensor: ratgdo_id: ${id_prefix} name: "${friendly_name} Obstruction" device_class: problem + - platform: ratgdo + type: motor + id: ${id_prefix}_motor + ratgdo_id: ${id_prefix} + name: "${friendly_name} Motor" + device_class: running number: - platform: ratgdo diff --git a/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp b/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp index 4165189..70f2f11 100644 --- a/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp +++ b/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp @@ -16,7 +16,13 @@ namespace ratgdo { void RATGDOBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "RATGDO BinarySensor", this); - ESP_LOGCONFIG(TAG, " Type: %s", this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_MOTION ? "Motion" : "Obstruction"); + if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_MOTION) { + ESP_LOGCONFIG(TAG, " Type: Motion"); + } else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_OBSTRUCTION) { + ESP_LOGCONFIG(TAG, " Type: Obstruction"); + } else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_MOTOR) { + ESP_LOGCONFIG(TAG, " Type: Motor"); + } } void RATGDOBinarySensor::on_motion_state(MotionState state) { @@ -30,6 +36,12 @@ namespace ratgdo { if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_OBSTRUCTION) this->publish_state(state == ObstructionState::OBSTRUCTION_STATE_OBSTRUCTED); } + void RATGDOBinarySensor::on_motor_state(MotorState state) + { + ESP_LOGD(TAG, "name: %s this->type_:%d on_motor_state: %d", this->get_name(), this->binary_sensor_type_, state); + if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_MOTOR) + this->publish_state(state == MotorState::MOTOR_STATE_ON); + } } // 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 de782c4..bfc8f57 100644 --- a/components/ratgdo/binary_sensor/ratgdo_binary_sensor.h +++ b/components/ratgdo/binary_sensor/ratgdo_binary_sensor.h @@ -12,6 +12,7 @@ namespace ratgdo { enum SensorType { RATGDO_SENSOR_MOTION, RATGDO_SENSOR_OBSTRUCTION, + RATGDO_SENSOR_MOTOR }; class RATGDOBinarySensor : public binary_sensor::BinarySensor, public RATGDOClient, public Component { diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index 7ab4f04..8b04a8c 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -159,7 +159,7 @@ namespace ratgdo { ESP_LOGCONFIG(TAG, " Rolling Code Counter: %d", this->rollingCodeCounter); } - void RATGDOComponent::readRollingCode(uint8_t& door, uint8_t& light, uint8_t& lock, uint8_t& motion, uint8_t& obstruction) + void RATGDOComponent::readRollingCode(uint8_t& door, uint8_t& light, uint8_t& lock, uint8_t& motion, uint8_t& obstruction, uint8_t& motor) { uint32_t rolling = 0; uint64_t fixed = 0; @@ -184,6 +184,7 @@ namespace ratgdo { light = (byte2 >> 1) & 1; lock = byte2 & 1; motion = 0; // when the status message is read, reset motion state to 0|clear + motor = 0; // when the status message is read, reset motor state to 0|off // obstruction = (byte1 >> 6) & 1; // unreliable due to the time it takes to register an obstruction ESP_LOGD(TAG, "Door: %d Light: %d Lock: %d Motion: %d Obstruction: %d", door, light, lock, motion, obstruction); if (this->forceUpdate_) { @@ -198,6 +199,8 @@ namespace ratgdo { ESP_LOGD(TAG, "Light: %d (toggle)", light); } else if (cmd == 0x84) { ESP_LOGD(TAG, "Unknown 0x84"); + } else if (cmd == 0x284) { + motor = 1; } else if (cmd == 0x280) { ESP_LOGD(TAG, "Pressed: %s", byte1 == 1 ? "pressed" : "released"); } else if (cmd == 0x48c) { @@ -427,7 +430,7 @@ namespace ratgdo { msgStart = 0; byteCount = 0; - readRollingCode(this->doorState, this->lightState, this->lockState, this->motionState, this->obstructionState); + readRollingCode(this->doorState, this->lightState, this->lockState, this->motionState, this->obstructionState, this->motorState); } } } @@ -497,6 +500,15 @@ namespace ratgdo { } } + void RATGDOComponent::sendMotorStatus() + { + MotorState val = static_cast(this->MotorState); + ESP_LOGD(TAG, "Motor state %s", motor_state_to_string(val)); + for (auto* child : this->children_) { + child->on_motor_state(val); + } + } + void RATGDOComponent::sendObstructionStatus() { ObstructionState val = static_cast(this->obstructionState); diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index eae89fe..9904d3b 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -124,6 +124,7 @@ namespace ratgdo { void sendLockStatus(); void sendMotionStatus(); + void sendMotorStatus(); void query(); void doorStateLoop(); void dryContactLoop(); diff --git a/components/ratgdo/ratgdo_child.cpp b/components/ratgdo/ratgdo_child.cpp index eabe097..aea7b0a 100644 --- a/components/ratgdo/ratgdo_child.cpp +++ b/components/ratgdo/ratgdo_child.cpp @@ -11,6 +11,7 @@ namespace ratgdo { void RATGDOClient::on_lock_state(LockState state) {}; void RATGDOClient::on_motion_state(MotionState state) {}; void RATGDOClient::on_obstruction_state(ObstructionState state) {}; + void RATGDOClient::on_motor_state(MotorState state) {}; void RATGDOClient::on_rolling_code_change(uint32_t rollingCodeCounter) {}; } // namespace ratgdo diff --git a/components/ratgdo/ratgdo_child.h b/components/ratgdo/ratgdo_child.h index 801cb6e..3e9a509 100644 --- a/components/ratgdo/ratgdo_child.h +++ b/components/ratgdo/ratgdo_child.h @@ -18,6 +18,7 @@ namespace ratgdo { virtual void on_lock_state(LockState state); virtual void on_motion_state(MotionState state); virtual void on_obstruction_state(ObstructionState state); + virtual void on_motor_state(MotorState state); virtual void on_rolling_code_change(uint32_t rollingCodeCounter); protected: diff --git a/components/ratgdo/ratgdo_state.cpp b/components/ratgdo/ratgdo_state.cpp index 2d54479..3155f23 100644 --- a/components/ratgdo/ratgdo_state.cpp +++ b/components/ratgdo/ratgdo_state.cpp @@ -54,15 +54,25 @@ namespace ratgdo { const char* motion_state_to_string(MotionState state) { switch (state) { - case MOTION_STATE_CLEAR: - return "CLEAR"; case MOTION_STATE_DETECTED: return "DETECTED"; + case MOTION_STATE_CLEAR: default: return "CLEAR"; } } + const char* motor_state_to_string(MotorState state) + { + switch (state) { + case MOTOR_STATE_ON: + return "ON"; + case MOTOR_STATE_OFF: + default: + return "OFF"; + } + } + const char* obstruction_state_to_string(ObstructionState state) { switch (state) { diff --git a/components/ratgdo/ratgdo_state.h b/components/ratgdo/ratgdo_state.h index e06feb6..06e278c 100644 --- a/components/ratgdo/ratgdo_state.h +++ b/components/ratgdo/ratgdo_state.h @@ -62,5 +62,11 @@ namespace ratgdo { }; const char* obstruction_state_to_string(ObstructionState state); + /// Enum for all states a the motor can be in. + enum MotorState : uint8_t { + MOTOR_STATE_OFF = 0, + MOTOR_STATE_ON = 1, + }; + } // namespace ratgdo } // namespace esphome \ No newline at end of file