def detect_location_info():
    success = None

    for source in DATA_SOURCE:
        try:
            raw_info = requests.get(source, timeout=5).json()
            success = source
            break
        except (requests.RequestException, ValueError):
            success = False

    if success is False:
        return None
    else:
        data = {key: raw_info.get(key) for key in LocationInfo._fields}
        if success is DATA_SOURCE[1]:
            data['ip'] = raw_info.get('query')
            data['country_code'] = raw_info.get('countryCode')
            data['country_name'] = raw_info.get('country')
            data['region_code'] = raw_info.get('region')
            data['region_name'] = raw_info.get('regionName')
            data['zip_code'] = raw_info.get('zip')
            data['time_zone'] = raw_info.get('timezone')
            data['latitude'] = raw_info.get('lat')
            data['longitude'] = raw_info.get('lon')

    # From Wikipedia: Fahrenheit is used in the Bahamas, Belize,
    # the Cayman Islands, Palau, and the United States and associated
    # territories of American Samoa and the U.S. Virgin Islands
    data['use_fahrenheit'] = data['country_code'] in (
        'BS', 'BZ', 'KY', 'PW', 'US', 'AS', 'VI')

    return LocationInfo(**data)
Detect location information.
dataset/ETHPy150Open home-assistant/home-assistant/homeassistant/util/location.py/detect_location_info