2019-04-03 17:40:03 +02:00
|
|
|
"""Tracking for bluetooth devices."""
|
2016-04-19 08:18:46 -07:00
|
|
|
import logging
|
2019-09-12 19:01:55 +03:00
|
|
|
from typing import List, Set, Tuple
|
2016-04-19 08:18:46 -07:00
|
|
|
|
2019-09-12 19:01:55 +03:00
|
|
|
# pylint: disable=import-error
|
|
|
|
import bluetooth
|
|
|
|
from bt_proximity import BluetoothRSSI
|
2016-09-04 19:10:20 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-05-15 23:43:45 +02:00
|
|
|
from homeassistant.components.device_tracker import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.components.device_tracker.const import (
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_SCAN_INTERVAL,
|
2019-09-12 19:01:55 +03:00
|
|
|
CONF_TRACK_NEW,
|
2019-07-31 12:25:30 -07:00
|
|
|
DEFAULT_TRACK_NEW,
|
|
|
|
DOMAIN,
|
2019-09-12 19:01:55 +03:00
|
|
|
SCAN_INTERVAL,
|
|
|
|
SOURCE_TYPE_BLUETOOTH,
|
2019-05-15 23:43:45 +02:00
|
|
|
)
|
2019-09-12 19:01:55 +03:00
|
|
|
from homeassistant.components.device_tracker.legacy import (
|
|
|
|
YAML_DEVICES,
|
|
|
|
async_load_config,
|
|
|
|
)
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.event import track_point_in_utc_time
|
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2019-05-15 23:43:45 +02:00
|
|
|
from homeassistant.util.async_ import run_coroutine_threadsafe
|
2019-09-12 19:01:55 +03:00
|
|
|
import homeassistant.util.dt as dt_util
|
2016-04-19 08:18:46 -07:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
BT_PREFIX = "BT_"
|
2016-04-19 08:18:46 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_REQUEST_RSSI = "request_rssi"
|
2018-03-31 23:22:54 +02:00
|
|
|
|
2018-12-25 12:29:44 -05:00
|
|
|
CONF_DEVICE_ID = "device_id"
|
|
|
|
|
|
|
|
DEFAULT_DEVICE_ID = -1
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_TRACK_NEW): cv.boolean,
|
|
|
|
vol.Optional(CONF_REQUEST_RSSI): cv.boolean,
|
|
|
|
vol.Optional(CONF_DEVICE_ID, default=DEFAULT_DEVICE_ID): vol.All(
|
|
|
|
vol.Coerce(int), vol.Range(min=-1)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2016-09-04 19:10:20 +02:00
|
|
|
|
2016-04-19 08:18:46 -07:00
|
|
|
|
2019-09-12 19:01:55 +03:00
|
|
|
def is_bluetooth_device(device) -> bool:
|
|
|
|
"""Check whether a device is a bluetooth device by its mac."""
|
|
|
|
return device.mac and device.mac[:3].upper() == BT_PREFIX
|
|
|
|
|
|
|
|
|
|
|
|
def discover_devices(device_id: int) -> List[Tuple[str, str]]:
|
|
|
|
"""Discover Bluetooth devices."""
|
|
|
|
result = bluetooth.discover_devices(
|
|
|
|
duration=8,
|
|
|
|
lookup_names=True,
|
|
|
|
flush_cache=True,
|
|
|
|
lookup_class=False,
|
|
|
|
device_id=device_id,
|
|
|
|
)
|
|
|
|
_LOGGER.debug("Bluetooth devices discovered = %d", len(result))
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def see_device(see, mac: str, device_name: str, rssi=None) -> None:
|
|
|
|
"""Mark a device as seen."""
|
|
|
|
attributes = {}
|
|
|
|
if rssi is not None:
|
|
|
|
attributes["rssi"] = rssi
|
|
|
|
see(
|
|
|
|
mac=f"{BT_PREFIX}{mac}",
|
|
|
|
host_name=device_name,
|
|
|
|
attributes=attributes,
|
|
|
|
source_type=SOURCE_TYPE_BLUETOOTH,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_tracking_devices(hass: HomeAssistantType) -> Tuple[Set[str], Set[str]]:
|
|
|
|
"""
|
|
|
|
Load all known devices.
|
|
|
|
|
|
|
|
We just need the devices so set consider_home and home range to 0
|
|
|
|
"""
|
|
|
|
yaml_path: str = hass.config.path(YAML_DEVICES)
|
|
|
|
devices_to_track: Set[str] = set()
|
|
|
|
devices_to_not_track: Set[str] = set()
|
|
|
|
|
2019-05-15 23:43:45 +02:00
|
|
|
for device in run_coroutine_threadsafe(
|
2019-07-31 12:25:30 -07:00
|
|
|
async_load_config(yaml_path, hass, 0), hass.loop
|
2019-05-15 23:43:45 +02:00
|
|
|
).result():
|
2017-04-30 07:04:49 +02:00
|
|
|
# Check if device is a valid bluetooth device
|
2019-09-12 19:01:55 +03:00
|
|
|
if not is_bluetooth_device(device):
|
|
|
|
continue
|
|
|
|
|
|
|
|
normalized_mac: str = device.mac[3:]
|
|
|
|
if device.track:
|
|
|
|
devices_to_track.add(normalized_mac)
|
|
|
|
else:
|
|
|
|
devices_to_not_track.add(normalized_mac)
|
|
|
|
|
|
|
|
return devices_to_track, devices_to_not_track
|
|
|
|
|
|
|
|
|
|
|
|
def setup_scanner(hass: HomeAssistantType, config: dict, see, discovery_info=None):
|
|
|
|
"""Set up the Bluetooth Scanner."""
|
|
|
|
device_id: int = config.get(CONF_DEVICE_ID)
|
|
|
|
devices_to_track, devices_to_not_track = get_tracking_devices(hass)
|
2016-04-19 08:18:46 -07:00
|
|
|
|
2017-04-30 07:04:49 +02:00
|
|
|
# If track new devices is true discover new devices on startup.
|
2019-09-12 19:01:55 +03:00
|
|
|
track_new: bool = config.get(CONF_TRACK_NEW, DEFAULT_TRACK_NEW)
|
|
|
|
_LOGGER.debug("Tracking new devices = %s", track_new)
|
|
|
|
|
|
|
|
if not devices_to_track and not track_new:
|
|
|
|
_LOGGER.debug("No Bluetooth devices to track and not tracking new devices")
|
|
|
|
|
2016-04-19 08:18:46 -07:00
|
|
|
if track_new:
|
2019-09-12 19:01:55 +03:00
|
|
|
for mac, device_name in discover_devices(device_id):
|
|
|
|
if mac not in devices_to_track and mac not in devices_to_not_track:
|
|
|
|
devices_to_track.add(mac)
|
|
|
|
see_device(see, mac, device_name)
|
2016-04-19 08:18:46 -07:00
|
|
|
|
2019-05-15 23:43:45 +02:00
|
|
|
interval = config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL)
|
2016-04-19 08:18:46 -07:00
|
|
|
|
2018-03-31 23:22:54 +02:00
|
|
|
request_rssi = config.get(CONF_REQUEST_RSSI, False)
|
2019-09-12 19:01:55 +03:00
|
|
|
if request_rssi:
|
|
|
|
_LOGGER.debug("Detecting RSSI for devices")
|
2018-03-31 23:22:54 +02:00
|
|
|
|
2018-09-12 22:52:31 -07:00
|
|
|
def update_bluetooth(_):
|
2018-08-27 03:08:23 -04:00
|
|
|
"""Update Bluetooth and set timer for the next update."""
|
|
|
|
update_bluetooth_once()
|
2019-07-31 12:25:30 -07:00
|
|
|
track_point_in_utc_time(hass, update_bluetooth, dt_util.utcnow() + interval)
|
2018-08-27 03:08:23 -04:00
|
|
|
|
|
|
|
def update_bluetooth_once():
|
2017-04-30 07:04:49 +02:00
|
|
|
"""Lookup Bluetooth device and update status."""
|
2016-04-19 08:18:46 -07:00
|
|
|
try:
|
2016-09-06 19:51:36 +02:00
|
|
|
if track_new:
|
2019-09-12 19:01:55 +03:00
|
|
|
for mac, device_name in discover_devices(device_id):
|
|
|
|
if mac not in devices_to_track and mac not in devices_to_not_track:
|
|
|
|
devices_to_track.add(mac)
|
|
|
|
|
|
|
|
for mac in devices_to_track:
|
2017-04-30 07:04:49 +02:00
|
|
|
_LOGGER.debug("Scanning %s", mac)
|
2019-09-12 19:01:55 +03:00
|
|
|
device_name = bluetooth.lookup_name(mac, timeout=5)
|
2018-03-31 23:22:54 +02:00
|
|
|
rssi = None
|
|
|
|
if request_rssi:
|
2019-07-14 23:11:54 +02:00
|
|
|
client = BluetoothRSSI(mac)
|
|
|
|
rssi = client.request_rssi()
|
|
|
|
client.close()
|
2019-09-12 19:01:55 +03:00
|
|
|
if device_name is None:
|
2016-04-19 08:18:46 -07:00
|
|
|
# Could not lookup device name
|
|
|
|
continue
|
2019-09-12 19:01:55 +03:00
|
|
|
see_device(see, mac, device_name, rssi)
|
2016-04-19 08:18:46 -07:00
|
|
|
except bluetooth.BluetoothError:
|
2017-04-30 07:04:49 +02:00
|
|
|
_LOGGER.exception("Error looking up Bluetooth device")
|
2016-04-19 08:18:46 -07:00
|
|
|
|
2018-08-27 03:08:23 -04:00
|
|
|
def handle_update_bluetooth(call):
|
|
|
|
"""Update bluetooth devices on demand."""
|
|
|
|
update_bluetooth_once()
|
|
|
|
|
2018-09-12 22:52:31 -07:00
|
|
|
update_bluetooth(dt_util.utcnow())
|
2018-08-27 03:08:23 -04:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.services.register(DOMAIN, "bluetooth_tracker_update", handle_update_bluetooth)
|
2016-04-19 08:18:46 -07:00
|
|
|
|
|
|
|
return True
|