用于访问openHAB REST API的python库
项目描述
用于访问openHAB REST API的python库
此库允许轻松访问openHAB REST API。实现了一些功能,但并非全部,这是一个正在进行中的工作。
需求
- python >= 3.8
- python :: dateutil
- python :: httpx
- python :: authlib
- openHAB版本 3 / 4
安装
使用pip安装最新版本
pip install python-openhab
示例
此库的示例用法
from openhab import OpenHAB
base_url = 'http://localhost:8080/rest'
openhab = OpenHAB(base_url)
# fetch all items
items = openhab.fetch_all_items()
sunset = items.get('Sunset')
print(sunset.state)
# fetch a single item
item = openhab.get_item('light_switch')
# turn a switch on
item.on()
# send a state update (this only update the state)
item.state = 'OFF'
# send a command
item.command('ON')
# check if item state is NULL
if item.state is None and item.is_state_null():
pass
# check if item state is UNDEF
if item.state is None and item.is_state_undef():
pass
# fetch some group
lights_group = openhab.get_item('lights_group')
# send command to group
lights_group.on()
# send update to each member
for v in lights_group.members.values():
v.update('OFF')
关于NULL和UNDEF的说明
在openHAB中,项目可能有两种状态名为NULL和UNDEF,它们有不同的含义,但基本上表示项目没有可用的值。此库将项目的状态设置为None,无论其openHAB值为NULL或UNDEF,以简化与库的工作。这是因为我们将某些类型转换为原生类型。
为了检查项目状态是否为NULL或UNDEF,您可以使用辅助函数
item.is_state_null()
item.is_state_undef()
实验性OAuth2支持
为了尝试OAuth2身份验证,您首先需要在openHAB端点注册,以获取令牌和刷新令牌。
假设您的openHAB实例运行在 http://127.0.0.1:8080 (请替换为正确的地址),使用以下代码片段来获取令牌
import pathlib
import openhab.oauth2_helper
import os
import json
url_base = 'http://127.0.0.1:8080'
api_username = 'admin'
api_password = 'admin'
oauth2_client_id = 'http://127.0.0.1/auth'
oauth2_token_cache = pathlib.Path(__file__).resolve().parent / '.oauth2_token_test'
# this must be set for oauthlib to work on http (do not set for https!)
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
oauth2_token = openhab.oauth2_helper.get_oauth2_token(url_base, username=api_username, password=api_password)
with oauth2_token_cache.open('w') as fhdl:
json.dump(oauth2_token, fhdl, indent=2, sort_keys=True)
返回的JSON是使用OAuth2认证openHAB所必需的,同时还有一个用于刷新会话的刷新令牌。
接下来尝试使用以下方式连接到openHAB
import openhab
import pathlib
import json
import os
url_base = 'http://127.0.0.1:8080'
url_rest = f'{url_base}/rest'
oauth2_client_id = 'http://127.0.0.1/auth'
oauth2_token_cache = pathlib.Path(__file__).resolve().parent / '.oauth2_token_test'
# this must be set for oauthlib to work on http (do not set for https!)
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
oauth2_config = {'client_id': oauth2_client_id,
'token_cache': str(oauth2_token_cache)
}
with oauth2_token_cache.open('r') as fhdl:
oauth2_config['token'] = json.load(fhdl)
oh = openhab.OpenHAB(base_url=url_rest, oauth2_config=oauth2_config)
o = oh.get_item('test_item')
print(o)
项目详情
下载文件
下载您平台对应的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分布
python-openhab-2.19.0.tar.gz (32.6 kB 查看哈希值)
构建分布
python_openhab-2.19.0-py3-none-any.whl (34.8 kB 查看哈希值)
关闭
python-openhab-2.19.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 2aee8264588be3b5d9a5d870a73cbfca157097b9dc7438c76bf57876ed0cd265 |
|
MD5 | 71571a67c182c245c8826dc406e9810b |
|
BLAKE2b-256 | da08ef9e05ce8297b5d244e6786e495c78bc75fc0518d65a34dc6f40cc925efc |
关闭
python_openhab-2.19.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 7c22a41e8849a1de33099daf09f4faca186e1762f9eeb298387afc9cb89f72f5 |
|
MD5 | b0de8f7aeb1711ef0428013b036543b3 |
|
BLAKE2b-256 | 7d6400e5d4f6b3b887ef4c4d1e9b6ecfa51efb79d4fcf63a8254d8da9d9f25e7 |