跳转到主要内容

从WeatherFlow获取天气预报数据

项目描述

🌤️ WeatherFlow天气预报和传感器API包装器

Latest PyPI version Supported Python

Python库,用于从WeatherFlow检索预报数据。

请访问https://weatherflow.github.io/Tempest/api/swagger/获取更多信息。

安装

pyweatherflow-forecast 在PyPi上有

pip install pyweatherflow-forecast

用法

此库主要设计用于在异步环境中使用。

库的主要接口是 pyweatherflow_forecast.WeatherFlow。此接口接受最多4个选项

  • station_id: (必需) 提供您想要数据的站点的ID。
  • api_token: (必需) 为上述站点ID输入您的个人API令牌。您可以通过此处登录您的凭据获取个人使用令牌。然后单击右上角的“创建令牌”。
  • elevation: (可选) 您的站点位于海平面以上的高度(以米为单位)。如果没有提供,则默认为0。这用于一些计算传感器。
  • session: (可选) 一个现有的aiohttp.ClientSession。默认值为None,然后创建一个新的ClientSession。

示例

以下是一个异步调用的示例,其中包含从所有端点返回的数据。请注意,这里没有打印所有数据。请查看 data.py 以查看不同类及其输出值列表。

"""This module is only used to run some realtime data tests using the async functions, while developing the module.

Create a .env file and add STATION_ID with the id of your station and API_TOKEN with the personal Token.
"""
# ruff: noqa: F401
"""This module is only used to run some realtime data tests using the async functions, while developing the module.

Create a .env file and add STATION_ID with the id of your station and API_TOKEN with the personal Token.
"""
from __future__ import annotations

from dotenv import load_dotenv
import os
import asyncio
import aiohttp
import logging
import time

from pyweatherflow_forecast import (
    WeatherFlow,
    WeatherFlowStationData,
    WeatherFlowForecastData,
    WeatherFlowSensorData,
)

_LOGGER = logging.getLogger(__name__)

async def main() -> None:
    """Async test module."""

    logging.basicConfig(level=logging.DEBUG)
    start = time.time()

    load_dotenv()
    station_id = os.getenv("STATION_ID")
    api_token = os.getenv("API_TOKEN")
    elevation = 60

    session = aiohttp.ClientSession()
    weatherflow = WeatherFlow(station_id=station_id, api_token=api_token, elevation=elevation, session=session, forecast_hours=24)

    try:
        station_data: WeatherFlowStationData = await weatherflow.async_get_station()
        print("###########################################")
        print("STATION NAME: ", station_data.station_name)
        print("DEVICE ID: ", station_data.device_id)
        print("FIRMWARE: ", station_data.firmware_revision)
        print("SERIAL: ", station_data.serial_number)

    except Exception as err:
        print(err)

    try:
        sensor_data: WeatherFlowSensorData = await weatherflow.async_fetch_sensor_data()
        print("###########################################")
        print("DATA AVAILABLE:", sensor_data.data_available)
        print("TEMPERATURE:", sensor_data.air_temperature)
        print("APPARENT:", sensor_data.feels_like)
        print("WIND GUST:", sensor_data.wind_gust)
        print("LAST LIGHTNING:", sensor_data.lightning_strike_last_epoch)
        print("WIND DIRECTION: ", sensor_data.wind_direction)
        print("WIND CARDINAL: ", sensor_data.wind_cardinal)
        print("PRECIP CHECKED: ", sensor_data.precip_accum_local_day_final)
        print("ABSOLUTE HUMIDITY: ", sensor_data.absolute_humidity)
        print("VISIBILITY: ", sensor_data.visibility)
        print("BEAUFORT: ", sensor_data.beaufort)
        print("BEAUFORT: ", sensor_data.beaufort_description)
        print("FREEZING ALT: ", sensor_data.freezing_altitude)
        print("VOLTAGE: ", sensor_data.voltage)
        print("BATTERY: ", sensor_data.battery)
        print("POWER SAVE MODE: ", sensor_data.power_save_mode)
        print("IS FREEZING: ", sensor_data.is_freezing)
        print("IS LIGHTNING: ", sensor_data.is_lightning)
        print("IS RAINING: ", sensor_data.is_raining)
        print("UV INDEX: ", sensor_data.uv)
        print("UV DESCRIPTION: ", sensor_data.uv_description)
        print("STATION NAME: ", sensor_data.station_name)
        print("PRECIP INTENSITY: ", sensor_data.precip_intensity)
        print("PRECIP: ", sensor_data.precip)
        print("PRECIP TYPE: ", sensor_data.precip_type)

    except Exception as err:
        print(err)


    try:
        data: WeatherFlowForecastData = await weatherflow.async_get_forecast()
        print("TEMPERATURE: ", data.temperature)
        print("***** DAILY DATA *****")
        for item in data.forecast_daily:
            print(item.temperature, item.temp_low, item.icon, item.condition, item.precipitation_probability, item.precipitation, item.wind_bearing, item.wind_speed, item.wind_gust)
        print("***** HOURLY DATA *****")
        cnt = 1
        for item in data.forecast_hourly:
            print("**", cnt, "** ", item.datetime, item.temperature, item.apparent_temperature, item.icon, item.condition, item.precipitation, item.precipitation_probability)
            cnt += 1
    except Exception as err:
        print(err)


    if session is not None:
        await session.close()

    end = time.time()

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

asyncio.run(main())

贡献

如果您想贡献,可以使用vscode中的devcontainers进行最简单的设置。请参阅此处说明

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码发行版

pyweatherflow_forecast-1.1.0.tar.gz (14.4 kB 查看哈希值)

上传时间 源代码

构建发行版

pyweatherflow_forecast-1.1.0-py3-none-any.whl (13.8 kB 查看哈希值)

上传时间 Python 3

由以下提供支持