core icon indicating copy to clipboard operation
core copied to clipboard

Support polling the MiFlora battery

Open Jc2k opened this issue 1 year ago • 2 comments

Proposed change

Add support for the MiFlora battery sensor. Unlike the other sensors, we cannot collect this passively. We only poll it once a day to conserve battery use. We also only poll it when we have recently seen an advertisement for it.

Screenshot 2022-08-05 at 22 59 16

Extends the PassiveBluetoothProcessorCoordinator in the bluetooth integration to support polling, adding a new ActiveBluetoothProcessorCoordinator for devices that predominately use PassiveBluetoothProcessorCoordinator but need to do a bit of polling as well.

https://github.com/Bluetooth-Devices/xiaomi-ble/compare/v0.6.4...v0.8.0

Type of change

  • [ ] Dependency upgrade
  • [ ] Bugfix (non-breaking change which fixes an issue)
  • [ ] New integration (thank you!)
  • [x] 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
  • [ ] 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:

Jc2k avatar Aug 06 '22 09:08 Jc2k

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

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

As per above, have pulled the bluetooth part out into https://github.com/home-assistant/core/pull/76549.

Jc2k avatar Aug 10 '22 09:08 Jc2k

Ok, this is all rebased again.

Jc2k avatar Aug 10 '22 18:08 Jc2k

Saw this in testing but I didn't have the latest version

2022-08-10 15:11:55.668 ERROR (MainThread) [homeassistant.components.bluetooth] Error in bluetooth callback
Traceback (most recent call last):
  File "/Users/bdraco/home-assistant/homeassistant/components/bluetooth/__init__.py", line 489, in _device_detected
    callback(service_info, BluetoothChange.ADVERTISEMENT)
  File "/Users/bdraco/home-assistant/homeassistant/components/bluetooth/active_update_coordinator.py", line 140, in _async_handle_bluetooth_event
    if self.needs_poll(service_info):
  File "/Users/bdraco/home-assistant/homeassistant/components/bluetooth/active_update_coordinator.py", line 95, in needs_poll
    return self._needs_poll_method(service_info, poll_age)
  File "/Users/bdraco/home-assistant/homeassistant/components/xiaomi_ble/__init__.py", line 62, in _needs_poll
    return data.poll_needed(service_info, last_poll)
  File "/Users/bdraco/home-assistant/venv/lib/python3.10/site-packages/xiaomi_ble/parser.py", line 1305, in poll_needed
    if self.device_id != 0x0098:
AttributeError: 'XiaomiBluetoothDeviceData' object has no attribute 'device_id'

Seems like a race

bdraco avatar Aug 11 '22 01:08 bdraco

Havent seen that myself, but I just bumped the parser to always return False in needs_poll if parser.pending (pending is True until it has definitely pased something).

Jc2k avatar Aug 11 '22 06:08 Jc2k

Havent seen that myself, but I just bumped the parser to always return False in needs_poll if parser.pending (pending is True until it has definitely pased something).

It took 10-15 restarts to happen

bdraco avatar Aug 11 '22 07:08 bdraco