From dec2d8d5b0d93d3a0ded66ed4847f86e839850fc Mon Sep 17 00:00:00 2001 From: Hunter Horsman <kariudo@gmail.com> Date: Mon, 27 Aug 2018 03:08:23 -0400 Subject: [PATCH] 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 --- .../device_tracker/bluetooth_tracker.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/device_tracker/bluetooth_tracker.py b/homeassistant/components/device_tracker/bluetooth_tracker.py index 2ca519d225c..217df0aacd4 100644 --- a/homeassistant/components/device_tracker/bluetooth_tracker.py +++ b/homeassistant/components/device_tracker/bluetooth_tracker.py @@ -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 -- GitLab