From 2e0042adb017f9bbc7322809aba522f121f66c04 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen <paulus@paulusschoutsen.nl> Date: Mon, 14 Dec 2015 21:39:48 -0800 Subject: [PATCH] Tweak iCloud device tracker --- .coveragerc | 1 + .../components/device_tracker/icloud.py | 32 +++++++++++++------ requirements_all.txt | 6 ++-- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/.coveragerc b/.coveragerc index 7192883b655..94f4352ef89 100644 --- a/.coveragerc +++ b/.coveragerc @@ -41,6 +41,7 @@ omit = homeassistant/components/device_tracker/ddwrt.py homeassistant/components/device_tracker/fritz.py homeassistant/components/device_tracker/geofancy.py + homeassistant/components/device_tracker/icloud.py homeassistant/components/device_tracker/luci.py homeassistant/components/device_tracker/ubus.py homeassistant/components/device_tracker/netgear.py diff --git a/homeassistant/components/device_tracker/icloud.py b/homeassistant/components/device_tracker/icloud.py index a4adaa547bc..a617fda9411 100644 --- a/homeassistant/components/device_tracker/icloud.py +++ b/homeassistant/components/device_tracker/icloud.py @@ -20,14 +20,15 @@ import re from homeassistant.const import CONF_USERNAME, CONF_PASSWORD from homeassistant.helpers.event import track_utc_time_change -SCAN_INTERVAL = 1800 - _LOGGER = logging.getLogger(__name__) REQUIREMENTS = ['https://github.com/picklepete/pyicloud/archive/' '80f6cd6decc950514b8dc43b30c5bded81b34d5f.zip' '#pyicloud==0.8.0'] +CONF_INTERVAL = 'interval' +DEFAULT_INTERVAL = 8 + def setup_scanner(hass, config, see): """ @@ -38,8 +39,12 @@ def setup_scanner(hass, config, see): from pyicloud.exceptions import PyiCloudNoDevicesException # Get the username and password from the configuration - username = config[CONF_USERNAME] - password = config[CONF_PASSWORD] + 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 try: _LOGGER.info('Logging into iCloud Account') @@ -48,11 +53,18 @@ def setup_scanner(hass, config, see): password, verify=True) except PyiCloudFailedLoginException as error: - _LOGGER.exception( - 'Error logging into iCloud Service: %s' % error - ) + _LOGGER.exception('Error logging into iCloud Service: %s', error) return + def keep_alive(now): + """ + Keeps authenticating icloud connection + """ + api.authenticate() + _LOGGER.info("Authenticate against iCloud.") + + track_utc_time_change(hass, keep_alive, second=0) + def update_icloud(now): """ Authenticate against iCloud and scan for devices. @@ -83,7 +95,7 @@ def setup_scanner(hass, config, see): _LOGGER.info('No iCloud Devices found!') track_utc_time_change( - hass, - update_icloud, - second=range(0, 60, SCAN_INTERVAL) + hass, update_icloud, + minute=range(0, 60, config.get(CONF_INTERVAL, DEFAULT_INTERVAL)), + second=0 ) diff --git a/requirements_all.txt b/requirements_all.txt index c9835294a20..48a127b0f30 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -9,6 +9,9 @@ jinja2>=2.8 # homeassistant.components.arduino PyMata==2.07a +# homeassistant.components.device_tracker.icloud +https://github.com/picklepete/pyicloud/archive/80f6cd6decc950514b8dc43b30c5bded81b34d5f.zip#pyicloud==0.8.0 + # homeassistant.components.device_tracker.netgear pynetgear==0.3 @@ -177,6 +180,3 @@ https://github.com/persandstrom/python-verisure/archive/9873c4527f01b1ba1f72ae60 # homeassistant.components.zwave pydispatcher==2.0.5 - -# homeassistant.sensor.icloud -https://github.com/picklepete/pyicloud/archive/80f6cd6decc950514b8dc43b30c5bded81b34d5f.zip#pyicloud==0.8.0 -- GitLab