2 回答
TA贡献2012条经验 获得超12个赞
在循环期间更改 internal_dict_value 的值的问题是您不知道它将保存什么类型。尝试类似的东西if type(internal_dict_value) is dict:
TA贡献1821条经验 获得超6个赞
这是我的(全功能)python 脚本的最终版本。
import requests,json
bridgeIP = "IP/FQDN_Here:port_here"
userID = "userHash_here"
lightID = "2"
def lambda_handler(event, lambda_context):
url = f"http://{bridgeIP}/api/{userID}/lights/{lightID}"
r = requests.get(url)
data = json.loads(r.text)
def nested_get(input_dict, nested_key):
internal_dict_value = input_dict
for k in nested_key:
if type(internal_dict_value) is dict:
internal_dict_value = internal_dict_value.get(k, None)
if internal_dict_value is None:
return None
return internal_dict_value
bulbStatus = nested_get(data,["state","on"])
if bulbStatus == False:
requests.put(f"{url}/state",json.dumps({"on":True}))
elif bulbStatus == True:
requests.put(f"{url}/state",json.dumps({"on":False}))
return {
'statusCode': 200,
'body': json.dumps('Mission accomplished!')
}
添加回答
举报