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

Downstream from Core Plugwise #728

Merged
merged 15 commits into from
Sep 24, 2024
Merged

Downstream from Core Plugwise #728

merged 15 commits into from
Sep 24, 2024

Conversation

bouwew
Copy link
Contributor

@bouwew bouwew commented Sep 24, 2024

Summary by CodeRabbit

  • Documentation

    • Updated docstrings for clarity in several test functions related to Plugwise integration.
  • Refactor

    • Improved type annotations for various test functions, enhancing readability and maintainability.
    • Consolidated multiple error scenario tests into a single parameterized test function for efficiency.
    • Streamlined the handling of entity registry access in tests, promoting a parameterized approach.
  • Style

    • Replaced platform references with explicit domain references in various tests for better clarity.
    • Updated import statements for consistency across test files.

Copy link
Contributor

coderabbitai bot commented Sep 24, 2024

Walkthrough

The changes involve updates to test files within the Plugwise integration, focusing on enhancing clarity through improved docstrings, adding type annotations, and refactoring how entity registries are accessed in tests. These modifications streamline the code and improve maintainability without altering the core functionality of the tests.

Changes

File Change Summary
tests/components/plugwise/test_binary_sensor.py Updated docstrings for two test functions to clarify their specific purposes related to Plugwise notification binary sensors.
tests/components/plugwise/test_button.py Streamlined import statements and updated service calls to enhance clarity and consistency in referencing button services.
tests/components/plugwise/test_climate.py Replaced hardcoded strings for climate services with constants for better maintainability and clarity.
tests/components/plugwise/test_config_flow.py Consolidated multiple test functions into a single parameterized test for invalid setups, added type annotations, and improved clarity regarding expected input and output types.
tests/components/plugwise/test_number.py Replaced Platform.NUMBER with NUMBER_DOMAIN in service calls for number entities, enhancing consistency.
tests/components/plugwise/test_select.py Renamed a parameter in one test function and updated the docstring to clarify the test's focus on legacy thermostat behavior.
tests/components/plugwise/test_sensor.py Refactored how the entity registry is accessed in tests, passing it as a parameter instead of retrieving it directly, promoting a more explicit approach.
tests/components/plugwise/test_switch.py Replaced Platform.SWITCH with SWITCH_DOMAIN in assertions, updated function signatures to include return type hints, and removed unnecessary imports for better clarity.

Possibly related PRs

Suggested labels

bug, quality, enhancement, needs_upstreaming, downstreaming

Suggested reviewers

  • CoMPaTech

🐇 In the code where bunnies hop,
Clarity blooms, we can't stop!
With types and docs so clear and bright,
Our tests will surely take flight!
A joyful leap, a happy cheer,
For better code, we hold dear! 🌼


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 055bb63 and 1c090a3.

Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • tests/components/plugwise/test_button.py (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/components/plugwise/test_button.py
Additional comments not posted (2)
CHANGELOG.md (2)

5-8: LGTM: New "Ongoing" section added correctly

The new "Ongoing" section has been added at the top of the changelog, following the established format. The entry about test-code improvements is clear and concise.


Line range hint 1-1038: Well-maintained and informative changelog

The CHANGELOG.md file is comprehensive, well-structured, and provides valuable insights into the development history of the Plugwise integration. It follows a consistent format, with clear version numbering and dates, making it easy for users and developers to track changes and improvements over time.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
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 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 or @coderabbitai title 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.

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: 1

Outside diff range and nitpick comments (3)
tests/components/plugwise/test_sensor.py (1)

52-54: LGTM: Function signature updated correctly.

The addition of entity_registry: er.EntityRegistry as a parameter is a good practice. It improves testability by allowing the entity registry to be mocked or controlled in tests.

Consider adding type hinting for the hass parameter:

async def test_unique_id_migration_humidity(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
    mock_smile_adam_4: MagicMock,
    mock_config_entry: MockConfigEntry,
) -> None:
tests/components/plugwise/test_switch.py (1)

163-166: LGTM with a minor suggestion: Function updated correctly.

The changes to test_unique_id_migration_plug_relay are well-implemented:

  • The new entity_registry parameter allows for better testing of entity registry operations.
  • The return type annotation is consistent with previous function updates.
  • The use of SWITCH_DOMAIN is consistent with other changes in the file.

Consider reformatting the function signature for better readability:

async def test_unique_id_migration_plug_relay(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
    mock_smile_adam: MagicMock,
    mock_config_entry: MockConfigEntry,
) -> None:

Also applies to: 173-173, 182-182

tests/components/plugwise/test_config_flow.py (1)

448-450: LGTM: Improved type annotations and formatting for test_form_cannot_connect_port

The addition of parameter type hints (hass: HomeAssistant, mock_smile: MagicMock) and the return type hint (-> None) enhances code clarity and supports static type checking. Splitting the function definition across multiple lines improves readability.

Consider aligning the closing parenthesis with the opening one for consistency:

async def test_form_cannot_connect_port(
    hass: HomeAssistant, mock_smile: MagicMock
) -> None:
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between dd8d1c9 and aa4f01d.

Files selected for processing (5)
  • tests/components/plugwise/test_binary_sensor.py (2 hunks)
  • tests/components/plugwise/test_config_flow.py (7 hunks)
  • tests/components/plugwise/test_select.py (1 hunks)
  • tests/components/plugwise/test_sensor.py (4 hunks)
  • tests/components/plugwise/test_switch.py (11 hunks)
Additional comments not posted (19)
tests/components/plugwise/test_select.py (1)

79-79: Great docstring improvement!

The updated docstring provides more specific information about the test's purpose, clarifying that it's for a legacy Anna without a thermostat schedule. This improvement enhances the test's documentation and makes its intent clearer for future developers.

tests/components/plugwise/test_binary_sensor.py (3)

58-58: Improved docstring clarity.

The updated docstring provides a more accurate description of the test's purpose, specifically mentioning that it tests a "climate related plugwise-notification binary_sensor". This change enhances the test's documentation and aligns well with the actual implementation.


71-71: Enhanced docstring specificity.

The updated docstring provides a more precise description of the test's purpose, explicitly mentioning that it tests a "Smile P1 related plugwise-notification binary_sensor". This change improves the test's documentation and accurately reflects the implementation details.


58-58: Summary: Improved test documentation for Plugwise binary sensors.

The changes in this file focus on updating the docstrings for two test functions:

  1. test_adam_climate_binary_sensor_change
  2. test_p1_v4_binary_sensor_entity

Both updates provide more specific and accurate descriptions of the binary sensors being tested. These changes enhance the overall clarity of the test suite and align well with the PR objective of updating downstream from Core Plugwise.

The modifications are purely documentary and do not affect the functionality of the tests. They contribute to better maintainability and understanding of the test suite for the Plugwise integration.

Also applies to: 71-71

tests/components/plugwise/test_sensor.py (3)

6-6: LGTM: Import statements updated correctly.

The changes in import statements are appropriate:

  1. Using SENSOR_DOMAIN from homeassistant.components.sensor is a good practice for consistency.
  2. Importing the entity registry module as er aligns with the updated function signatures and usage in the test functions.

These changes improve code readability and maintainability.

Also applies to: 9-9


Line range hint 61-65: LGTM: Entity creation calls updated correctly.

The use of SENSOR_DOMAIN instead of Platform.SENSOR in the entity creation calls is consistent with the updated import statement. This change improves code consistency without affecting functionality.

Also applies to: 70-74


148-151: LGTM: Function signature updated with improved type hinting.

The changes in the test_p1_3ph_dsmr_sensor_entities function signature are excellent:

  1. Adding entity_registry: er.EntityRegistry as a parameter aligns with the new approach of passing the entity registry.
  2. The addition of type hints for all parameters improves code readability and helps catch potential type-related errors.

These changes enhance the overall quality and maintainability of the test code.

tests/components/plugwise/test_switch.py (6)

9-9: LGTM: Import statements updated correctly.

The new import statements are consistent with the changes made in the test cases. The SWITCH_DOMAIN import improves clarity, and the er import is necessary for the updated test_unique_id_migration_plug_relay function.

Also applies to: 19-19


38-39: LGTM: Function signature updated with return type.

The addition of -> None to the function signature improves type hinting and explicitly indicates that the function doesn't return a value. This change is consistent with Python typing best practices.


45-46: LGTM: Service call assertions updated correctly.

The use of SWITCH_DOMAIN instead of Platform.SWITCH in the service call assertions is consistent with the new import statement. This change improves readability by using a more explicit domain reference for switch services.

Also applies to: 58-59


74-74: LGTM: Function signature updated with return type.

The addition of -> None to the function signature is consistent with the previous updates and improves type hinting by explicitly indicating that the function doesn't return a value.


75-76: LGTM: Service call assertions updated consistently.

The use of SWITCH_DOMAIN instead of Platform.SWITCH in all service call assertions is consistent with the previous updates. This change maintains consistency throughout the test file and improves readability.

Also applies to: 87-88, 99-100


Line range hint 1-205: Overall assessment: Excellent improvements to the test file.

The changes made to tests/components/plugwise/test_switch.py consistently enhance the code quality:

  1. Improved type hinting with return type annotations for all test functions.
  2. Consistent use of SWITCH_DOMAIN instead of Platform.SWITCH, improving clarity.
  3. Addition of the entity_registry parameter in test_unique_id_migration_plug_relay for better testing capabilities.

These modifications contribute to better code maintainability and readability. Great job on implementing these improvements!

tests/components/plugwise/test_config_flow.py (6)

103-103: LGTM: Improved type hinting for mock_smile function

The addition of the return type hint Generator[MagicMock] enhances code clarity and aids in static type checking. This change aligns with best practices for Python type annotations.


391-391: LGTM: Enhanced type annotations for test_form_invalid_setup

The addition of parameter type hints (hass: HomeAssistant, mock_smile: MagicMock) and the return type hint (-> None) improves code clarity and aids in static type checking. This change is in line with best practices for Python type annotations in test functions.


410-410: LGTM: Improved type annotations for test_form_invalid_auth

The addition of parameter type hints (hass: HomeAssistant, mock_smile: MagicMock) and the return type hint (-> None) enhances code clarity and supports static type checking. This change aligns with best practices for Python type annotations in test functions.


429-429: LGTM: Enhanced type annotations for test_form_cannot_connect

The addition of parameter type hints (hass: HomeAssistant, mock_smile: MagicMock) and the return type hint (-> None) improves code clarity and supports static type checking. This change is consistent with best practices for Python type annotations in test functions.


473-473: LGTM: Enhanced type annotations for test_form_other_problem

The addition of parameter type hints (hass: HomeAssistant, mock_smile: MagicMock) and the return type hint (-> None) improves code clarity and supports static type checking. This change is in line with best practices for Python type annotations in test functions.


Line range hint 1-524: Overall assessment: Improved type annotations across test functions

The changes in this file consistently add type hints to test functions, improving code clarity and enabling better static type checking. These modifications enhance the overall code quality without altering the functionality of the tests. The use of appropriate types (HomeAssistant, MagicMock, Generator) demonstrates a good understanding of the testing environment and the components being tested.

Great job on improving the type annotations throughout the test file. These changes will make the code more maintainable and less prone to type-related errors in the future.

@@ -74,7 +74,7 @@ async def test_adam_select_regulation_mode(


async def test_legacy_anna_select_entities(
hass: HomeAssistant, mock_smile_adam: MagicMock, init_integration: MockConfigEntry
hass: HomeAssistant, mock_smile_legacy_anna: MagicMock, init_integration: MockConfigEntry
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Issue Found: Incomplete mock object rename in test_select.py.

The test_legacy_anna_select_entities function still uses mock_smile_adam instead of mock_smile_legacy_anna. Please update the parameter name to ensure consistency and improve code clarity.

Analysis chain

LGTM! Verify mock object rename consistency.

The change from mock_smile_adam to mock_smile_legacy_anna improves code clarity. This rename aligns the mock object name with its actual purpose (testing legacy Anna).

Please run the following script to ensure the mock object rename is consistent across the codebase:


Action Required: Update the search command to correctly target Python files.

The previous search for mock_smile_adam using --type python failed because python is not a recognized file type in rg. Please use the correct type alias (py) or adjust the search pattern to include .py files.


Action Required: Verify the parameter rename within the specific test function.

To ensure that the mock_smile_adam mock has been correctly renamed to mock_smile_legacy_anna in the test_legacy_anna_select_entities function within test_select.py, please execute the following script:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining instances of 'mock_smile_adam' that might need updating.

# Test: Search for 'mock_smile_adam' in Python files. Expect: No results, or results that don't need changing.
rg --type python 'mock_smile_adam'

Length of output: 65


Script:

#!/bin/bash
# Description: Check for any remaining instances of 'mock_smile_adam' in Python files.

# Test: Search for 'mock_smile_adam' in files with .py extension.
rg 'mock_smile_adam' --glob '*.py'

Length of output: 7329


Script:

#!/bin/bash
# Description: Verify the parameter rename in the specific test function `test_legacy_anna_select_entities` within test_select.py.

# Test: Use ast-grep to find the function signature with the renamed parameter.
ast-grep --lang python --pattern $'def test_legacy_anna_select_entities(self, mock_smile_legacy_anna: MagicMock, init_integration: MockConfigEntry): $$$' tests/components/plugwise/test_select.py

Length of output: 195

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

Outside diff range and nitpick comments (2)
tests/components/plugwise/test_config_flow.py (2)

391-393: LGTM: Improved type hinting, consider adding return type

The addition of type annotations for the function parameters enhances type hinting, which is a good practice. To further improve it, consider adding the return type annotation -> None to the function signature.

async def test_form_cannot_connect_port(
    hass: HomeAssistant, mock_smile: MagicMock
) -> None:

This change would make the function signature consistent with other test functions in the file.


416-418: LGTM: Improved type hinting, consider adding return type

The addition of type annotations for the function parameters enhances type hinting, which is a good practice. To further improve it, consider adding the return type annotation -> None to the function signature.

async def test_options_flow_thermo(
    hass: HomeAssistant, mock_smile_anna_2: MagicMock
) -> None:

This change would make the function signature consistent with other test functions in the file.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f520390 and 7d063d1.

Files selected for processing (1)
  • tests/components/plugwise/test_config_flow.py (4 hunks)
Additional comments not posted (2)
tests/components/plugwise/test_config_flow.py (2)

2-2: LGTM: Improved type hinting

The addition of the Generator import and the return type annotation for the mock_smile fixture enhances type hinting. This change improves code readability and maintainability.

Also applies to: 103-103


Line range hint 1-448: Verify test coverage after consolidating error scenario tests

The changes in this file include the removal of several individual test functions for different error scenarios, with a single test_form_cannot_connect_port function now handling connection failures. While this consolidation can reduce code duplication, it's important to ensure that all previously covered error scenarios are still adequately tested.

Please verify that the test coverage for various error scenarios (e.g., invalid setup, invalid authentication, etc.) has not been reduced by this consolidation. Consider adding additional assertions or test cases within the remaining test functions to maintain comprehensive error handling coverage.

To help verify the test coverage, you can run the following command to check the coverage report for this specific file:

Review the coverage report to ensure that all critical code paths, especially those related to error handling, are still being exercised by the tests.

Copy link

sonarcloud bot commented Sep 24, 2024

@bouwew
Copy link
Contributor Author

bouwew commented Sep 24, 2024

@CoMPaTech I've removed 4 test-functions, they appear to be double.
Looking at the test-coverage they can indeed be removed.

Copy link
Member

@CoMPaTech CoMPaTech left a comment

Choose a reason for hiding this comment

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

In an earlier phase we had to manually test these, so I'm a bit lost why we can remove now (wasn't because of coverage of our own integration, more towards the testing with core if memory serves correctly, but if we don't need to lets scrap indeed.

@bouwew
Copy link
Contributor Author

bouwew commented Sep 24, 2024

Looking at Blame, the changes were done about 2 years ago. But I can't get clear why they were added.

I do think that the now removed single test-functions were implementing the same functionality as the test_flow_errors() function does.

@bouwew bouwew marked this pull request as ready for review September 24, 2024 13:26
@bouwew bouwew requested a review from a team as a code owner September 24, 2024 13:26
@bouwew bouwew merged commit b23ec2a into main Sep 24, 2024
8 checks passed
@bouwew bouwew deleted the test-typing branch September 24, 2024 13:37
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.

2 participants