GuyKh / iec-custom-component

HomeAssistant Custom Component for IEC
MIT License
131 stars 5 forks source link

If device is not supported, gracefully ignore it #184 #185

Closed elad-bar closed 1 month ago

elad-bar commented 1 month ago

PR Type

bug_fix


Description


Changes walkthrough ๐Ÿ“

Relevant files
Bug fix
coordinator.py
Gracefully handle unsupported devices in billing estimation

custom_components/iec/coordinator.py
  • Added a check for counter_devices being not None before accessing its
    elements.
  • Refactored code to handle unsupported devices gracefully.
  • Improved error handling by logging a warning and falling back to an
    alternative API.
  • +12/-5   

    ๐Ÿ’ก PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    codiumai-pr-agent-pro[bot] commented 1 month ago

    PR Reviewer Guide ๐Ÿ”

    Here are some key observations to aid the review process:

    โฑ๏ธ Estimated effort to review: 2 ๐Ÿ”ต๐Ÿ”ตโšชโšชโšช
    ๐Ÿงช No relevant tests
    ๐Ÿ”’ No security concerns identified
    โšก Recommended focus areas for review

    Error Handling
    The new code adds a check for `counter_devices` being not None, but doesn't handle the case where it might be an empty list. This could potentially lead to an IndexError when accessing `counter_devices[0]`. Code Duplication
    The `connection_size` variable is assigned twice, which seems unnecessary and might lead to confusion. The second assignment on line 642 overwrites the first one on line 637.
    codiumai-pr-agent-pro[bot] commented 1 month ago

    PR Code Suggestions โœจ

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Check for empty list before accessing its first element to prevent potential errors ___ **Consider adding a check for empty counter_devices list before accessing its first
    element to prevent potential IndexError.** [custom_components/iec/coordinator.py [635-642]](https://github.com/GuyKh/iec-custom-component/pull/185/files#diff-6ce6bbf13b6589ae1f4611633e1444dd2f21e3bf539c5cc80f3997b7ef9b40b5R635-R642) ```diff -if counter_devices is not None: +if counter_devices and len(counter_devices) > 0: first_counter_device = counter_devices[0] connection_size = first_counter_device.connection_size last_meter_read = int(first_counter_device.last_mr) last_meter_read_date = first_counter_device.last_mr_date phase_count = first_counter_device.connection_size.phase connection_size = connection_size.representative_connection_size ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 9 Why: This suggestion is highly relevant as it addresses a potential IndexError by checking if the list is empty before accessing its first element. This enhances the robustness of the code and aligns with the PR's aim to improve error handling.
    9

    ๐Ÿ’ก Need additional feedback ? start a PR chat

    elad-bar commented 1 month ago

    test it locally, works fine

    not sure if it works like that in the iec client, but each of the response from iec api has a block of status (reponseDescriptor.isSuccess), maybe have a warning log when it returns an issue will be easier to debug, if that's something you would like to have, i can try to do the and suggest another PR

    {
        "data": {
            "counterDevices": null,
            "mrType": "00"
        },
        "reponseDescriptor": {
            "isSuccess": false,
            "code": "08",
            "description": "No authorization for this product: PV Product"
        }
    }
    elad-bar commented 1 month ago

    will fix the lint error

    elad-bar commented 1 month ago

    Will wait for your release