Repository: home-assistant/core
Pull request: Close stale connections (Airthings BLE)
<!--
  You are amazing! Thanks for contributing to our project!
  Please, DO NOT DELETE ANY TEXT from this template! (unless instructed).
-->
## Breaking change
<!--
  If your PR contains a breaking change for existing users, it is important
  to tell them what breaks, how to make it work again and why we did this.
  This piece of text is published with the release notes, so it helps if you
  write it towards our users, not us.
  Note: Remove this section if this PR is NOT a breaking change.
-->


## Proposed change
<!--
  Describe the big picture of your changes here to communicate to the
  maintainers why we should accept this pull request. If it fixes a bug
  or resolves a feature request, be sure to link to that issue in the
  additional information section.
-->
People are complaining about devices going offline / unavailable. Trying to add `close_stale_connections` to see if that helps.


## Type of change
<!--
  What type of change does your PR introduce to Home Assistant?
  NOTE: Please, check only 1! box!
  If your PR requires multiple boxes to be checked, you'll most likely need to
  split it into multiple PRs. This makes things easier and faster to code review.
-->

- [ ] Dependency upgrade
- [x] 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
<!--
  Details are important, and help maintainers processing your PR.
  Please be sure to fill out additional details, if applicable.
-->

- This PR fixes or closes issue: fixes #104286
- This PR is related to issue: 
- Link to documentation pull request: 

## Checklist
<!--
  Put an `x` in the boxes that apply. You can also fill these out after
  creating the PR. If you're unsure about any of them, don't hesitate to ask.
  We're here to help! This is simply a reminder of what we are going to look
  for before merging your code.
-->

- [x] The code change is tested and works locally.
- [x] Local tests pass. **Your PR cannot be merged unless tests pass**
- [x] There is no commented out code in this PR.
- [x] I have followed the [development checklist][dev-checklist]
- [x] I have followed the [perfect PR recommendations][perfect-pr]
- [x] The code has been formatted using Ruff (`ruff format homeassistant tests`)
- [ ] Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

- [ ] Documentation added/updated for [www.home-assistant.io][docs-repository]

If the code communicates with devices, web services, or third-party tools:

- [ ] The [manifest file][manifest-docs] 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`.

<!--
  This project is very active and we have a high turnover of pull requests.

  Unfortunately, the number of incoming pull requests is higher than what our
  reviewers can review and merge so there is a long backlog of pull requests
  waiting for review. You can help here!
  
  By reviewing another pull request, you will help raise the code quality of
  that pull request and the final review will be faster. This way the general
  pace of pull request reviews will go up and your wait time will go down.
  
  When picking a pull request to review, try to choose one that hasn't yet
  been reviewed.

  Thanks for helping out!
-->

To help with the load of incoming pull requests:

- [x] I have reviewed two other [open pull requests][prs] in this repository.

[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone+-status%3Afailure

<!--
  Thank you for contributing <3

  Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/development_checklist/
[manifest-docs]: https://developers.home-assistant.io/docs/creating_integration_manifest/
[quality-scale]: https://developers.home-assistant.io/docs/integration_quality_scale_index/
[docs-repository]: https://github.com/home-assistant/home-assistant.io
[perfect-pr]: https://developers.home-assistant.io/docs/review-process/#creating-the-perfect-pr


Problem statement:
Airthings BLE Sensors Go Unavailable
### The problem

The Airthings BLE integration updates its sensors then five minutes later they go unavailable 

### What version of Home Assistant Core has the issue?

2023.11.2

### What was the last working version of Home Assistant Core?

?

### What type of installation are you running?

Home Assistant OS

### Integration causing the issue

Airthings BLE

### Link to integration documentation on our website

_No response_

### Diagnostics information

2023-11-20 17:01:15.306 ERROR (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (D8:3A:DD:39:xx:yy): Error stopping scanner: [org.bluez.Error.DoesNotExist] Does Not Exist
2023-11-20 17:06:15.315 ERROR (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (D8:3A:DD:39:xx:yy): Error stopping scanner: [org.bluez.Error.DoesNotExist] Does Not Exist
2023-11-20 17:13:49.416 DEBUG (MainThread) [homeassistant.components.airthings_ble] Get device characteristics exception: [org.freedesktop.DBus.Error.NoReply] Remote peer disconnected
2023-11-20 17:13:51.480 DEBUG (MainThread) [homeassistant.components.airthings_ble] Finished fetching airthings_ble data in 29.054 seconds (success: False)
2023-11-20 17:13:58.132 DEBUG (MainThread) [homeassistant.components.airthings_ble] Get device characteristics exception: [org.freedesktop.DBus.Error.UnknownObject] Method "ReadValue" with signature "a{sv}" on interface "org.bluez.GattCharacteristic1" doesn't exist
2023-11-20 17:14:00.174 DEBUG (MainThread) [homeassistant.components.airthings_ble] Finished fetching airthings_ble data in 3.263 seconds (success: False)
2023-11-20 17:14:13.205 DEBUG (MainThread) [homeassistant.components.airthings_ble] Characteristics not handled: 00002a29-0000-1000-8000-00805f9b34fb
2023-11-20 17:14:16.179 DEBUG (MainThread) [homeassistant.components.airthings_ble] Finished fetching airthings_ble data in 5.702 seconds (success: False)
2023-11-20 17:14:38.748 DEBUG (MainThread) [homeassistant.components.airthings_ble] Characteristics not handled: 00002a29-0000-1000-8000-00805f9b34fb
2023-11-20 17:14:41.187 DEBUG (MainThread) [homeassistant.components.airthings_ble] Finished fetching airthings_ble data in 4.608 seconds (success: False)
2023-11-20 17:15:57.474 DEBUG (MainThread) [homeassistant.components.airthings_ble] Finished fetching airthings_ble data in 36.137 seconds (success: False)
2023-11-20 17:16:15.337 ERROR (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (D8:3A:DD:39:xx:yy): Error stopping scanner: [org.bluez.Error.DoesNotExist] Does Not Exist
2023-11-20 17:17:53.644 DEBUG (MainThread) [homeassistant.components.airthings_ble] Finished fetching airthings_ble data in 36.090 seconds (success: False)
2023-11-20 17:22:15.348 ERROR (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (D8:3A:DD:39:xx:yy): Error stopping scanner: [org.bluez.Error.DoesNotExist] Does Not Exist
2023-11-20 17:35:15.372 ERROR (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (D8:3A:DD:39:xx:yy): Error stopping scanner: [org.bluez.Error.DoesNotExist] Does Not Exist

### Example YAML snippet

```yaml
N/A
```


### Anything in the logs that might be useful for us?

```txt
N/A
```


### Additional information

_No response_

Hints from issue discussion:
Hey there @vincegio, @lastrada, mind taking a look at this issue as it has been labeled with an integration (`airthings_ble`) you are listed as a [code owner](https://github.com/home-assistant/core/blob/dev/CODEOWNERS#L50) for? Thanks!

<details>
<summary>Code owner commands</summary>


Code owners of `airthings_ble` can trigger bot actions by commenting:

- `@home-assistant close` Closes the issue.
- `@home-assistant rename Awesome new title` Renames the issue.
- `@home-assistant reopen` Reopen the issue.
- `@home-assistant unassign airthings_ble` Removes the current integration label and assignees on the issue, 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 issue.
- `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

</details>


<sub><sup>(message by CodeOwnersMention)</sup></sub>

---

[airthings_ble documentation](https://www.home-assistant.io/integrations/airthings_ble)
[airthings_ble source](https://github.com/home-assistant/core/tree/dev/homeassistant/components/airthings_ble)
<sub><sup>(message by IssueLinks)</sup></sub>
Yeah, the same thing has begun happening to me.  It's a bummer
Same here
Same here!

```txt
2023-12-02 09:56:15.485 DEBUG (MainThread) [homeassistant.components.airthings_ble] Finished fetching airthings_ble data in 81.014 seconds (success: False)
2023-12-02 09:58:16.776 DEBUG (MainThread) [homeassistant.components.airthings_ble] Finished fetching airthings_ble data in 81.012 seconds (success: False)
```

<img width="1049" alt="Captura de pantalla 2023-12-02 a las 10 08 28" src="https://github.com/home-assistant/core/assets/62313198/13072c31-e76d-49b1-9055-b0c55b7737a1">

I recently purchased an Airthings Wave+ because of the HA integration and the first week it was rock solid and then suddenly yesterday at 8pm ET, the data stopped flowing in for some reason.

<img width="985" alt="airthings_ble" src="https://github.com/home-assistant/core/assets/513244/aef20c1f-2b6f-41bd-a906-d3506b31be4b">

I have physically moved AirThings device to be within 1m of the Raspberry Pi 4 that is running HAOS to help to eliminate range/interference.  Prior to 8pm yesterday, the Pi was located about 5m away.

After looking at bluetoothctl, I saw that the devices were connecting and then dropping (all devices not just Airthings) every minute or so.  I thought that perhaps I needed to remove and re-add my bluetooth device in HA, but I think that i figured it out.

When I looked at Settings | Integrations | Bluetooth | Configure I had "Passive Scanning" enabled.  I disabled it and restarted and it's been connected solidly ever since.  It makes sense because the passive scanning is to extend battery life and so grabbing and releasing BT would result in disconnections and infrequent updates. 
Mine dropped out yesterday with "Passive Scanning" disabled from day one.
Same here.. Got four Wave Pluses all acting the same way. Going very stable for some time, then starting to decrease reporting, before ending more or less totally disconnected displaying NaN. Tried soft and hard reboots of both HA and units themselves.

Running in ProxMox with only ESPhome BT proxies, not local BT or other dongles.
<img width="1254" alt="Skjermbilde 2023-12-03 kl  12 27 28" src="https://github.com/home-assistant/core/assets/52956570/a4b8d782-c6c5-455c-a258-883641e74070">
<img width="760" alt="Skjermbilde 2023-12-03 kl  12 27 17" src="https://github.com/home-assistant/core/assets/52956570/5ec7b4ee-d41a-4901-84ce-57d3a13bc4d9">
<img width="825" alt="Skjermbilde 2023-12-03 kl  12 26 48" src="https://github.com/home-assistant/core/assets/52956570/ace477e4-2d61-4964-a573-2f8502307581">


> Mine dropped out yesterday with "Passive Scanning" disabled from day one.

Apparently switching "Passive scanning" on and off is sorting issue, at least temporarily.
I was having the same issue and got it to finally connect by turning on "Passive scanning".   I will leave it on and check back tomorrow to see if it stays connected.
I think passive scanning are not the solution, The Bluetooth should works as active scanning, maybe when you use a bluetooth proxy like Shelly plus devices, you can set these as passive but the root Bluetooth I think isn’t a good idea.
I found another solution and at the moment is still working from 1 hour a go, turn the active scanning, and go to integrations, airthings BLE, three dots, system options and disable check box of Enable Polling for updates.. I’ll update my status as soon have news about the solution 
![IMG_4301](https://github.com/home-assistant/core/assets/7962872/6cc4b741-9b46-4867-8390-56b85c786991)
![IMG_4302](https://github.com/home-assistant/core/assets/7962872/ba8ccaee-abdf-4b14-9050-90ea3f189526)

Commit message: Fix pr comment

Patch to review:
diff --git a/homeassistant/components/airthings_ble/__init__.py b/homeassistant/components/airthings_ble/__init__.py
index d7e6bddbcd4e10..dcb9ce3b1c0b74 100644
--- a/homeassistant/components/airthings_ble/__init__.py
+++ b/homeassistant/components/airthings_ble/__init__.py
@@ -5,6 +5,7 @@
 import logging
 
 from airthings_ble import AirthingsBluetoothDeviceData
+from bleak_retry_connector import close_stale_connections
 
 from homeassistant.components import bluetooth
 from homeassistant.config_entries import ConfigEntry
@@ -42,6 +43,8 @@ async def _async_update_method():
         ble_device = bluetooth.async_ble_device_from_address(hass, address)
         airthings = AirthingsBluetoothDeviceData(_LOGGER, elevation, is_metric)
 
+        await close_stale_connections(ble_device)
+
         try:
             data = await airthings.update_device(ble_device)
         except Exception as err:


Code review comments:
diff_hunk: @@ -42,6 +43,8 @@ async def _async_update_method():
         ble_device = bluetooth.async_ble_device_from_address(hass, address)
         airthings = AirthingsBluetoothDeviceData(_LOGGER, elevation, is_metric)
 
+        await close_stale_connections(ble_device)
path: homeassistant/components/airthings_ble/__init__.py
text: @user1:
This method is only intended to be used when the integration is setup to clean up if things didn't get shutdown cleanly. The library should implement proper connection management with disconnected callbacks so it shouldn't be needed in the update path.

It's completely fine to move it above in the setup process but if the intent is to workaround a flaw in the library, the library should be fixed instead 

@user1:
I just realized there is a problem with close stale connections not actually being able to close the connections if the device stops advertising when its connected like some switchbot devices do.  I'm not sure if that applies to airthings-ble though.

Regardless the solution is in https://github.com/home-assistant/core/pull/106835

If you aren't sure which one to use `close_stale_connections_by_address` is the better choice