original_docstring:  Parse and process the known devices file. 
docstring: Parse and process the known devices file.
docstring_tokens: Parse and process the known devices file .
code: def _read_known_devices_file(self):
          """ Parse and process the known devices file. """
  
          # Read known devices if file exists
          if os.path.isfile(self.path_known_devices_file):
              self.lock.acquire()
  
              known_devices = {}
  
              with open(self.path_known_devices_file) as inp:
                  default_last_seen = datetime(1990, 1, 1)
  
                  # Temp variable to keep track of which entity ids we use
                  # so we can ensure we have unique entity ids.
                  used_entity_ids = []
  
                  try:
                      for row in csv.DictReader(inp):
                          device = row['device']
  
                          row['track'] = True if row['track'] == '1' else False
  
                          if row['picture']:
                              row['default_state_attr'] = {
                                  components.ATTR_ENTITY_PICTURE: row['picture']}
  
                          else:
                              row['default_state_attr'] = None
  
                          # If we track this device setup tracking variables
                          if row['track']:
                              row['last_seen'] = default_last_seen
  
                              # Make sure that each device is mapped
                              # to a unique entity_id name
                              name = util.slugify(row['name']) if row['name'] \
                                  else "unnamed_device"
  
                              entity_id = ENTITY_ID_FORMAT.format(name)
                              tries = 1
  
                              while entity_id in used_entity_ids:
                                  tries += 1
  
                                  suffix = "_{}".format(tries)
  
                                  entity_id = ENTITY_ID_FORMAT.format(
                                      name + suffix)
  
                              row['entity_id'] = entity_id
                              used_entity_ids.append(entity_id)
  
                              row['picture'] = row['picture']
  
                          known_devices[device] = row
  
                      if not known_devices:
                          self.logger.warning(
                              "No devices to track. Please update {}.".format(
                                  self.path_known_devices_file))
  
                      # Remove entities that are no longer maintained
                      new_entity_ids = set([known_devices[device]['entity_id']
                                            for device in known_devices
                                            if known_devices[device]['track']])
  
                      for entity_id in \
                              self.device_entity_ids - new_entity_ids:
  
                          self.logger.info(
                              "DeviceTracker:Removing entity {}".format(
                                  entity_id))
                          self.states.remove(entity_id)
  
                      # File parsed, warnings given if necessary
                      # entities cleaned up, make it available
                      self.known_devices = known_devices
  
                      self.logger.info(
                          "DeviceTracker:Loaded devices from {}".format(
                              self.path_known_devices_file))
  
                  except KeyError:
                      self.invalid_known_devices_file = True
                      self.logger.warning((
                          "Invalid known devices file: {}. "
                          "We won't update it with new found devices."
                          ).format(self.path_known_devices_file))
  
                  finally:
                      self.lock.release()
code_tokens: def _read_known_devices_file ( self ) : if os . path . isfile ( self . path_known_devices_file ) : self . lock . acquire ( ) known_devices = { } with open ( self . path_known_devices_file ) as inp : default_last_seen = datetime ( 1990 , 1 , 1 ) used_entity_ids = [ ] try : for row in csv . DictReader ( inp ) : device = row [ 'device' ] row [ 'track' ] = True if row [ 'track' ] == '1' else False if row [ 'picture' ] : row [ 'default_state_attr' ] = { components . ATTR_ENTITY_PICTURE : row [ 'picture' ] } else : row [ 'default_state_attr' ] = None if row [ 'track' ] : row [ 'last_seen' ] = default_last_seen name = util . slugify ( row [ 'name' ] ) if row [ 'name' ] else "unnamed_device" entity_id = ENTITY_ID_FORMAT . format ( name ) tries = 1 while entity_id in used_entity_ids : tries += 1 suffix = "_{}" . format ( tries ) entity_id = ENTITY_ID_FORMAT . format ( name + suffix ) row [ 'entity_id' ] = entity_id used_entity_ids . append ( entity_id ) row [ 'picture' ] = row [ 'picture' ] known_devices [ device ] = row if not known_devices : self . logger . warning ( "No devices to track. Please update {}." . format ( self . path_known_devices_file ) ) new_entity_ids = set ( [ known_devices [ device ] [ 'entity_id' ] for device in known_devices if known_devices [ device ] [ 'track' ] ] ) for entity_id in self . device_entity_ids - new_entity_ids : self . logger . info ( "DeviceTracker:Removing entity {}" . format ( entity_id ) ) self . states . remove ( entity_id ) self . known_devices = known_devices self . logger . info ( "DeviceTracker:Loaded devices from {}" . format ( self . path_known_devices_file ) ) except KeyError : self . invalid_known_devices_file = True self . logger . warning ( ( "Invalid known devices file: {}. " "We won't update it with new found devices." ) . format ( self . path_known_devices_file ) ) finally : self . lock . release ( )
short_docstring: Parse and process the known devices file.
short_docstring_tokens: Parse and process the known devices file .
comment: """ Parse and process the known devices file. """$SEP$# Read known devices if file exists$SEP$# Temp variable to keep track of which entity ids we use$SEP$# so we can ensure we have unique entity ids.$SEP$# If we track this device setup tracking variables$SEP$# Make sure that each device is mapped$SEP$# to a unique entity_id name$SEP$# Remove entities that are no longer maintained$SEP$# File parsed, warnings given if necessary$SEP$# entities cleaned up, make it available