-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
executable file
·46 lines (36 loc) · 1.3 KB
/
start.py
File metadata and controls
executable file
·46 lines (36 loc) · 1.3 KB
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
46
import requests
import yaml
#YAMLファイルから対象のdeviceのListを取得
def Deviceslist(file):
with open(file,'r') as yml:
deviceslists = yaml.safe_load(yml)
return deviceslists["devices"]
def APIToken(file):
with open(file, 'r') as yml:
apitoken = yaml.safe_load(yml)
# 辞書ではなく文字列だけ返す
return apitoken["token"]
#取得したdeviceslist,APITokenから全ての対象の電源を順番にあげる
def startcommand(Device, Token):
url_base = "https://api.switch-bot.com/v1.0/devices/{}/commands"
headers = {
"Authorization": Token,
"Content-Type": "application/json"
}
payload = {
"command": "turnOn", # ← turnOff → turnOn に変更!
"parameter": "default",
"commandType": "command"
}
for value in Device.values():
url = url_base.format(value)
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
print(f"{value} → 電源ON 成功: {response.json()}")
else:
print(f"{value} → 電源ON 失敗: {response.status_code}, {response.text}")
Device = Deviceslist("deviceslist.yaml")
print(Device)
Token = APIToken("deviceslist.yaml")
print(Token)
startcommand(Device, Token)