Giter VIP home page Giter VIP logo

py-weatherbit's Introduction

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())

py-weatherbit's People

Contributors

briis avatar lymanepp avatar mariusthvdb avatar nepozs avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.