4
4
import json
5
5
import logging
6
6
import struct
7
+ import importlib
7
8
from threading import Thread
8
9
9
10
import aioblescan as aiobs
17
18
from homeassistant .helpers import config_validation as cv
18
19
from homeassistant .helpers import device_registry , entity_registry
19
20
from homeassistant .util import dt
20
- try :
21
- from homeassistant .components import bluetooth
22
- except ImportError :
23
- bluetooth = None
24
21
25
22
from .ble_parser import BleParser
26
23
from .bt_helpers import (BT_INTERFACES , BT_MULTI_SELECT , DEFAULT_BT_INTERFACE ,
@@ -516,6 +513,10 @@ def __init__(self, config, dataqueue):
516
513
self .report_unknown_whitelist = []
517
514
self .last_bt_reset = dt .now ()
518
515
self .scanners = {}
516
+ try :
517
+ self .bluetooth = importlib .import_module ("homeassistant.components.bluetooth" )
518
+ except ImportError :
519
+ self .bluetooth = None
519
520
if self .config [CONF_REPORT_UNKNOWN ]:
520
521
if self .config [CONF_REPORT_UNKNOWN ] != "Off" :
521
522
self .report_unknown = self .config [CONF_REPORT_UNKNOWN ]
@@ -588,8 +589,7 @@ def __init__(self, config, dataqueue):
588
589
aeskeys = self .aeskeys ,
589
590
)
590
591
591
- @staticmethod
592
- def hci_packet_on_advertisement (scanner , packet ):
592
+ def hci_packet_on_advertisement (self , scanner , packet ):
593
593
def _format_uuid (uuid : bytes ) -> str :
594
594
if len (uuid ) == 2 or len (uuid ) == 4 :
595
595
return "{:08x}-0000-1000-8000-00805f9b34fb" .format (
@@ -675,15 +675,15 @@ def _format_uuid(uuid: bytes) -> str:
675
675
manufacturer_data = manufacturer_data ,
676
676
tx_power = tx_power ,
677
677
details = {"address_type" : address_type },
678
- advertisement_monotonic_time = bluetooth .MONOTONIC_TIME (),
678
+ advertisement_monotonic_time = self . bluetooth .MONOTONIC_TIME (),
679
679
)
680
680
681
681
def process_hci_events (self , data , device_id = None , gateway_id = DOMAIN , proxy = False ):
682
682
"""Parse HCI events."""
683
683
self .evt_cnt += 1
684
684
if len (data ) < 12 :
685
685
return
686
- if bluetooth is not None and proxy :
686
+ if self . bluetooth is not None and proxy :
687
687
try :
688
688
scanner_name = device_id or gateway_id
689
689
scanner = self .scanners .get (scanner_name )
@@ -694,8 +694,8 @@ def process_hci_events(self, data, device_id=None, gateway_id=DOMAIN, proxy=Fals
694
694
if entry .name .lower () == gateway_id .lower ()), None )
695
695
source = next ((connection [1 ] for connection in device .connections if
696
696
connection [0 ] in ["mac" , "bluetooth" ]), gateway_id ) if device else gateway_id
697
- scanner = bluetooth .BaseHaRemoteScanner (source , gateway_id , None , False )
698
- bluetooth .async_register_scanner (hass , scanner )
697
+ scanner = self . bluetooth .BaseHaRemoteScanner (source , gateway_id , None , False )
698
+ self . bluetooth .async_register_scanner (hass , scanner )
699
699
self .scanners [scanner_name ] = scanner
700
700
self .hci_packet_on_advertisement (scanner , data )
701
701
except Exception as e :
0 commit comments