Skip to content

Set should_poll attribute to False for MySensors battery sensor#165282

Draft
EdLeckert wants to merge 3 commits intohome-assistant:devfrom
EdLeckert:fix-stop-spamming-battery-state
Draft

Set should_poll attribute to False for MySensors battery sensor#165282
EdLeckert wants to merge 3 commits intohome-assistant:devfrom
EdLeckert:fix-stop-spamming-battery-state

Conversation

@EdLeckert
Copy link

The attribute should_poll defaults to True, and having force_update set to True causes integration to spam the battery state every 30 seconds with a typically identical value.

Proposed change

Adding

_attr_should_poll = False

to MyBatterySensor at line 260 of sensor.py fixes the issue.

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

  • 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
  • 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.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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 diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

should_poll defaults to True, and having force_update set to True causes integration to spam the battery state every 30 seconds with a typically identical value.
Copilot AI review requested due to automatic review settings March 10, 2026 19:46
Copy link

@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 @EdLeckert

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
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 10, 2026 19:46
@home-assistant
Copy link

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

Code owner commands

Code owners of mysensors 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 mysensors 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, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
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 PR addresses excessive battery sensor state updates in the mysensors integration by disabling polling for the node-level battery sensor entity, preventing periodic (default 30s) state writes when the value hasn’t changed.

Changes:

  • Mark MyBatterySensor as non-polled by setting _attr_should_poll = False.

Copy link
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.

Please add a test too. Just moving time forward should not trigger a state update.

@MartinHjelmare MartinHjelmare changed the title Set should_poll attribute to False for battery sensor Set should_poll attribute to False for MySensors battery sensor Mar 10, 2026
@EdLeckert
Copy link
Author

Please add a test too. Just moving time forward should not trigger a state update.

This is my first core integration PR and I am not familiar with the test configuration. If you can tell me what you want added I can attach it to the PR.

@MartinHjelmare
Copy link
Member

MartinHjelmare commented Mar 11, 2026

Here's the existing test for the battery sensor:

async def test_battery_entity(
hass: HomeAssistant,
battery_sensor: Sensor,
receive_message: Callable[[str], None],
) -> None:
"""Test sensor with battery level reporting."""
battery_entity_id = "sensor.battery_sensor_1_battery"
state = hass.states.get(battery_entity_id)
assert state
assert state.state == "42"
assert ATTR_BATTERY_LEVEL not in state.attributes
receive_message("1;255;3;0;0;84\n")
await hass.async_block_till_done()
state = hass.states.get(battery_entity_id)
assert state
assert state.state == "84"
assert ATTR_BATTERY_LEVEL not in state.attributes

Add a new test or extend that test, and test that moving time forward 30 seconds doesn't trigger a state reported event.

We have a test helper for moving time forward.

core/tests/common.py

Lines 499 to 502 in 30aec4d

@callback
def async_fire_time_changed(
hass: HomeAssistant, datetime_: datetime | None = None, fire_all: bool = False
) -> None:

We have the async_track_state_report_event to listen for state reported events:

def async_track_state_report_event(
hass: HomeAssistant,
entity_ids: str | Iterable[str],
action: Callable[[Event[EventStateReportedData]], Any],
job_type: HassJobType | None = None,
) -> CALLBACK_TYPE:

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.

MySensors integration spams battery sensor state with unnecessary updates

3 participants