Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with multiple sensors #89

Open
flecmart opened this issue Jul 16, 2022 · 3 comments
Open

Problem with multiple sensors #89

flecmart opened this issue Jul 16, 2022 · 3 comments

Comments

@flecmart
Copy link

Hi,

I updated the custom_component because of the breaking change in the latest home assistant release. Previously, I ran an airwave plus and and airwave mini with the custom_component for months without a problem.

After the migration only one of my sensors seem to connect. The other one becomes unavailable. Both sensors work independently but when I try to connect them both, one fails without any log in the debug log. It just says:

2022-07-16 09:04:34 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] Searching for Airthings sensors...
2022-07-16 09:04:34 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] Getting info about device(s)
2022-07-16 09:04:34 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] Connecting to AA:AA:AA:AA:AA:AA
2022-07-16 09:04:34 DEBUG (SyncWorker_6) [custom_components.airthings_wave.sensor] Searching for Airthings sensors...
2022-07-16 09:04:34 DEBUG (SyncWorker_6) [custom_components.airthings_wave.sensor] Getting info about device(s)
2022-07-16 09:04:34 DEBUG (SyncWorker_6) [custom_components.airthings_wave.airthings] Connecting to BB:BB:BB:BB:BB:BB
2022-07-16 09:04:37 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] Connected to AA:AA:AA:AA:AA:AA
2022-07-16 09:04:39 INFO (SyncWorker_5) [custom_components.airthings_wave.sensor] AA:AA:AA:AA:AA:AA: Manufacturer: Airthings AS Model: 2920 Serial: 099595 Device: Airthings Wave Mini Firmware: M-BLE-2.0.8-master+0 Hardware Rev.: REV E
2022-07-16 09:04:39 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] Getting sensors
2022-07-16 09:04:39 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] Connecting to AA:AA:AA:AA:AA:AA
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] Connected to AA:AA:AA:AA:AA:AA
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] 00002a29-0000-1000-8000-00805f9b34fb (Handle: 49): Manufacturer Name String
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] 00002a27-0000-1000-8000-00805f9b34fb (Handle: 47): Hardware Revision String
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] 00002a26-0000-1000-8000-00805f9b34fb (Handle: 45): Firmware Revision String
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] 00002a25-0000-1000-8000-00805f9b34fb (Handle: 43): Serial Number String
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] 00002a24-0000-1000-8000-00805f9b34fb (Handle: 41): Model Number String
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] 00002a23-0000-1000-8000-00805f9b34fb (Handle: 39): System ID
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] f000ffc5-0451-4000-b000-000000000000 (Handle: 34): Unknown
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] f000ffc2-0451-4000-b000-000000000000 (Handle: 30): Unknown
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] f000ffc1-0451-4000-b000-000000000000 (Handle: 26): Unknown
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] b42e41c4-ade7-11e4-89d3-123b93f75cba (Handle: 21): Unknown
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] b42e3ef4-ade7-11e4-89d3-123b93f75cba (Handle: 17): Unknown
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] b42e3b98-ade7-11e4-89d3-123b93f75cba (Handle: 13): Unknown
2022-07-16 09:04:43 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] 00002a05-0000-1000-8000-00805f9b34fb (Handle: 9): Service Changed
2022-07-16 09:04:44 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform airthings_wave is taking over 10 seconds.
2022-07-16 09:04:44 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform airthings_wave is taking over 10 seconds.
2022-07-16 09:04:45 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] AA:AA:AA:AA:AA:AA: Found sensor UUID: b42e3b98-ade7-11e4-89d3-123b93f75cba Handle: 13
2022-07-16 09:04:45 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] Get initial sensor data to populate HA entities
2022-07-16 09:04:45 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] Connecting to AA:AA:AA:AA:AA:AA
2022-07-16 09:04:48 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] Retrying AA:AA:AA:AA:AA:AA
2022-07-16 09:04:51 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] Connected to AA:AA:AA:AA:AA:AA
2022-07-16 09:04:51 DEBUG (SyncWorker_5) [custom_components.airthings_wave.airthings] AA:AA:AA:AA:AA:AA Got sensordata {'date_time': '2022-07-16T09:04:51.735477', 'temperature': 25.19, 'humidity': 37.87, 'voc': 520.0}
2022-07-16 09:04:53 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] AA:AA:AA:AA:AA:AA: date_time: 2022-07-16T09:04:51.735477
2022-07-16 09:04:53 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] Added sensor entity AA:AA:AA:AA:AA:AA-date_time
2022-07-16 09:04:53 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] AA:AA:AA:AA:AA:AA: temperature: 25.19
2022-07-16 09:04:53 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] Added sensor entity AA:AA:AA:AA:AA:AA-temperature
2022-07-16 09:04:53 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] AA:AA:AA:AA:AA:AA: humidity: 37.87
2022-07-16 09:04:53 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] Added sensor entity AA:AA:AA:AA:AA:AA-humidity
2022-07-16 09:04:53 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] AA:AA:AA:AA:AA:AA: voc: 520.0
2022-07-16 09:04:53 DEBUG (SyncWorker_5) [custom_components.airthings_wave.sensor] Added sensor entity AA:AA:AA:AA:AA:AA-voc
2022-07-16 09:04:53 DEBUG (SyncWorker_2) [custom_components.airthings_wave.sensor] State AA:AA:AA:AA:AA:AA-date_time 2022-07-16T09:04:51.735477
2022-07-16 09:04:53 DEBUG (SyncWorker_0) [custom_components.airthings_wave.sensor] State AA:AA:AA:AA:AA:AA-temperature 25.19
2022-07-16 09:04:53 DEBUG (SyncWorker_0) [custom_components.airthings_wave.sensor] State AA:AA:AA:AA:AA:AA-humidity 37.87
2022-07-16 09:04:53 DEBUG (SyncWorker_3) [custom_components.airthings_wave.sensor] State AA:AA:AA:AA:AA:AA-voc 520.0
2022-07-16 09:05:19 WARNING (MainThread) [homeassistant.bootstrap] Waiting on integrations to complete setup: sensor.airthings_wave_2
2022-07-16 09:05:34 ERROR (MainThread) [homeassistant.components.sensor] Setup of platform airthings_wave is taking longer than 60 seconds. Startup will proceed without waiting any longer.
2022-07-16 09:07:23 DEBUG (SyncWorker_3) [custom_components.airthings_wave.airthings] Connecting to AA:AA:AA:AA:AA:AA
2022-07-16 09:07:33 WARNING (MainThread) [homeassistant.helpers.entity] Update of sensor.airthings_wave_mini_temperature is taking over 10 seconds
2022-07-16 09:07:34 DEBUG (SyncWorker_3) [custom_components.airthings_wave.airthings] Connected to AA:AA:AA:AA:AA:AA
2022-07-16 09:07:34 DEBUG (SyncWorker_3) [custom_components.airthings_wave.airthings] AA:AA:AA:AA:AA:AA Got sensordata {'date_time': '2022-07-16T09:07:34.613619', 'temperature': 25.2, 'humidity': 37.83, 'voc': 512.0}
2022-07-16 09:07:36 DEBUG (SyncWorker_3) [custom_components.airthings_wave.sensor] State AA:AA:AA:AA:AA:AA-temperature 25.2
2022-07-16 09:07:36 DEBUG (SyncWorker_9) [custom_components.airthings_wave.sensor] State AA:AA:AA:AA:AA:AA-humidity 37.83
2022-07-16 09:07:36 DEBUG (SyncWorker_2) [custom_components.airthings_wave.sensor] State AA:AA:AA:AA:AA:AA-voc 512.0
2022-07-16 09:09:53 DEBUG (SyncWorker_3) [custom_components.airthings_wave.airthings] Connecting to AA:AA:AA:AA:AA:AA
2022-07-16 09:09:55 DEBUG (SyncWorker_3) [custom_components.airthings_wave.airthings] Connected to AA:AA:AA:AA:AA:AA
2022-07-16 09:09:55 DEBUG (SyncWorker_3) [custom_components.airthings_wave.airthings] AA:AA:AA:AA:AA:AA Got sensordata {'date_time': '2022-07-16T09:09:55.913570', 'temperature': 25.2, 'humidity': 37.83, 'voc': 512.0}
2022-07-16 09:09:58 DEBUG (SyncWorker_3) [custom_components.airthings_wave.sensor] State AA:AA:AA:AA:AA:AA-temperature 25.2
2022-07-16 09:09:58 DEBUG (SyncWorker_0) [custom_components.airthings_wave.sensor] State AA:AA:AA:AA:AA:AA-humidity 37.83
2022-07-16 09:09:58 DEBUG (SyncWorker_10) [custom_components.airthings_wave.sensor] State AA:AA:AA:AA:AA:AA-voc 512.0

While it says it is connecting to Sensor A and B it actually ever connects only to A.

Did anyone try multiple sensors? I suspect some kind of race condition here caused by the switch to asyncio. Bleak has an example on how to connect to two devices at the same time: https://github.com/hbldh/bleak/blob/develop/examples/two_devices.py

Maybe this helps someone. I will try to check the code for this myself but currently I am out of free time.

Thanks for the integration :-)

@flecmart
Copy link
Author

flecmart commented Jul 18, 2022

I examined this last night and I think the problem is that bleak cannot be used by two python programs at the same time in a non blocking way. Because my configuration looked like this:

sensor:
   - platform: airthings_wave
      scan_interval: 150
      elevation: !secret home_elevation
      mac: !secret airthings_wave_mini
    - platform: airthings_wave
      scan_interval: 150
      elevation: !secret home_elevation
      mac: !secret airthings_wave_plus

Home assistant spawned two python processes and one was then blocking the other (if my assumption is correct).

It works for multiple devices when I do not provide mac adresses and let the component scan for near BLE devices using a config l ike this:

sensor:
  - platform: airthings_wave
    scan_interval: 150
    elevation: !secret home_elevation

While playing around I went over the code and made sure disconnect() will be called after every connect()... this was not the case previosuly. I created a PR so someone could review and eventually merge those changes.

@onyxhat
Copy link

onyxhat commented Jul 19, 2022

Was having the same issue with explicitly defined mac for multiple sensors - removing that seems to have fixed my problem, but would be nice to have that functionality restored or have the docs updated.

@flecmart
Copy link
Author

Agree! For that my theory has to be verified and we have to find a way to use bleak clients from two separate python processes in a non blocking way.

I am no expert in asynchronous programming and at the moment my time is very limited... If no one will look a this I might get back learn something new about async programming but this might take a while...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants