-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
45 lines (37 loc) · 1.44 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import json
from app.hue_cloud import set_light_state
from app.secrets_manager import get_secret
def lambda_handler(event, context):
"""
AWS Lambda handler to control Philips Hue lights via the Cloud API.
The event contains the action (on/off) and the light IDs to control.
"""
# Retrieve the access_token from AWS Secrets Manager
secret = get_secret("hue-api/oauth")
access_token = secret['access_token'] # Assuming you've stored it as access_token
# Extract action (on/off) from the event payload, default is "on"
action = event.get("action", "on")
# Extract the list of light IDs from the event payload
lights = event.get("lights", []) # e.g., [1, 2]
if not lights:
return {
'statusCode': 400,
'body': json.dumps("No lights specified in the request.")
}
# Set the state of all lights in the list
success = True
for light_id in lights:
result = set_light_state(access_token, light_id, action)
if not result:
success = False
# Return the response based on whether all lights were successfully controlled
if success:
return {
'statusCode': 200,
'body': json.dumps(f"All specified lights turned {action} successfully!")
}
else:
return {
'statusCode': 500,
'body': json.dumps(f"Failed to turn {action} all specified lights.")
}