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

Improving icloud device tracker #14078

Merged
merged 5 commits into from May 8, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 35 additions & 24 deletions homeassistant/components/device_tracker/icloud.py
Expand Up @@ -24,8 +24,9 @@

REQUIREMENTS = ['pyicloud==0.9.1']

CONF_IGNORED_DEVICES = 'ignored_devices'
CONF_ACCOUNTNAME = 'account_name'
CONF_MAX_INTERVAL = 'max_interval'
CONF_GPS_ACCURACY_THRESHOLD = 'gps_accuracy_threshold'

# entity attributes
ATTR_ACCOUNTNAME = 'account_name'
Expand Down Expand Up @@ -79,8 +80,11 @@ def setup_scanner(hass, config: dict, see, discovery_info=None):
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
account = config.get(CONF_ACCOUNTNAME, slugify(username.partition('@')[0]))
max_interval = config.get(CONF_MAX_INTERVAL, 30)
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Copy link
Member

Choose a reason for hiding this comment

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

Default should be set in the PLATFORM_SCHEMA.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, fixed that. Re-ran all tests and checks. Tested manually that defaults values do take effect.

gps_accuracy_threshold = config.get(CONF_GPS_ACCURACY_THRESHOLD, 1000)
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Copy link
Member

Choose a reason for hiding this comment

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

Dito


icloudaccount = Icloud(hass, username, password, account, see)
icloudaccount = Icloud(hass, username, password, account, max_interval,
gps_accuracy_threshold, see)

if icloudaccount.api is not None:
ICLOUDTRACKERS[account] = icloudaccount
Expand All @@ -96,6 +100,7 @@ def lost_iphone(call):
for account in accounts:
if account in ICLOUDTRACKERS:
ICLOUDTRACKERS[account].lost_iphone(devicename)

hass.services.register(DOMAIN, 'icloud_lost_iphone', lost_iphone,
schema=SERVICE_SCHEMA)

Expand All @@ -106,6 +111,7 @@ def update_icloud(call):
for account in accounts:
if account in ICLOUDTRACKERS:
ICLOUDTRACKERS[account].update_icloud(devicename)

hass.services.register(DOMAIN, 'icloud_update', update_icloud,
schema=SERVICE_SCHEMA)

Expand All @@ -115,6 +121,7 @@ def reset_account_icloud(call):
for account in accounts:
if account in ICLOUDTRACKERS:
ICLOUDTRACKERS[account].reset_account_icloud()

hass.services.register(DOMAIN, 'icloud_reset_account',
reset_account_icloud, schema=SERVICE_SCHEMA)

Expand All @@ -137,7 +144,8 @@ def setinterval(call):
class Icloud(DeviceScanner):
"""Representation of an iCloud account."""

def __init__(self, hass, username, password, name, see):
def __init__(self, hass, username, password, name, max_interval,
gps_accuracy_threshold, see):
"""Initialize an iCloud account."""
self.hass = hass
self.username = username
Expand All @@ -148,6 +156,8 @@ def __init__(self, hass, username, password, name, see):
self.seen_devices = {}
self._overridestates = {}
self._intervals = {}
self._max_interval = max_interval
self._gps_accuracy_threshold = gps_accuracy_threshold
self.see = see

self._trusted_device = None
Expand Down Expand Up @@ -348,7 +358,7 @@ def determine_interval(self, devicename, latitude, longitude, battery):
self._overridestates[devicename] = None

if currentzone is not None:
self._intervals[devicename] = 30
self._intervals[devicename] = self._max_interval
return

if mindistance is None:
Expand All @@ -363,7 +373,6 @@ def determine_interval(self, devicename, latitude, longitude, battery):

if interval > 180:
# Three hour drive? This is far enough that they might be flying
# home - check every half hour
interval = 30

if battery is not None and battery <= 33 and mindistance > 3:
Expand Down Expand Up @@ -403,22 +412,24 @@ def update_device(self, devicename):
status = device.status(DEVICESTATUSSET)
battery = status.get('batteryLevel', 0) * 100
location = status['location']
if location:
self.determine_interval(
devicename, location['latitude'],
location['longitude'], battery)
interval = self._intervals.get(devicename, 1)
attrs[ATTR_INTERVAL] = interval
accuracy = location['horizontalAccuracy']
kwargs['dev_id'] = dev_id
kwargs['host_name'] = status['name']
kwargs['gps'] = (location['latitude'],
location['longitude'])
kwargs['battery'] = battery
kwargs['gps_accuracy'] = accuracy
kwargs[ATTR_ATTRIBUTES] = attrs
self.see(**kwargs)
self.seen_devices[devicename] = True
if location and location['horizontalAccuracy']:
horizontal_accuracy = int(location['horizontalAccuracy'])
if horizontal_accuracy < self._gps_accuracy_threshold:
self.determine_interval(
devicename, location['latitude'],
location['longitude'], battery)
interval = self._intervals.get(devicename, 1)
attrs[ATTR_INTERVAL] = interval
accuracy = location['horizontalAccuracy']
kwargs['dev_id'] = dev_id
kwargs['host_name'] = status['name']
kwargs['gps'] = (location['latitude'],
location['longitude'])
kwargs['battery'] = battery
kwargs['gps_accuracy'] = accuracy
kwargs[ATTR_ATTRIBUTES] = attrs
self.see(**kwargs)
self.seen_devices[devicename] = True
except PyiCloudNoDevicesException:
_LOGGER.error("No iCloud Devices found")

Expand All @@ -434,7 +445,7 @@ def lost_iphone(self, devicename):
device.play_sound()

def update_icloud(self, devicename=None):
"""Authenticate against iCloud and scan for devices."""
"""Request device information from iCloud and update device_tracker."""
from pyicloud.exceptions import PyiCloudNoDevicesException

if self.api is None:
Expand All @@ -443,13 +454,13 @@ def update_icloud(self, devicename=None):
try:
if devicename is not None:
if devicename in self.devices:
self.devices[devicename].location()
self.update_device(devicename)
else:
_LOGGER.error("devicename %s unknown for account %s",
devicename, self._attrs[ATTR_ACCOUNTNAME])
else:
for device in self.devices:
self.devices[device].location()
self.update_device(device)
except PyiCloudNoDevicesException:
_LOGGER.error("No iCloud Devices found")

Expand Down