'Perform the proximity checking.'

def check_proximity_state_change(self, entity, old_state, new_state):

    entity_name = new_state.name
    devices_to_calculate = False
    devices_in_zone = ''
    zone_state = self.hass.states.get(self.proximity_zone)
    proximity_latitude = zone_state.attributes.get('latitude')
    proximity_longitude = zone_state.attributes.get('longitude')
    for device in self.proximity_devices:
        device_state = self.hass.states.get(device)
        if (device_state is None):
            devices_to_calculate = True
            continue
        if (device_state.state not in self.ignored_zones):
            devices_to_calculate = True
        if (device_state.state.lower() == self.friendly_name.lower()):
            device_friendly = device_state.name
            if (devices_in_zone != ''):
                devices_in_zone = (devices_in_zone + ',    ')
            devices_in_zone = (devices_in_zone + device_friendly)
    if (not devices_to_calculate):
        self.dist_to = 'not    set'
        self.dir_of_travel = 'not    set'
        self.nearest = 'not    set'
        self.schedule_update_ha_state()
        return
    if (devices_in_zone != ''):
        self.dist_to = 0
        self.dir_of_travel = 'arrived'
        self.nearest = devices_in_zone
        self.schedule_update_ha_state()
        return
    if ('latitude' not in new_state.attributes):
        return
    distances_to_zone = {}
    for device in self.proximity_devices:
        device_state = self.hass.states.get(device)
        if (device_state.state in self.ignored_zones):
            continue
        if ('latitude' not in device_state.attributes):
            continue
        dist_to_zone = distance(proximity_latitude, proximity_longitude, device_state.attributes['latitude'], device_state.attributes['longitude'])
        distances_to_zone[device] = round(convert(dist_to_zone, 'm', self.unit_of_measurement), 1)
    closest_device = None
    dist_to_zone = None
    for device in distances_to_zone:
        if ((not dist_to_zone) or (distances_to_zone[device] < dist_to_zone)):
            closest_device = device
            dist_to_zone = distances_to_zone[device]
    if (closest_device != entity):
        self.dist_to = round(distances_to_zone[closest_device])
        self.dir_of_travel = 'unknown'
        device_state = self.hass.states.get(closest_device)
        self.nearest = device_state.name
        self.schedule_update_ha_state()
        return
    if ((old_state is None) or ('latitude' not in old_state.attributes)):
        self.dist_to = round(distances_to_zone[entity])
        self.dir_of_travel = 'unknown'
        self.nearest = entity_name
        self.schedule_update_ha_state()
        return
    distance_travelled = 0
    old_distance = distance(proximity_latitude, proximity_longitude, old_state.attributes['latitude'], old_state.attributes['longitude'])
    new_distance = distance(proximity_latitude, proximity_longitude, new_state.attributes['latitude'], new_state.attributes['longitude'])
    distance_travelled = round((new_distance - old_distance), 1)
    if (distance_travelled < (self.tolerance * (-1))):
        direction_of_travel = 'towards'
    elif (distance_travelled > self.tolerance):
        direction_of_travel = 'away_from'
    else:
        direction_of_travel = 'stationary'
    self.dist_to = round(dist_to_zone)
    self.dir_of_travel = direction_of_travel
    self.nearest = entity_name
    self.schedule_update_ha_state()
    _LOGGER.debug('proximity.%s    update    entity:    distance=%s:    direction=%s:    device=%s', self.friendly_name, round(dist_to_zone), direction_of_travel, entity_name)
    _LOGGER.info('%s:    proximity    calculation    complete', entity_name)
