Skip to content

Add support for QOS Feature - #243

Open
ismdcf wants to merge 2 commits into
dmamontov:mainfrom
ismdcf:main
Open

Add support for QOS Feature#243
ismdcf wants to merge 2 commits into
dmamontov:mainfrom
ismdcf:main

Conversation

@ismdcf

@ismdcf ismdcf commented Jul 14, 2024

Copy link
Copy Markdown

Added ability to turn on and off the QOS feature.

Summary by Sourcery

This pull request introduces support for the QOS feature, allowing it to be toggled on and off. It includes updates to switch entity descriptions, availability checks, and adds corresponding test cases and fixtures.

  • New Features:
    • Added support for toggling the QOS feature on and off.
  • Enhancements:
    • Updated switch entity descriptions to include QOS feature.
    • Modified the availability check to account for the QOS feature.
  • Tests:
    • Added test fixtures and test cases for the QOS feature.

@sourcery-ai

sourcery-ai Bot commented Jul 14, 2024

Copy link
Copy Markdown

Reviewer's Guide by Sourcery

This pull request adds support for the QOS feature, allowing it to be toggled on and off. The implementation includes new switch entity descriptions, API methods for toggling and fetching QOS data, and necessary data preparation methods. Additionally, tests and fixture data have been added to ensure the functionality works as expected.

File-Level Changes

Files Changes
custom_components/miwifi/switch.py
custom_components/miwifi/luci.py
custom_components/miwifi/updater.py
custom_components/miwifi/const.py
Implemented QOS feature support including switch entity descriptions, API methods, and data preparation.
tests/test_switch.py
tests/fixtures/qos_data.json
Added tests and fixture data for QOS feature.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey @ismdcf - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 8 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 3 issues found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

f"{ATTR_SWITCH_WIFI_5_0_GAME}_{STATE_OFF}": "mdi:wifi-off",
f"{ATTR_SWITCH_WIFI_GUEST}_{STATE_ON}": "mdi:wifi-lock-open",
f"{ATTR_SWITCH_WIFI_GUEST}_{STATE_OFF}": "mdi:wifi-off",
f"{ATTR_SWITCH_QOS}_{STATE_ON}": "mdi:wifi-plus",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: Consider using a more descriptive icon for QoS.

The icons 'mdi:wifi-plus' and 'mdi:wifi-minus' might not clearly convey the QoS functionality to users. Consider using icons that more intuitively represent QoS, such as 'mdi:quality-high' and 'mdi:quality-low'.

Suggested change
f"{ATTR_SWITCH_QOS}_{STATE_ON}": "mdi:wifi-plus",
f"{ATTR_SWITCH_QOS}_{STATE_ON}": "mdi:quality-high",
f"{ATTR_SWITCH_QOS}_{STATE_OFF}": "mdi:quality-low",

)

is_available: bool = self._additional_prepare() and len(wifi_data) > 0
is_available: bool = False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nitpick: Redundant initialization of 'is_available'.

The variable 'is_available' is initialized to 'False' but is immediately reassigned in the following lines. Consider removing the initial assignment to avoid redundancy.

Comment on lines +179 to +181
if self.entity_description.key in [
ATTR_SWITCH_QOS,
]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: Simplify the condition check for QoS.

The condition check for 'self.entity_description.key' can be simplified to 'if self.entity_description.key == ATTR_SWITCH_QOS:'. This makes the code more readable and avoids unnecessary list creation.

Suggested change
if self.entity_description.key in [
ATTR_SWITCH_QOS,
]:
if self.entity_description.key == ATTR_SWITCH_QOS:

async def _async_update_qos(self, state: int = 1) -> None:
"""Update QOS

:param data: dict: QOS data

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue: Incorrect parameter description in docstring.

The docstring for '_async_update_qos' mentions a 'data' parameter, but the method actually takes a 'state' parameter. Update the docstring to reflect the correct parameter.


self.async_write_ha_state()

def _data_not_changed(self, wifi_data: dict) -> bool:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: Improve method name for clarity.

The method name '_data_not_changed' could be more descriptive. Consider renaming it to '_is_data_unchanged' to better convey its purpose.

Suggested change
def _data_not_changed(self, wifi_data: dict) -> bool:
def _is_data_unchanged(self, wifi_data: dict) -> bool:

:return dict: dict with api data.
"""

return await self.get("misystem/qos_switch", {"on": qosState})

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

question (bug_risk): Potential issue with API parameter naming.

Ensure that the API endpoint 'misystem/qos_switch' expects the parameter 'on' with the value of 'qosState'. If the API expects a different parameter name, this could lead to unexpected behavior.


response: dict = await self.luci.qos_info()

data[ATTR_SWITCH_QOS] = response["status"]["on"] == 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue: Handle potential missing 'status' key in response.

The line assumes that 'response["status"]' always exists. Consider adding a check to handle cases where 'status' might be missing from the response to avoid potential KeyError exceptions.

Comment thread tests/test_switch.py
assert len(mock_luci_client.mock_calls) == _prev_calls + 4

#
# @pytest.mark.asyncio

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (testing): Uncomment the test for QOS feature

The test for the QOS feature is currently commented out. Please uncomment it and ensure it runs successfully to validate the new functionality.

Comment thread tests/test_switch.py
# def success_set_qos(data: dict) -> dict:
# return {"code": 0}
#
# def error_set_qos(data: dict) -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (testing): Add test for QOS toggle error handling

Currently, the test does not verify the behavior when the QOS toggle fails. Please add assertions to check that the system handles the LuciRequestError correctly.

Suggested change
# def error_set_qos(data: dict) -> None:
def test_qos_toggle_error_handling():
with pytest.raises(LuciRequestError):
error_set_qos({})

Comment thread tests/test_switch.py
#
# _prev_calls: int = len(mock_luci_client.mock_calls)
#
# assert await hass.services.async_call(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (testing): Check for service call failures

Add assertions to check for failures in the service calls to ensure that the system handles them gracefully.

Suggested change
# assert await hass.services.async_call(
# result = await hass.services.async_call(
# SWITCH_DOMAIN,
# SERVICE_TURN_ON,
# blocking=True,
# return_response=True,
# )
# assert result['success'], f"Service call failed: {result}"

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.

1 participant