|
楼主 |
发表于 2021-2-24 09:40:00
|
显示全部楼层
加了个配置文件,存储获得的参数
使用 ConfigParser
配置文件如下:
[Param]
timestamp = XXXXX
noncestr = XXXXX
sign = XXXXX
初始化的时候读配置文件,每次更新写配置文件;
def setup_platform(hass, config, add_devices, discovery_info=None):
sensor_name = config.get(CONF_NAME)
openid = config.get(OPENID)
unionid = config.get(UNIONID)
cfg = configparser.ConfigParser()
cfg.read('param.ini')
timestamp1 = cfg.get('Param', 'timestamp')
noncestr = cfg.get('Param', 'noncestr')
sign = cfg.get('Param', 'sign')
url = API_URL + "?openid=" + openid +"×tamp=" + timestamp1 + "&noncestr=" + noncestr + "&sign=" + sign + "&unionid=" + unionid + "&userInfo=null"
add_devices([GJDW(sensor_name,url,openid,unionid,cfg)])
update 函数中添加
self._cfg.set("Param", "timestamp", ret["timestamp"])
self._cfg.set("Param", "noncestr", ret["noncestr"])
self._cfg.set("Param", "sign", ret["sign"])
self._cfg.write(open('param.ini','w'))
我使用还是很稳定的,期间可能会出现参数错误,但是后续还是可以执行的,可忽略这次报错。只要不是连续报错就行
|
|