Skip to content
Merged
Changes from all 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
63 changes: 38 additions & 25 deletions homeassistant/components/device_tracker/icloud.py
Original file line number Diff line number Diff line change
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 @@ -64,13 +65,15 @@
SERVICE_SCHEMA = vol.Schema({
vol.Optional(ATTR_ACCOUNTNAME): vol.All(cv.ensure_list, [cv.slugify]),
vol.Optional(ATTR_DEVICENAME): cv.slugify,
vol.Optional(ATTR_INTERVAL): cv.positive_int,
vol.Optional(ATTR_INTERVAL): cv.positive_int
})

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(ATTR_ACCOUNTNAME): cv.slugify,
vol.Optional(CONF_MAX_INTERVAL, default=30): cv.positive_int,
vol.Optional(CONF_GPS_ACCURACY_THRESHOLD, default=1000): cv.positive_int
})


Expand All @@ -79,8 +82,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)
gps_accuracy_threshold = config.get(CONF_GPS_ACCURACY_THRESHOLD)

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 +102,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 +113,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 +123,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 +146,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 +158,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 +360,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 +375,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 +414,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 +447,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 +456,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