-
-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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
+38
−25
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4b33370
Improving icloud device tracker
evgeniy-khatko e446e97
Adding config validations for new values
OlgaKhatko c23ed27
Adding config validations for new values
evgeniy-khatko 8a9c5be
Adding config validations.
evgeniy-khatko 2303593
Moving icloud specific setup to platform schema. Setting default in p…
evgeniy-khatko File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
@@ -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) | ||
gps_accuracy_threshold = config.get(CONF_GPS_ACCURACY_THRESHOLD, 1000) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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) | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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: | ||
|
@@ -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: | ||
|
@@ -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") | ||
|
||
|
@@ -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: | ||
|
@@ -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") | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is not validated -> https://developers.home-assistant.io/docs/en/creating_platform_code_review.html#3-configuration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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.