Skip to content

Add Nuki battery percentage sensor#84968

Merged
MartinHjelmare merged 11 commits into
home-assistant:devfrom
Savjee:dev
Jan 8, 2023
Merged

Add Nuki battery percentage sensor#84968
MartinHjelmare merged 11 commits into
home-assistant:devfrom
Savjee:dev

Conversation

@Savjee
Copy link
Copy Markdown
Contributor

@Savjee Savjee commented Jan 2, 2023

Proposed change

Nuki's API now exposes the battery percentage. Integrate this with Home Assistant.

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

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.

To help with the load of incoming pull requests:

@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented Jan 2, 2023

Hey there @pschmitt, @pvizeli, @pree, mind taking a look at this pull request as it has been labeled with an integration (nuki) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of nuki can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Change the title of the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign nuki Removes the current integration label and assignees on the issue, add the integration domain after the command.

Copy link
Copy Markdown
Member

@Kane610 Kane610 left a comment

Choose a reason for hiding this comment

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

Minor comments

Comment thread homeassistant/components/nuki/sensor.py Outdated
Comment thread homeassistant/components/nuki/sensor.py Outdated
@Savjee
Copy link
Copy Markdown
Contributor Author

Savjee commented Jan 2, 2023

@Kane610 Thanks for the quick review and suggestions.
Made some changes, hope they're good now ;)

@gjohansson-ST
Copy link
Copy Markdown
Member

Missing test or add to coverage

@pree
Copy link
Copy Markdown
Member

pree commented Jan 2, 2023

There is already a PR waiting for review regarding the device registry (#79806)

@Savjee
Copy link
Copy Markdown
Contributor Author

Savjee commented Jan 2, 2023

Not sure how to proceed.
Do I remove my device registry changes to allow for PR #79806 to be merged?

@pree
Copy link
Copy Markdown
Member

pree commented Jan 2, 2023

When the other PR is merged you can merge the changes into your PR or vice versa. Just wanted to mention this :)

@flashouillette
Copy link
Copy Markdown

Hello The better update will be integration of the https:// and not only http://

@pree
Copy link
Copy Markdown
Member

pree commented Jan 3, 2023

Hello The better update will be integration of the https:// and not only http://

Sadly, the Nuki bridge doesn't support TLS.

Copy link
Copy Markdown
Contributor

@bbr111 bbr111 left a comment

Choose a reason for hiding this comment

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

tested in my local installation.
works - battery sensor is present hand shows percentage

@MartinHjelmare
Copy link
Copy Markdown
Member

Please remove the device registry related changes. Each PR should only do one thing.

@MartinHjelmare MartinHjelmare changed the title Nuki: add battery percentage + integrate with device registry Add Nuki battery percentage sensor Jan 7, 2023
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Per above.

@Savjee
Copy link
Copy Markdown
Contributor Author

Savjee commented Jan 7, 2023

@MartinHjelmare I removed the device registry changes.

@MartinHjelmare
Copy link
Copy Markdown
Member

Please either add tests or exclude the new module from coverage calculation in.coveragerc.

@Savjee
Copy link
Copy Markdown
Contributor Author

Savjee commented Jan 7, 2023

Done!

Comment thread homeassistant/components/nuki/sensor.py Outdated
@property
def name(self):
"""Return the name of the lock."""
return "Battery"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use the _attr_ attributes to set the constant properties. Eg instead of defining a name property we set the class attribute _attr_name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your feedback. I adapted the code.

Additional question: can I do the same for the ones that return data from the pynuki package?

Can this:

@property
def extra_state_attributes(self):
    """Return the device specific state attributes."""
    data = {
        ATTR_NUKI_ID: self._nuki_device.nuki_id,
    }
    return data

Be turned into this?

_attr_extra_state_attributes = {
  ATTR_NUKI_ID: self._nuki_device.nuki_id,
}

(Sorry, I'm new to HA development and haven't much experience with Python)

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare Jan 7, 2023

Choose a reason for hiding this comment

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

Yes, but then we need to add an __init__ method and set the attribute as an instance attribute on the entity as it's bad practice to set a mutable object as a class attribute.

Also, this will only work if the attribute won't need updating after the entity is created. If the attribute needs updating, we either need to do that in a state update callback or keep using a property that is read when the entity state is updated.

So further changes for this is optional.

Comment thread homeassistant/components/nuki/sensor.py Outdated
Comment thread homeassistant/components/nuki/sensor.py Outdated
Comment thread homeassistant/components/nuki/sensor.py Outdated
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare 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!

Please link a docs PR in the PR description. We need to add the sensor platform to the front matter of the docs page:
https://www.home-assistant.io/integrations/nuki/

@Savjee
Copy link
Copy Markdown
Contributor Author

Savjee commented Jan 7, 2023

Wow, didn't think of that! You guys are very thorough ;)
home-assistant/home-assistant.io#25633

@MartinHjelmare
Copy link
Copy Markdown
Member

Thanks!

@MartinHjelmare MartinHjelmare merged commit fc00c6d into home-assistant:dev Jan 8, 2023
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants