Python 3库,用于使用NetAtmo API管理Vaillant恒温器。它提供了Vaillant的NetAtmo API一对一的映射,并提供与官方Vaillant VSMART/ERELAX应用相似的功能。
注意:此库仍处于预发行状态,直到v1.0.0。在任何v0.xy版本中,公共API可能会发生破坏。
可以简单地使用PIP安装库。
pip install vaillant-netatmo-api库需要Python 3,并且具有韧性和HTTPX依赖性。
注意:HTTPX当前是预发行软件。
requirements.txt中概述的版本应正常工作,但是如果有一些破坏更改,请检查其GitHub问题跟踪器是否已知问题。
所有NetAtmo API都受到保护,需要一个携带者令牌才能进行身份验证。为了获得这个令牌,Netatmo提供了OAuth API。
由于Vaillant使用资源所有者密码凭据赠款,因此AuthClient API中只有一种方法:
async_token :获取一个携带者令牌并将其存储在令牌商店中 from vaillant_netatmo_api import auth_client
CLIENT_ID = ""
CLIENT_SECRET = ""
def handle_token_update ( token ):
token_string = token . serialize ()
write_to_storage ( token_string )
async with auth_client ( CLIENT_ID , CLIENT_SECRET , handle_token_update ) as client :
await client . async_token (
username ,
password ,
user_prefix ,
app_version ,
) ThermostatClient有三个API,所有这些都需要携带者令牌进行身份验证:
async_get_thermostats_data :获取与用户帐户关联的所有设备async_set_system_mode :设备和模块的更改系统模式(即夏季,冬季或霜冻)async_set_minor_mode :更改设备和模块的次要模式(即手动模式,外出模式或热水增压模式)async_sync_schedule :更新设备和模块的时间表数据async_switch_schedule :更改设备和模块的主动时间表 from vaillant_netatmo_api import thermostat_client , SystemMode , Token
CLIENT_ID = ""
CLIENT_SECRET = ""
token_string = read_from_storage ()
token = Token . deserialize ( token_string )
def handle_token_update ( token ):
token_string = token . serialize ()
write_to_storage ( token_string )
async with thermostat_client ( CLIENT_ID , CLIENT_SECRET , token , handle_token_update ) as client :
devices = await client . async_get_thermostats_data ()
d_id = devices [ 0 ]. id
m_id = devices [ 0 ]. modules [ 0 ]. id
await client . async_set_system_mode ( d_id , m_id , SystemMode . WINTER )即使Library提供了上下文管理器,用于使用AuthClient和ThermostatClient ,但这仅在开发过程中或在非常不经常使用的情况下进行。
两个客户端都使用httpx.AsyncClient作为基础HTTP通信库,该库实现了连接池和连接重用。这意味着应使用AuthClient或ThermostatClient的同一实例来完成多个同意请求,这是不可能通过使用上下文管理器API来完成的,因为此API每次调用auth_client或thermostat_client方法都会返回客户端的新实例。
为了实现最佳用法(将利用连接池和连接重复使用),应通过实例化客户端并在构造函数中提供httpx.AsyncClient实例来使用AuthClient和ThermostatClient 。提供的客户端应用作Singleton,或与其他某些上下文管理机制一起使用,其上下文宽于一个代码或一个入站请求。
这是在家庭助理中使用的示例。
# When setting up integration with all the devices of one account, instantiate and store the client in a configuration memory store
async def async_setup_entry ( hass : HomeAssistant , entry : ConfigEntry ) -> bool :
client = get_async_client ( hass )
token_store = TokenStore (
entry . data [ CONF_CLIENT_ID ],
entry . data [ CONF_CLIENT_SECRET ],
token ,
handle_token_update ,
)
hass . data [ DOMAIN ][ entry . entry_id ] = ThermostatClient ( client , token_store )
hass . config_entries . async_setup_platforms ( entry , PLATFORMS )
return True
# When unloading the integration of this same account, read the client and close it manually
async def async_unload_entry ( hass : HomeAssistant , entry : ConfigEntry ) -> bool :
unload_ok = await hass . config_entries . async_unload_platforms ( entry , PLATFORMS )
if unload_ok :
hass . data [ DOMAIN ]. pop ( entry . entry_id )
return unload_ok当将此库集成到其他应用程序(烧瓶,django或类似)中时,应使用代表某种应用程序上下文的类似挂钩。
如果不通过以下项目实现以前的实现,则该库将不存在:
他们通过探索和记录API来为基金会奠定基础。
该库以任何方式与Vaillant或Netatmo无关。如果Vaillant或Netatmo决定使用API更改任何内容,或阻止其第一个派对应用程序之外的使用,则该库将停止工作。