Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions homeassistant/components/device_tracker/bluetooth_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
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__)
Expand Down Expand Up @@ -79,7 +80,7 @@ def discover_devices():

request_rssi = config.get(CONF_REQUEST_RSSI, False)

def update_bluetooth(now):
def update_bluetooth(now, once=False):
"""Lookup Bluetooth device and update status."""
try:
if track_new:
Expand All @@ -99,9 +100,17 @@ def update_bluetooth(now):
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)
if not once:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest extract line 85-102 to a new function. And call that function from update_bluetooth and handle_update_bluetooth. The you can avoid the once parameter

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, addressed with 5b4bfa4

track_point_in_utc_time(
hass, update_bluetooth, dt_util.utcnow() + interval)

def handle_update_bluetooth(call):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this function?
Why not call update_bluetooth_once directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certianly could, I just left it using the handle function per a previous comment you said:

I would suggest extract line 85-102 to a new function. And call that function from update_bluetooth and handle_update_bluetooth. The you can avoid the once parameter

I assumed you wanted it left using the handle_update_bluetooth function, to absorb the unused call parameter.

"""Update bluetooth devices on demand."""
now = dt_util.utcnow()
update_bluetooth(now, True)

update_bluetooth(dt_util.utcnow())

hass.services.register(DOMAIN, "bluetooth_update", handle_update_bluetooth)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefix the service name with the name of the platform, eg like this: 'bluetooth_tracker_update'. I intentionally stripped the name of the second bluetooth.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, I updated it to 'bluetooth_tracker_update' in a6d6f76


return True