Add device_tracker.bluetooth_update service (#15252)

* Add device_tracker.bluetooth_update service

Will immediately scan for Bluetooth devices outside of the interval timer. Allows for less frequent scanning, with scanning on demand via automation.

* remove excess whitespace per bot comments

* Refactored update_bluetooth to call new function update_bluetooth_once

* Change service name to bluetooth_tracker_update to reflect platform name

* Reformat for line length

* Linting fix, pydoc, first line should end with a period

* Fixed a method call, and removed some more unsused parameters
This commit is contained in:
Hunter Horsman 2018-08-27 03:08:23 -04:00 committed by Daniel Høyer Iversen
parent a439690bd7
commit dec2d8d5b0

View file

@ -12,7 +12,8 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import track_point_in_utc_time
from homeassistant.components.device_tracker import (
YAML_DEVICES, CONF_TRACK_NEW, CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL,
load_config, PLATFORM_SCHEMA, DEFAULT_TRACK_NEW, SOURCE_TYPE_BLUETOOTH)
load_config, PLATFORM_SCHEMA, DEFAULT_TRACK_NEW, SOURCE_TYPE_BLUETOOTH,
DOMAIN)
import homeassistant.util.dt as dt_util
_LOGGER = logging.getLogger(__name__)
@ -79,7 +80,13 @@ def setup_scanner(hass, config, see, discovery_info=None):
request_rssi = config.get(CONF_REQUEST_RSSI, False)
def update_bluetooth(now):
def update_bluetooth():
"""Update Bluetooth and set timer for the next update."""
update_bluetooth_once()
track_point_in_utc_time(
hass, update_bluetooth, dt_util.utcnow() + interval)
def update_bluetooth_once():
"""Lookup Bluetooth device and update status."""
try:
if track_new:
@ -99,9 +106,14 @@ def setup_scanner(hass, config, see, discovery_info=None):
see_device(mac, result, rssi)
except bluetooth.BluetoothError:
_LOGGER.exception("Error looking up Bluetooth device")
track_point_in_utc_time(
hass, update_bluetooth, dt_util.utcnow() + interval)
update_bluetooth(dt_util.utcnow())
def handle_update_bluetooth(call):
"""Update bluetooth devices on demand."""
update_bluetooth_once()
update_bluetooth()
hass.services.register(
DOMAIN, "bluetooth_tracker_update", handle_update_bluetooth)
return True