Return a set of all connected minion ids, optionally within a subset

def connected_ids(self, subset=None, show_ip=False, show_ipv4=None, include_localhost=None):
        '''
        Return a set of all connected minion ids, optionally within a subset
        '''
        if include_localhost is not None:
            salt.utils.versions.warn_until(
                'Sodium',
                'The \'include_localhost\' argument is no longer required; any'
                'connected localhost minion will always be included.'
            )
        if show_ipv4 is not None:
            salt.utils.versions.warn_until(
                'Sodium',
                'The \'show_ipv4\' argument has been renamed to \'show_ip\' as'
                'it now also includes IPv6 addresses for IPv6-connected'
                'minions.'
            )
        minions = set()
        if self.opts.get('minion_data_cache', False):
            search = self.cache.list('minions')
            if search is None:
                return minions
            addrs = salt.utils.network.local_port_tcp(int(self.opts['publish_port']))
            if '127.0.0.1' in addrs:
                # Add in the address of a possible locally-connected minion.
                addrs.discard('127.0.0.1')
                addrs.update(set(salt.utils.network.ip_addrs(include_loopback=False)))
            if '::1' in addrs:
                # Add in the address of a possible locally-connected minion.
                addrs.discard('::1')
                addrs.update(set(salt.utils.network.ip_addrs6(include_loopback=False)))
            if subset:
                search = subset
            for id_ in search:
                try:
                    mdata = self.cache.fetch('minions/{0}'.format(id_), 'data')
                except SaltCacheError:
                    # If a SaltCacheError is explicitly raised during the fetch operation,
                    # permission was denied to open the cached data.p file. Continue on as
                    # in the releases <= 2016.3. (An explicit error raise was added in PR
                    # #35388. See issue #36867 for more information.
                    continue
                if mdata is None:
                    continue
                grains = mdata.get('grains', {})
                for ipv4 in grains.get('ipv4', []):
                    if ipv4 in addrs:
                        if show_ip:
                            minions.add((id_, ipv4))
                        else:
                            minions.add(id_)
                        break
                for ipv6 in grains.get('ipv6', []):
                    if ipv6 in addrs:
                        if show_ip:
                            minions.add((id_, ipv6))
                        else:
                            minions.add(id_)
                        break
        return minions