2015-11-13 06:37:15 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.device_tracker.icloud
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-11-22 04:04:28 +00:00
|
|
|
Device tracker platform that supports scanning iCloud devices.
|
2015-11-13 06:37:15 +00:00
|
|
|
|
2015-11-22 04:04:28 +00:00
|
|
|
It does require that your device has beend registered with Find My iPhone.
|
|
|
|
|
|
|
|
Note: that this may cause battery drainage as it wakes up your device to
|
|
|
|
get the current location.
|
|
|
|
|
|
|
|
Note: You may receive an email from Apple stating that someone has logged
|
|
|
|
into your account.
|
2015-11-13 06:37:15 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/device_tracker.icloud/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2015-12-04 16:19:16 +00:00
|
|
|
import re
|
2015-11-13 06:37:15 +00:00
|
|
|
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD
|
2015-11-22 04:04:28 +00:00
|
|
|
from homeassistant.helpers.event import track_utc_time_change
|
2015-11-13 06:37:15 +00:00
|
|
|
|
2015-12-04 16:08:46 +00:00
|
|
|
SCAN_INTERVAL = 1800
|
2015-11-13 06:37:15 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
REQUIREMENTS = ['https://github.com/picklepete/pyicloud/archive/'
|
|
|
|
'80f6cd6decc950514b8dc43b30c5bded81b34d5f.zip'
|
2015-11-22 04:04:28 +00:00
|
|
|
'#pyicloud==0.8.0']
|
2015-11-13 06:37:15 +00:00
|
|
|
|
|
|
|
|
2015-11-22 04:04:28 +00:00
|
|
|
def setup_scanner(hass, config, see):
|
2015-11-22 04:12:41 +00:00
|
|
|
"""
|
|
|
|
Set up the iCloud Scanner
|
|
|
|
"""
|
2015-12-04 16:08:46 +00:00
|
|
|
from pyicloud import PyiCloudService
|
|
|
|
from pyicloud.exceptions import PyiCloudFailedLoginException
|
|
|
|
from pyicloud.exceptions import PyiCloudNoDevicesException
|
2015-11-13 06:37:15 +00:00
|
|
|
|
2015-11-22 04:04:28 +00:00
|
|
|
# Get the username and password from the configuration
|
|
|
|
username = config[CONF_USERNAME]
|
|
|
|
password = config[CONF_PASSWORD]
|
2015-11-13 06:37:15 +00:00
|
|
|
|
2015-11-22 04:04:28 +00:00
|
|
|
try:
|
|
|
|
_LOGGER.info('Logging into iCloud Account')
|
|
|
|
# Attempt the login to iCloud
|
|
|
|
api = PyiCloudService(username,
|
|
|
|
password,
|
|
|
|
verify=True)
|
2015-12-04 16:08:46 +00:00
|
|
|
except PyiCloudFailedLoginException as error:
|
2015-11-22 04:12:41 +00:00
|
|
|
_LOGGER.exception(
|
2015-12-04 16:23:05 +00:00
|
|
|
'Error logging into iCloud Service: %s' % error
|
2015-11-22 04:12:41 +00:00
|
|
|
)
|
2015-12-04 16:08:46 +00:00
|
|
|
return
|
2015-11-13 06:37:15 +00:00
|
|
|
|
2015-11-22 04:04:28 +00:00
|
|
|
def update_icloud(now):
|
2015-11-22 04:12:41 +00:00
|
|
|
"""
|
|
|
|
Authenticate against iCloud and scan for devices.
|
|
|
|
"""
|
2015-11-13 06:37:15 +00:00
|
|
|
try:
|
2015-11-22 04:12:41 +00:00
|
|
|
# The session timeouts if we are not using it so we
|
|
|
|
# have to re-authenticate. This will send an email.
|
2015-11-22 04:04:28 +00:00
|
|
|
api.authenticate()
|
|
|
|
# Loop through every device registered with the iCloud account
|
|
|
|
for device in api.devices:
|
|
|
|
status = device.status()
|
|
|
|
location = device.location()
|
|
|
|
# If the device has a location add it. If not do nothing
|
|
|
|
if location:
|
|
|
|
see(
|
|
|
|
dev_id=re.sub(r"(\s|\W|')",
|
|
|
|
'',
|
|
|
|
status['name']),
|
|
|
|
host_name=status['name'],
|
|
|
|
gps=(location['latitude'], location['longitude']),
|
|
|
|
battery=status['batteryLevel']*100,
|
|
|
|
gps_accuracy=location['horizontalAccuracy']
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# No location found for the device so continue
|
|
|
|
continue
|
2015-11-22 04:12:41 +00:00
|
|
|
except PyiCloudNoDevicesException:
|
2015-12-04 16:08:46 +00:00
|
|
|
_LOGGER.info('No iCloud Devices found!')
|
2015-11-22 04:04:28 +00:00
|
|
|
|
|
|
|
track_utc_time_change(
|
|
|
|
hass,
|
|
|
|
update_icloud,
|
|
|
|
second=range(0, 60, SCAN_INTERVAL)
|
2015-11-22 04:12:41 +00:00
|
|
|
)
|