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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if attributes are present in new_state before accessing them #71967

Merged
merged 4 commits into from May 25, 2022

Conversation

RoboMagus
Copy link
Contributor

Proposed change

Not every update that causes calc_integration to run is guaranteed to contain attributes. Example: when reloading template sensors, they may briefly exist in an unknown state.
This change first checks if attributes are present in the new state, before attempting to access them.
Fixes #71684.

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)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

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
  • The code has been formatted using Black (black --fast 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.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 馃 Silver
  • 馃 Gold
  • 馃弳 Platinum

To help with the load of incoming pull requests:

@probot-home-assistant
Copy link

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

if self._unit_of_measurement != new_unit_of_measurement:
self._unit_of_measurement = new_unit_of_measurement
update_state = True
if new_state.attributes:
Copy link
Contributor

@dgomes dgomes May 16, 2022

Choose a reason for hiding this comment

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

This will not cover the issue in #71684

AttributeError: 'NoneType' object has no attribute 'attributes'

Something like:

            if new_state and ATTR_UNIT_OF_MEASUREMENT in new_state.attributes:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah rats, you're right. I was under the impression that the lack of 'attributes' being present in the state was causing this issue.

In that case I think it's best to pull the None / Unknown handling of new_state that's already in there to the top, since anything else before the current location of that early return does depend on it.

@MartinHjelmare MartinHjelmare changed the title Check if attributes are present in new_state before accessing them. Check if attributes are present in new_state before accessing them May 16, 2022
@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label May 23, 2022
@RoboMagus
Copy link
Contributor Author

@dgomes, should the workflow be run again to be able to complete this merge?
(My first time contributing so I both don't know the requirements for this, nor do I have the permission to kick-off the workflow again).

@emontnemery
Copy link
Contributor

emontnemery commented May 24, 2022

@RoboMagus The change itself looks good, but can you please update the test case tests/components/integration/test_sensor.py::test_calc_errors to make sure your changes are covered?

Instead of waiting for CI, you can run the test and generate a coverage report locally:

pytest --cov=homeassistant/components/integration --cov-report html -- tests/components/integration

Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Please make sure the modified code is covered by tests

@RoboMagus
Copy link
Contributor Author

Thanks @emontnemery,

I had already checked if no tests were broken, but was unaware of the drop in coverage (and the target for it).
Should be good now.

@emontnemery emontnemery merged commit c1ddde3 into home-assistant:dev May 25, 2022
@RoboMagus RoboMagus deleted the integration_attributes_fix branch May 25, 2022 06:49
@github-actions github-actions bot locked and limited conversation to collaborators May 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bugfix cla-signed integration: integration small-pr PRs with less than 30 lines. smash Indicator this PR is close to finish for merging or closing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Frequent errors in Riemann Sum Integration
5 participants