core/homeassistant/components/device_tracker/icloud.py

86 lines
2.9 KiB
Python
Raw Normal View History

"""
2016-03-07 17:12:06 +00:00
Support for iCloud connected devices.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/device_tracker.icloud/
"""
import logging
import re
2016-02-19 05:27:50 +00:00
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
2015-11-22 04:04:28 +00:00
from homeassistant.helpers.event import track_utc_time_change
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pyicloud==0.8.3']
2015-12-15 05:39:48 +00:00
CONF_INTERVAL = 'interval'
DEFAULT_INTERVAL = 8
2015-11-22 04:04:28 +00:00
def setup_scanner(hass, config, see):
2016-03-07 17:12:06 +00:00
"""Setup the iCloud Scanner."""
from pyicloud import PyiCloudService
from pyicloud.exceptions import PyiCloudFailedLoginException
from pyicloud.exceptions import PyiCloudNoDevicesException
2016-03-07 17:12:06 +00:00
# Get the username and password from the configuration.
2015-12-15 05:39:48 +00:00
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
if username is None or password is None:
_LOGGER.error('Must specify a username and password')
return False
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)
except PyiCloudFailedLoginException as error:
2015-12-15 05:39:48 +00:00
_LOGGER.exception('Error logging into iCloud Service: %s', error)
return False
2015-12-15 05:39:48 +00:00
def keep_alive(now):
2016-03-07 20:18:53 +00:00
"""Keep authenticating iCloud connection."""
2015-12-15 05:39:48 +00:00
api.authenticate()
2015-12-15 09:42:05 +00:00
_LOGGER.info("Authenticate against iCloud")
2015-12-15 05:39:48 +00:00
track_utc_time_change(hass, keep_alive, second=0)
2015-11-22 04:04:28 +00:00
def update_icloud(now):
2016-03-07 17:12:06 +00:00
"""Authenticate against iCloud and scan for devices."""
try:
2015-11-22 04:12:41 +00:00
# The session timeouts if we are not using it so we
2015-12-15 09:42:05 +00:00
# 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:
_LOGGER.info('No iCloud Devices found!')
2015-11-22 04:04:28 +00:00
track_utc_time_change(
2015-12-15 05:39:48 +00:00
hass, update_icloud,
minute=range(0, 60, config.get(CONF_INTERVAL, DEFAULT_INTERVAL)),
second=0
2015-11-22 04:12:41 +00:00
)
return True