WeatherFlow REST API的Python包装器
项目描述
🌤️ WeatherFlow REST API的Python包装器
此模块通过其REST API与WeatherFlow气象站通信。
此模块主要编写用于在Home Assistant的weatherflow
自定义集成中使用,但也可能用于其他目的。
完成此操作后,它将替换之前的模块pysmartweatherio
。
安装
pyweatherflowrest
可在PyPI上获得
pip install pyweatherflowrest
用法
此库主要设计用于在异步上下文中使用。
库的主要接口是pyweatherflowrest.WeatherFlowApiClient
。此接口需要6个选项
station_id
: (必需) 提供您要获取数据的站的站id。api_token
: (必需) 为上述站id输入您的个人api令牌。您可以通过此处并使用您的凭据登录来获取您的个人使用令牌。然后在右上角点击创建令牌。units
: (可选) 此处的有效选项是metric或imperial。WeatherFlow站总是以公制单位发送数据,因此只有在未选择公制时才会进行转换。默认值为metricforecast_hours
:(可选)指定需要检索的小时级预报的小时数。有效值在1到240之间。默认值为48小时。homeassistant
:(可选)有效选项为True或False。如果设置为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.tar.gz的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | 766dc11ed8f4176879e149b7a782ef11852200f6f9c59801d2314e7003e6641d |
|
MD5 | 2839f0039e950d218cfe7cfb4545ebe9 |
|
BLAKE2b-256 | 61f3442c08dadf2b7964871aed0b1739542e1d1fa046b557c34a4b333b538f52 |
关闭
pyweatherflowrest-1.0.11-py3-none-any.whl的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | 5d7fca829dcb97afa32695e1439c30b14a4d61ebca0bb931ab9ef7a3f0d43d1e |
|
MD5 | b944370ccf50b21dfef73f339112af9c |
|
BLAKE2b-256 | ad83f29ee52bffa7d4e5298c922bda2e02f10c13a1265e16efb871979e35e347 |