Skip to content

Add state_class to Nuki battery sensor#159756

Merged
jbouwh merged 1 commit into
home-assistant:devfrom
victor-gurbani:fix-nuki-battery-state-class
Dec 26, 2025
Merged

Add state_class to Nuki battery sensor#159756
jbouwh merged 1 commit into
home-assistant:devfrom
victor-gurbani:fix-nuki-battery-state-class

Conversation

@victor-gurbani
Copy link
Copy Markdown
Contributor

@victor-gurbani victor-gurbani commented Dec 25, 2025

Proposed change

This PR fixes the Nuki battery level sensor missing long-term statistics by setting the sensor’s state_class to "measurement".

Battery percentage is a value that benefits from being stored long-term. Without a state_class, the sensor is not eligible for long-term statistics, and users need to resort to manually customised overrides.

Type of change

  • Bugfix (non-breaking change which fixes an issue)

Additional information

  • This PR fixes or closes issue: N/A (no issue)
  • This PR is related to issue: N/A
  • Link to documentation pull request: N/A
  • Link to developer documentation pull request: N/A
  • Link to frontend pull request: N/A

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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][dev-checklist]
  • I have followed the [perfect PR recommendations][perfect-pr]
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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

  • Documentation added/updated for [www.home-assistant.io][docs-repository] (not needed)

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

  • The [manifest file][manifest-docs] has all fields filled out correctly. (not needed as manifest has not changed)
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt. (not needed)
    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. (not needed)

To help with the load of incoming pull requests:

Commands I ran locally:

python -m ruff format homeassistant/components/nuki/sensor.py
python -m pytest tests/components/nuki

Copilot AI review requested due to automatic review settings December 25, 2025 22:06
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @victor-gurbani

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant home-assistant Bot marked this pull request as draft December 25, 2025 22:06
@home-assistant
Copy link
Copy Markdown
Contributor

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
Copy link
Copy Markdown
Contributor

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 pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign nuki 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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds the state_class attribute to the Nuki battery sensor to enable long-term statistics collection in Home Assistant. Battery level is a measurement that users want to track over time, and without a state_class, the sensor is not eligible for long-term statistics.

Key Changes

  • Added SensorStateClass.MEASUREMENT to the Nuki battery sensor
  • Updated test snapshots to reflect the new capability

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
homeassistant/components/nuki/sensor.py Imported SensorStateClass and added _attr_state_class = SensorStateClass.MEASUREMENT to the NukiBatterySensor class
tests/components/nuki/snapshots/test_sensor.ambr Updated test snapshots to include the new state_class capability in both entity registry and state attributes

The changes are well-implemented and follow Home Assistant patterns. The addition of SensorStateClass.MEASUREMENT to battery sensors is a common pattern used across many integrations in Home Assistant (e.g., August, Yale, and others). The test snapshots have been properly updated to reflect the new capability. No issues identified.

Copy link
Copy Markdown
Contributor

@jbouwh jbouwh left a comment

Choose a reason for hiding this comment

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

LGTM,
Thanks @victor-gurbani 👍

@jbouwh jbouwh added this to the 2025.12.5 milestone Dec 26, 2025
@jbouwh jbouwh merged commit 9d1f500 into home-assistant:dev Dec 26, 2025
41 of 42 checks passed
mettolen pushed a commit to mettolen/core that referenced this pull request Dec 26, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 27, 2025
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.

6 participants