Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add device_tracker.bluetooth_update service #15252

22 changes: 17 additions & 5 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,13 @@ def discover_devices():

request_rssi = config.get(CONF_REQUEST_RSSI, False)

def update_bluetooth(now):
def update_bluetooth():
Copy link
Member

Choose a reason for hiding this comment

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

I thought the now when calling the function from track_point_in_utc_time ?

Copy link
Contributor Author

@kariudo kariudo Jul 18, 2018

Choose a reason for hiding this comment

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

Look at the changeset, track_point_in_utc_time is called with dt_util.utcnow() not with now, I assumed that the unused now was not being used due to the delay introduced by it being called at the end of the function which means that time would be in the past when it was called, which could cause it to just continuously run again after the interval + time it starts, rather than interval+ time it completed.

Copy link
Contributor

@awarecan awarecan Sep 13, 2018

Choose a reason for hiding this comment

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

This broke the code, argument even not used you still need have it

2018-09-12 16:28:11 ERROR (MainThread) [homeassistant.core] Error doing job: Future exception was never retrieved
 Traceback (most recent call last):
   File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
     result = self.fn(*self.args, **self.kwargs)
 TypeError: update_bluetooth() takes 0 positional arguments but 1 was given

"""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:
Expand All @@ -99,9 +106,14 @@ 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)

update_bluetooth(dt_util.utcnow())
def handle_update_bluetooth(call):
Copy link
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
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."""
update_bluetooth_once()

update_bluetooth()

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

return True