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

Add platform tests to enphase_envoy #114390

Open
wants to merge 47 commits into
base: dev
Choose a base branch
from

Conversation

catsmanac
Copy link
Contributor

Proposed change

Currently enphase_envoy integration has only tests for configflow and sensor platform. This pr adds tests for the used platforms which are missing tests.

The test data model is extended with Encharge/Enpower battery data and new tests are added for:

  • binary_sensor
  • number
  • select
  • switch

The existing sensor platform test is updated for the new battery sensors.

All snapshot are updated or newly provided.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @bdraco, @cgarwood, @dgomes, @joostlek, mind taking a look at this pull request as it has been labeled with an integration (enphase_envoy) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of enphase_envoy can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign enphase_envoy Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

Comment on lines 17 to 34
@pytest.fixture(name="setup_enphase_envoy_binary_sensor")
async def setup_enphase_envoy_binary_sensor_fixture(
hass: HomeAssistant, config: ConfigType, mock_envoy: AsyncMock
):
"""Define a fixture to set up Enphase Envoy with binary sensor platform only."""
with (
patch(
"homeassistant.components.enphase_envoy.Envoy",
return_value=mock_envoy,
),
patch(
"homeassistant.components.enphase_envoy.PLATFORMS",
[Platform.BINARY_SENSOR],
),
):
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
yield
Copy link
Member

Choose a reason for hiding this comment

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

I think it would make sense to make the Envoy mock a fixture (I mean it is already, but a fixture that patches the right packages automatically) and to make this whole a function.

A good example to look at is flexit_bacnet. They also use snapshot tests, and they have a nice way to setup the integration for specific platforms.

Also, we should not use the config: ConfigType here, since this isn't a YAML integration anymore, but flexit_bacnet is a good example to look from

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review and suggestion. Used the flexit_bacnet example to refactor this one.

# number entities states should be created from test data
assert len(hass.states.async_all()) == 4

entity_registry = er.async_get(hass)
Copy link
Member

Choose a reason for hiding this comment

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

This can be loaded in as a test fixture

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 52 to 56
entity_entries = er.async_entries_for_config_entry(
entity_registry, config_entry.entry_id
)
assert entity_entries
assert entity_entries == snapshot
Copy link
Member

Choose a reason for hiding this comment

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

I think it's good to have one snapshot entry per entity. With this you avoid a large diff when updating an entity, since this is an (unordered) list, adding an entity can make the list move all over the place, making the diff useless. The snapshot test in flexit_bacnet and analytics_insight is a good example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like that concept, the large ones don't really help finding what changes. Implemented.

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft March 29, 2024 08:26
@bdraco
Copy link
Member

bdraco commented Apr 14, 2024

It looks like there is a conflict now

@catsmanac
Copy link
Contributor Author

Yes, expected that with the other pr disabling the phase entities. I'll resolve that.

@catsmanac
Copy link
Contributor Author

Conflicts resolved, tests updated for disabled phase data and added/modified tests using entity_registry_enabled_by_default fixture.

Comment on lines 39 to 48
if entity_count == 0:
assert len(entity_entries) == 0
else:
# compare registered entities against snapshot of prior run
assert entity_entries
for entity_entry in entity_entries:
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
assert hass.states.get(entity_entry.entity_id) == snapshot(
name=f"{entity_entry.entity_id}-state"
)
Copy link
Member

Choose a reason for hiding this comment

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

We try to avoid using if statements in tests as much as possible as it makes it much harder to read and see what we are actually testing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed where possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants