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

Show new errors from the MotionMount #137006

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from

Conversation

RJPoelstra
Copy link
Contributor

Proposed change

This change updates the 'error sensor' that can now show the new errors that are generated by the MotionMount

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.

To help with the load of incoming pull requests:

Copy link
Member

@andrewsayre andrewsayre left a comment

Choose a reason for hiding this comment

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

Looks good, however, would you please add some tests to cover these changes? You'll need to cover around 82% for the checks to pass, which you should be able to do by testing the entity state.

@home-assistant home-assistant bot marked this pull request as draft January 31, 2025 16:46
@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.

@RJPoelstra
Copy link
Contributor Author

RJPoelstra commented Jan 31, 2025

Hi @andrewsayre, thanks for your review.
Is it oké if I add the tests later? There are currently only the required tests for the config flow, but I intend to add 100% coverage after getting the Bronze quality scale. I’ve already prepared a PR for bronze, so I think full test coverage will only take a week or 2.
Adding all required boiler plate for the entity tests feels a bit out of scope for this simple PR.

@andrewsayre
Copy link
Member

andrewsayre commented Jan 31, 2025

I'd rather see the start of testing here vs. it all coming in (a presumably large PR) later, even if it requires adding test setup/mocking code now. I don't consider that out of scope. 👍

@andrewsayre
Copy link
Member

To clarify, for the CI to pass, you only need to add tests for the changes, not the entire integration. A test that asserts the entity state should be all that's needed. 🙂

@RJPoelstra
Copy link
Contributor Author

As of yet I’ve no idea how to add any entity test, so this is going to take some considerable time to add.

@andrewsayre
Copy link
Member

Yeah, I hear it can feel daunting when it's new. Would you like to take a stab at it first? Otherwise I'd be happy to get a test started.

Here’s an example test for the state: https://github.com/home-assistant/core/blob/dev/tests/components/heos/test_media_player.py#L73

The most work is behind the scenes mocking out the API calls with data. The config flow tests should already do this though.

@RJPoelstra
Copy link
Contributor Author

I'll give it a try.
If I'm stuck I'll let you know :)

@RJPoelstra RJPoelstra marked this pull request as ready for review February 3, 2025 13:19
@home-assistant home-assistant bot requested a review from andrewsayre February 3, 2025 13:19
Copy link
Member

@andrewsayre andrewsayre left a comment

Choose a reason for hiding this comment

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

Great job on the tests! A few comments to look at! Let me know if you have any questions!

homeassistant/components/motionmount/sensor.py Outdated Show resolved Hide resolved
Comment on lines 24 to 28
type(mock_motionmount_config_flow).name = PropertyMock(return_value=ZEROCONF_NAME)
type(mock_motionmount_config_flow).mac = PropertyMock(return_value=MAC)
type(mock_motionmount_config_flow).is_authenticated = PropertyMock(
return_value=True
)
Copy link
Member

@andrewsayre andrewsayre Feb 3, 2025

Choose a reason for hiding this comment

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

Instead of patching the type, patch the location where motionmount is imported so new instances are MagicMocks. Then you can use the mock object to set the values needed directly. Something similar to this:

with patch(
        "homeassistant.components.motionmount.motionmount.MotionMount",
        autospec=True,
    ) as motionmount_mock:
   motionmount_mock.name.return_value = ZEROCONF_NAME
   ...
   assert await hass.config_entries.async_setup(mock_config_entry.entry_id)

I think most integrations move this into a fixture generator, similar to mock_motionmount_config_flow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've got it working using the patch.
I tried updating the generator, but I can't wrap my head around it.

tests/components/motionmount/test_sensor.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft February 3, 2025 18:56
@RJPoelstra RJPoelstra marked this pull request as ready for review February 4, 2025 12:12
@home-assistant home-assistant bot requested a review from andrewsayre February 4, 2025 12:12
Copy link
Member

@andrewsayre andrewsayre left a comment

Choose a reason for hiding this comment

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

Looking good! A few minor change and this will be ready!

@@ -9,6 +10,14 @@
from . import MotionMountConfigEntry
from .entity import MotionMountEntity

error_messages = {
Copy link
Member

Choose a reason for hiding this comment

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

Module-level consts should be all-caps (and optionally denoted as Final):

Suggested change
error_messages = {
from typing import Final
ERROR_MESSAGES: Final = {


return "internal"
for error, message in error_messages.items():
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for error, message in error_messages.items():
for error, message in ERROR_MESSAGES.items():



@pytest.mark.parametrize(
"system_status",
Copy link
Member

Choose a reason for hiding this comment

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

Parameterize supports multiple arguments and will split the tuple into the args for you automatically:

Suggested change
"system_status",
("system", "status"),

async def test_error_status_sensor_states(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
system_status: (MotionMountSystemError, str),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
system_status: (MotionMountSystemError, str),
status: MotionMountSystemError,
state: str,

system_status: (MotionMountSystemError, str),
) -> None:
"""Tests the state attributes."""
(status, state) = system_status
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
(status, state) = system_status

@home-assistant home-assistant bot marked this pull request as draft February 4, 2025 15:45
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.

3 participants