跳转到主要内容

WeatherFlow REST API的Python包装器

项目描述

🌤️ WeatherFlow REST API的Python包装器

Latest PyPI version Supported Python

此模块通过其REST API与WeatherFlow气象站通信。

此模块主要编写用于在Home Assistant的weatherflow自定义集成中使用,但也可能用于其他目的。

完成此操作后,它将替换之前的模块pysmartweatherio

安装

pyweatherflowrest可在PyPI上获得

pip install pyweatherflowrest

用法

此库主要设计用于在异步上下文中使用。

库的主要接口是pyweatherflowrest.WeatherFlowApiClient。此接口需要6个选项

  • station_id: (必需) 提供您要获取数据的站的站id。
  • api_token: (必需) 为上述站id输入您的个人api令牌。您可以通过此处并使用您的凭据登录来获取您的个人使用令牌。然后在右上角点击创建令牌。
  • units: (可选) 此处的有效选项是metricimperial。WeatherFlow站总是以公制单位发送数据,因此只有在未选择公制时才会进行转换。默认值为metric
  • forecast_hours:(可选)指定需要检索的小时级预报的小时数。有效值在1到240之间。默认值为48小时
  • homeassistant:(可选)有效选项为TrueFalse。如果设置为True,则某些单位类型将不会被转换,因为Home Assistant会处理这个问题。默认值为False
  • session:(可选)现有的aiohttp.ClientSession。默认值为None,然后将会创建一个新的ClientSession。
import asyncio
import logging
import time

from pyweatherflowrest.api import WeatherFlowApiClient
from pyweatherflowrest.data import ObservationDescription, StationDescription, ForecastDescription, ForecastDailyDescription
from pyweatherflowrest.exceptions import WrongStationID, Invalid, NotAuthorized, BadRequest

_LOGGER = logging.getLogger(__name__)

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

    weatherflow = WeatherFlowApiClient("YOUR STATION ID", "YOUR TOKEN")
    try:
        await weatherflow.initialize() # Must be the first call

    except WrongStationID as err:
        _LOGGER.debug(err)
    except Invalid as err:
        _LOGGER.debug(err)
    except NotAuthorized as err:
        _LOGGER.debug(err)
    except BadRequest as err:
        _LOGGER.debug(err)

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

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


    data: ForecastDescription = await weatherflow.update_forecast()
    if data is not None:
        for field in data.__dataclass_fields__:
            value = getattr(data, field)
            if field == "forecast_daily":
                for item in value:
                    print(item.conditions, item.air_temp_high)
            elif field == "forecast_hourly":
                for item in value:
                    print(item.conditions, item.air_temperature)
            else:
                print(field,"-", value)

    end = time.time()

    await weatherflow.req.close()

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

asyncio.run(main())

项目详情


下载文件

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

源分布

pyweatherflowrest-1.0.11.tar.gz (15.6 kB 查看散列值

上传时间

构建分布

pyweatherflowrest-1.0.11-py3-none-any.whl (14.4 kB 查看散列值

上传时间 Python 3

由以下机构支持

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF 赞助商 PingdomPingdom 监控 SentrySentry 错误记录 StatusPageStatusPage 状态页面