AWS Lambda ERROR: AttributeError 'list' object has no attribute 'get'

前端 未结 2 1672
悲&欢浪女
悲&欢浪女 2021-01-29 05:17

I have a Lambda function that is designed to turn ON/OFF my Philip HUE lightbulbs. I am able to execute the python script & it runs (error-free) on my local machine. However

2条回答
  •  故里飘歌
    2021-01-29 05:42

    With the help of @JakePearse I was able to resolve the issue.

    Here is the final version of my (fully-functional) python script.

    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!')
        }
    

提交回复
热议问题