fix: update switch for JSON path syntax in async_setup_entry for array handling, refs #386#387
Conversation
📝 WalkthroughWalkthroughThe REST array-handling logic in the timer switch setup was updated to use bracket-style index placeholders ( ChangesPlaceholder Syntax Update
Estimated code review effort: 1 (Trivial) | ~5 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@custom_components/robonect/switch.py`:
- Around line 128-132: The switch setup logic in the robonect switch handling
should guard the result of get_json_dict_path() before iterating. In the code
that builds items from description.array, add the same missing-path check used
elsewhere so that if the array lookup returns None, the setup skips that entry
instead of calling enumerate on it. Use the existing description.array block and
the array variable in this switch path to place the guard immediately before the
loop.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 22ddb6b8-ba3e-43be-bca3-b3c882d903bd
📒 Files selected for processing (1)
custom_components/robonect/switch.py
| if description.array: | ||
| array = get_json_dict_path( | ||
| coordinator.data, description.rest_attrs.replace(".0", "") | ||
| coordinator.data, description.rest_attrs.replace("[0]", "") | ||
| ) | ||
| for idx, item in enumerate(array): |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '\n## File sizes\n'
wc -l custom_components/robonect/utils.py custom_components/robonect/sensor.py custom_components/robonect/switch.py
printf '\n## utils.py: get_json_dict_path\n'
rg -n "def get_json_dict_path|return None|matches" custom_components/robonect/utils.py -A6 -B4
printf '\n## sensor.py: array handling\n'
rg -n "array = get_json_dict_path|if array is None|enumerate\\(array\\)" custom_components/robonect/sensor.py -A6 -B6
printf '\n## switch.py: array handling\n'
rg -n "array = get_json_dict_path|if array is None|enumerate\\(array\\)" custom_components/robonect/switch.py -A6 -B6Repository: geertmeersman/robonect
Length of output: 3991
Guard array before iterating
get_json_dict_path() returns None when the array path is missing, so enumerate(array) can raise a TypeError here and abort switch setup. The sensor path already skips this case; add the same if array is None: continue check before the loop.
🐛 Proposed fix
if description.array:
array = get_json_dict_path(
coordinator.data, description.rest_attrs.replace("[0]", "")
)
+ if array is None:
+ continue
for idx, item in enumerate(array):📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if description.array: | |
| array = get_json_dict_path( | |
| coordinator.data, description.rest_attrs.replace(".0", "") | |
| coordinator.data, description.rest_attrs.replace("[0]", "") | |
| ) | |
| for idx, item in enumerate(array): | |
| if description.array: | |
| array = get_json_dict_path( | |
| coordinator.data, description.rest_attrs.replace("[0]", "") | |
| ) | |
| if array is None: | |
| continue | |
| for idx, item in enumerate(array): |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@custom_components/robonect/switch.py` around lines 128 - 132, The switch
setup logic in the robonect switch handling should guard the result of
get_json_dict_path() before iterating. In the code that builds items from
description.array, add the same missing-path check used elsewhere so that if the
array lookup returns None, the setup skips that entry instead of calling
enumerate on it. Use the existing description.array block and the array variable
in this switch path to place the guard immediately before the loop.
Summary by CodeRabbit