device_tracker - No longer keep writing same new devices to known_devices.csv

This commit is contained in:
Paulus Schoutsen 2014-12-12 07:32:50 -08:00
parent 81be3811dc
commit 7c404a0551

View File

@ -177,38 +177,41 @@ class DeviceTracker(object):
# Did we find any devices that we didn't know about yet? # Did we find any devices that we didn't know about yet?
new_devices = found_devices - self.untracked_devices new_devices = found_devices - self.untracked_devices
# Write new devices to known devices file if new_devices:
if not self.invalid_known_devices_file and new_devices: self.untracked_devices.update(new_devices)
known_dev_path = self.hass.get_config_path(KNOWN_DEVICES_FILE) # Write new devices to known devices file
if not self.invalid_known_devices_file:
try: known_dev_path = self.hass.get_config_path(KNOWN_DEVICES_FILE)
# If file does not exist we will write the header too
is_new_file = not os.path.isfile(known_dev_path)
with open(known_dev_path, 'a') as outp: try:
_LOGGER.info( # If file does not exist we will write the header too
"Found %d new devices, updating %s", is_new_file = not os.path.isfile(known_dev_path)
len(new_devices), known_dev_path)
writer = csv.writer(outp) with open(known_dev_path, 'a') as outp:
_LOGGER.info(
"Found %d new devices, updating %s",
len(new_devices), known_dev_path)
if is_new_file: writer = csv.writer(outp)
writer.writerow((
"device", "name", "track", "picture"))
for device in new_devices: if is_new_file:
# See if the device scanner knows the name writer.writerow((
# else defaults to unknown device "device", "name", "track", "picture"))
name = (self.device_scanner.get_device_name(device)
or "unknown_device")
writer.writerow((device, name, 0, "")) for device in new_devices:
# See if the device scanner knows the name
# else defaults to unknown device
name = (self.device_scanner.get_device_name(device)
or "unknown_device")
except IOError: writer.writerow((device, name, 0, ""))
_LOGGER.exception(
"Error updating %s with %d new devices", except IOError:
known_dev_path, len(new_devices)) _LOGGER.exception(
"Error updating %s with %d new devices",
known_dev_path, len(new_devices))
self.lock.release() self.lock.release()