Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds "Relay n (API failsafe only)" to button's actions #63

Merged
merged 3 commits into from
Dec 24, 2024

Conversation

edwardtfn
Copy link
Owner

@edwardtfn edwardtfn commented Dec 23, 2024

Solves #23

Summary by CodeRabbit

  • New Features
    • Added failsafe action text options for four buttons in the configuration.
    • Introduced a new Wi-Fi component for enhanced connectivity management.
  • Improvements
    • Enhanced button action definitions with failsafe options for better flexibility.
    • Streamlined logging and connection checks for relay toggling actions.
    • Improved clarity in operational flow with additional logging for relay actions.

Copy link
Contributor

coderabbitai bot commented Dec 23, 2024

Walkthrough

The pull request introduces enhancements to the ESPHome configuration for button actions and relay toggling. In the TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml file, four new substitutions are added to define failsafe action texts for each button, providing descriptive labels for these actions in API interactions. The button action select entities are updated to include the new failsafe action texts, increasing the configuration's flexibility. Additionally, the relay toggling logic is modified to check the API connection state, improving robustness and clarity in logging actions.

Changes

File Changes
ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml - Added 4 new substitutions for button failsafe texts
- Updated button action select entity options to include failsafe texts
ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml - Modified button_click_event script to check API connection state
- Updated relay toggling logic to include failsafe action checks
- Enhanced logging for relay toggle actions
ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml - Added new Wi-Fi component wifi_component

Possibly related PRs


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c4b2b89 and a44e486.

📒 Files selected for processing (2)
  • ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml (1 hunks)
  • ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml
🔇 Additional comments (2)
ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml (2)

211-213: Review complete Wi-Fi configuration.

The Wi-Fi configuration appears to be truncated. Please ensure the following are properly configured:

  • SSID and password handling
  • Network security settings
  • Fallback AP configuration
  • Power saving settings

211-211: Verify the Wi-Fi component's integration with failsafe functionality.

The addition of wifi_component ID is likely used for the new "Relay n (API failsafe only)" feature. Let's verify its usage in the relay configuration.

✅ Verification successful

Let me gather more context about how the wifi_component is being used in the relay configuration.


The wifi_component ID is correctly integrated with the failsafe functionality

The verification shows that:

  • The wifi_component ID is properly used in the relay configuration (ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml) to check Wi-Fi connectivity status
  • It's used alongside API server connection check to implement the failsafe functionality
  • The code properly handles disconnected states by checking !wifi_component->is_connected()
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for references to wifi_component in other yaml files
rg "wifi_component" --type yaml

# Specifically look for failsafe-related code using this component
ast-grep --pattern 'id: wifi_component'

Length of output: 331


Script:

#!/bin/bash
# Get the context around the wifi_component usage in the relay file
rg -B 5 -A 5 "wifi_component" ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml

# Also check the complete wifi configuration section
rg -B 5 -A 5 "wifi_component" ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml

Length of output: 720


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@edwardtfn edwardtfn linked an issue Dec 23, 2024 that may be closed by this pull request
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml (1)

208-243: Consider refactoring the relay toggle logic for better maintainability.

While the implementation is functionally correct, the code structure could be improved to reduce repetition and enhance maintainability.

Consider refactoring using a map or helper function:

- switch (button) {
-   case ${BUTTON_1_ID}:
-     select_state = sl_button_1_action->state;
-     toggle_relay = (select_state == "${BUTTON_1_ACTION_TEXT}" or
-                     (api_disconnected and select_state == "${BUTTON_1_ACTION_FAILSAFE_TEXT}"));
-     if (toggle_relay)
-       sw_relay_1->toggle();
-     break;
-   // ... similar cases for buttons 2-4
- }
+ const std::map<uint8_t, std::tuple<select::Select*, switch_::Switch*, const char*, const char*>> button_map = {
+   {${BUTTON_1_ID}, {sl_button_1_action, sw_relay_1, "${BUTTON_1_ACTION_TEXT}", "${BUTTON_1_ACTION_FAILSAFE_TEXT}"}},
+   {${BUTTON_2_ID}, {sl_button_2_action, sw_relay_2, "${BUTTON_2_ACTION_TEXT}", "${BUTTON_2_ACTION_FAILSAFE_TEXT}"}},
+   {${BUTTON_3_ID}, {sl_button_3_action, sw_relay_3, "${BUTTON_3_ACTION_TEXT}", "${BUTTON_3_ACTION_FAILSAFE_TEXT}"}},
+   {${BUTTON_4_ID}, {sl_button_4_action, sw_relay_4, "${BUTTON_4_ACTION_TEXT}", "${BUTTON_4_ACTION_FAILSAFE_TEXT}"}}
+ };
+ 
+ if (auto it = button_map.find(button); it != button_map.end()) {
+   auto [action_select, relay_switch, action_text, failsafe_text] = it->second;
+   select_state = action_select->state;
+   toggle_relay = (select_state == action_text or
+                   (api_disconnected and select_state == failsafe_text));
+   if (toggle_relay)
+     relay_switch->toggle();
+ }

This refactoring:

  • Reduces code duplication
  • Makes it easier to maintain and modify button-relay mappings
  • Centralizes the toggle logic
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ec528ec and 60a3248.

📒 Files selected for processing (2)
  • ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml (5 hunks)
  • ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml (1 hunks)
🔇 Additional comments (3)
ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml (2)

22-25: LGTM! Well-structured failsafe action definitions.

The new substitutions follow consistent naming patterns and clearly indicate their failsafe nature.


296-296: LGTM! Consistent implementation of failsafe options.

The failsafe options are properly added to each button's action select component, maintaining consistency across all buttons.

Also applies to: 311-311, 321-321, 331-331

ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml (1)

194-205: LGTM! Robust API connection state handling.

The implementation properly checks the API connection state and provides appropriate logging levels for different states.

@edwardtfn
Copy link
Owner Author

Actionable comments posted: 0

🧹 Nitpick comments (1)

ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml (1)> 208-243: Consider refactoring the relay toggle logic for better maintainability.

While the implementation is functionally correct, the code structure could be improved to reduce repetition and enhance maintainability.
Consider refactoring using a map or helper function:

- switch (button) {
-   case ${BUTTON_1_ID}:
-     select_state = sl_button_1_action->state;
-     toggle_relay = (select_state == "${BUTTON_1_ACTION_TEXT}" or
-                     (api_disconnected and select_state == "${BUTTON_1_ACTION_FAILSAFE_TEXT}"));
-     if (toggle_relay)
-       sw_relay_1->toggle();
-     break;
-   // ... similar cases for buttons 2-4
- }
+ const std::map<uint8_t, std::tuple<select::Select*, switch_::Switch*, const char*, const char*>> button_map = {
+   {${BUTTON_1_ID}, {sl_button_1_action, sw_relay_1, "${BUTTON_1_ACTION_TEXT}", "${BUTTON_1_ACTION_FAILSAFE_TEXT}"}},
+   {${BUTTON_2_ID}, {sl_button_2_action, sw_relay_2, "${BUTTON_2_ACTION_TEXT}", "${BUTTON_2_ACTION_FAILSAFE_TEXT}"}},
+   {${BUTTON_3_ID}, {sl_button_3_action, sw_relay_3, "${BUTTON_3_ACTION_TEXT}", "${BUTTON_3_ACTION_FAILSAFE_TEXT}"}},
+   {${BUTTON_4_ID}, {sl_button_4_action, sw_relay_4, "${BUTTON_4_ACTION_TEXT}", "${BUTTON_4_ACTION_FAILSAFE_TEXT}"}}
+ };
+ 
+ if (auto it = button_map.find(button); it != button_map.end()) {
+   auto [action_select, relay_switch, action_text, failsafe_text] = it->second;
+   select_state = action_select->state;
+   toggle_relay = (select_state == action_text or
+                   (api_disconnected and select_state == failsafe_text));
+   if (toggle_relay)
+     relay_switch->toggle();
+ }

This refactoring:

  • Reduces code duplication
  • Makes it easier to maintain and modify button-relay mappings
  • Centralizes the toggle logic

📜 Review details
Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro

📥 Commits
Reviewing files that changed from the base of the PR and between ec528ec and 60a3248.

📒 Files selected for processing (2)
🔇 Additional comments (3)

I liked the suggestion, but I'm not gonna implement this now as this triggers a warning in ESPHome compiler:

INFO ESPHome 2024.12.2
INFO Reading configuration /config/esphome/tx-ultimate-us.yaml...
WARNING GPIO5 is a strapping PIN and should only be used for I/O with care.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
WARNING GPIO2 is a strapping PIN and should only be used for I/O with care.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
WARNING GPIO15 is a strapping PIN and should only be used for I/O with care.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
INFO Detected timezone 'Europe/Stockholm'
INFO Generating C++ source...
INFO Compiling app...
Processing tx-ultimate-us (board: esp32dev; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5
Dependency Graph
|-- AsyncTCP-esphome @ 2.1.4
|-- WiFi @ 2.0.0
|-- FS @ 2.0.0
|-- Update @ 2.0.0
|-- ESPAsyncWebServer-esphome @ 3.2.2
|-- DNSServer @ 2.0.0
|-- ESPmDNS @ 2.0.0
|-- ArduinoJson @ 6.18.5
|-- NeoPixelBus @ 2.7.3
|-- WiFiClientSecure @ 2.0.0
|-- HTTPClient @ 2.0.0
|-- ESP32-audioI2S @ 2.0.7
|-- Improv @ 1.2.4
Compiling .pioenvs/tx-ultimate-us/src/main.cpp.o
/config/esphome/packages/edwardtfn/TX-Ultimate-Easy/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml: In lambda function:
/config/esphome/packages/edwardtfn/TX-Ultimate-Easy/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml:213:11: warning: init-statement in selection statements only available with -std=c++17 or -std=gnu++17
           if (auto it = button_map.find(button); it != button_map.end()) {
           ^~~~
/config/esphome/packages/edwardtfn/TX-Ultimate-Easy/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml:214:14: warning: structured bindings only available with -std=c++17 or -std=gnu++17
             auto [action_select, relay_switch, action_text, failsafe_text] = it->second;
              ^
Linking .pioenvs/tx-ultimate-us/firmware.elf
RAM:   [=         ]  14.3% (used 46780 bytes from 327680 bytes)
Flash: [====      ]  36.1% (used 1420617 bytes from 3932160 bytes)
Building .pioenvs/tx-ultimate-us/firmware.bin
Creating esp32 image...
Successfully created esp32 image.
esp32_create_combined_bin([".pioenvs/tx-ultimate-us/firmware.bin"], [".pioenvs/tx-ultimate-us/firmware.elf"])
Wrote 0x16c3d0 bytes to file /data/build/tx-ultimate-us/.pioenvs/tx-ultimate-us/firmware.factory.bin, ready to flash to offset 0x0
esp32_copy_ota_bin([".pioenvs/tx-ultimate-us/firmware.bin"], [".pioenvs/tx-ultimate-us/firmware.elf"])
========================= [SUCCESS] Took 35.29 seconds =========================

@edwardtfn edwardtfn merged commit 93990e7 into main Dec 24, 2024
10 checks passed
@edwardtfn edwardtfn deleted the button-failsafe-action branch December 24, 2024 22:22
This was referenced Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default actions if the switch is offline or HA is offline
1 participant