OpenWeather
This Python class allows you to make calls to OpenWeather’s One Call API.
Access to the API is controlled by key. You will need to register for developer access in order to obtain your key.
Note
This class does not parse the incoming data, which is highly complex. It is up to your application to extract the data you require.
Method Chaining
Most methods return a reference to the driver instance (self
) to allow method chaining with dot syntax:
ow.set_units("metric")
.set_language("fr")
.exclude(["alerts"])
.request_forecast(secrets["lat"], secrets["lng"])
Source Code
You can find the source code on GitHub.
Class Constructor
- class OpenWeather(request_object, key, do_debug=False)
To instantiate an
OpenWeather
object, pass in aRequests
module and your OpenWeather API key.By default, the library will requests forecast data in metric units and in the English language. Use the methods set_units() and set_language() to change this behaviour.
- Parameters:
request_object (Requests object) – A Requests object.
key (String) – Your OpenWeather API key.
do_debug (Boolean) – Emit additional debug messages. Default:
False
.
Examples
# CircuitPython
import adafruit_requests as requests
open_weather = OpenWeather(requests, secrets["apikey"], True)
# MicroPython
import urequests as requests
open_weather = OpenWeather(requests, secrets["apikey"], True)
Class Methods
- OpenWeather.request_forecast(latitude, longitude)
This method is called to get the current weather forecast at the specified location.
The dictionary returned by the method contains one of two keys:
data
if the request was successful, in which case it will contain the forecast returned by the API, orerr
if the request failed. The value oferr
is a error message string.- Parameters:
latitude (Float) – The target location’s latitude co-ordinate.
longitude (Float) – The target location’s longitude co-ordinate.
- Returns:
Dictionary with keys
data
orerr
.
- OpenWeather.set_units(requested_units='standard')
Specify the units of measurement subsequent forecasts will use. Options are
standard
,metric
andimperial
. The last two choices are self-explanatory. If you choosestandard
, which is the default, OpenWeather will return units appropriate to the target location. For example, European locations will use metric measurements, but US locations will use Imperial measurements.- Parameters:
requested_units (String) – The type of unit. Default:
standard
.- Returns:
The instance (
self
).
- OpenWeather.set_language(language='en')
OpenWeather supports a wide range of languages, and this method sets the preferred language for subsequent forecasts.
- Parameters:
language (String) – A language code. Default:
en
.- Returns:
The instance (
self
).
- OpenWeather.exclude(list)
Select data items to be excluded from the forecast. This can be useful if you need to receive only a subset of forecast data, the better to reduce the memory footprint of returned data.
Data sections that you can exclude are:
current
,minutely
,hourly
,daily
andalerts
.- Parameters:
list (Array of strings) – The items to exclude. Default: none.
- Returns:
The instance (
self
).
Example
# We only want the next hour's forecast,
# so exclude other data sets
open_weather.exclude(["alerts", "minutely", "daily", "current"])