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更改任何內容,或阻止其第一個派對應用程序之外的使用,則該庫將停止工作。