Skip to main content

Python Wrapper for WeatherBit Rest API

Project description

Python Wrapper for WeatherBit API

Latest PyPI version Supported Python

This module communicates with WeatherBit.io using their REST API. The module is only supporting the Free Tier API's, for which there is more information here.

For the Free Tier, data can be retrieved for the following:

  • Current Conditions
  • Forecast Data - Daily data.
  • Severe Weather Alerts

It will require an API Key to work, which can be retrieved for free at Weatherbit

The module is primarily written for the purpose of being used in Home Assistant for the Custom Integration called weatherbit but might be used for other purposes also.

Install

pyweatherbitdata is avaible on PyPi:

pip install pyweatherbitdata

Usage

This library is primarily designed to be used in an async context.

The main interface for the library is the pyweatherbitdata.WeatherBitApiClient. This interface takes 7 options:

  • api_key: (required) A Personal API Key retrieved from WeatherBit (See above).
  • latitude: (required) Latitude of the location needing data from.
  • longitude: (required) Longitude of the location needing data from.
  • units: (optional) Valid options here are metric or imperial. This module is set to always retrieve data in metric units, so conversion of values will only take place if if metric is not selected. Default value is metric
  • language: (optional) The language for all text values retrieved from WeatherBit. Default is en
  • homeassistant: (optional) Valid options are True or False. If set to True, there will be some unit types that will not be converted, as Home Assistant will take care of that. Default value is True

Example program

"""Test Program."""
from __future__ import annotations

import asyncio
import logging
import time

from pyweatherbitdata.api import WeatherBitApiClient
from pyweatherbitdata.data import (
    ObservationDescription,
    BaseDataDescription,
    ForecastDescription
)
from pyweatherbitdata.exceptions import (
    InvalidApiKey,
    RequestError,
    ResultError,
)

_LOGGER = logging.getLogger(__name__)

async def main() -> None:
    logging.basicConfig(level=logging.DEBUG)
    start = time.time()

    weatherbit = WeatherBitApiClient(
        "YOU_API_KEY",
        55.625053,
        12.136619,
        language="da",
    )
    try:
        await weatherbit.initialize()

    except InvalidApiKey as err:
        _LOGGER.debug(err)
    except RequestError as err:
        _LOGGER.debug(err)
    except ResultError as err:
        _LOGGER.debug(err)

    data: BaseDataDescription = weatherbit.station_data
    if data is not None:
        for field in data.__dataclass_fields__:
            value = getattr(data, field)
            print(field, "-", value)

    data: ObservationDescription = await weatherbit.update_sensors()
    if data is not None:
        for field in data.__dataclass_fields__:
            value = getattr(data, field)
            print(field, "-", value)

    data: ForecastDescription = await weatherbit.update_forecast()
    if data is not None:
        for field in data.__dataclass_fields__:
            value = getattr(data, field)
            print(field, "-", value)

    end = time.time()

    await weatherbit.req.close()

    _LOGGER.info("Execution time: %s seconds", end - start)

asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyweatherbitdata-1.0.9.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyweatherbitdata-1.0.9-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file pyweatherbitdata-1.0.9.tar.gz.

File metadata

  • Download URL: pyweatherbitdata-1.0.9.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pyweatherbitdata-1.0.9.tar.gz
Algorithm Hash digest
SHA256 2bdb6eb9fe275630afdfb2e7f98ee5d5d30b825c83b78f8413dbe78bcc5f8184
MD5 4bea70c9366a2004433422b0a34c0f49
BLAKE2b-256 941c39c0c741b71c98a36d736ca53d0e77dae450a06f2642fae05760e1921dd4

See more details on using hashes here.

File details

Details for the file pyweatherbitdata-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: pyweatherbitdata-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pyweatherbitdata-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 818c3a321da82d8b0432aa8bd2df3e090e7abe0c8b7358690fd3fd81e4830d17
MD5 eb6f97afd0ec613a35c00567bf8ae782
BLAKE2b-256 600a88530e0875604d99b36a2abbff8037d0a68833155974c1818f2d0f23c868

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page