-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
53 lines (40 loc) · 1.6 KB
/
api.py
File metadata and controls
53 lines (40 loc) · 1.6 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
47
48
49
50
51
52
53
from jproperties import Properties
from requests import get, post
# home assistant long-lived authorization token
configs = Properties()
with open('api.properties', 'rb') as config_file:
configs.load(config_file)
items_view = configs.items()
list_keys = []
for item in items_view:
list_keys.append(item[0])
db_configs_dict = {}
for item in items_view:
db_configs_dict[item[0]] = item[1].data
# {'DB_HOST': 'localhost', 'DB_SCHEMA': 'Test', 'DB_User': 'root', 'DB_PWD': 'root@neon'}
# ['DB_HOST', 'DB_SCHEMA', 'DB_User', 'DB_PWD']
# homeassistant url endpoints
url_plug_state = "http://homeassistant.local:8123/api/states/switch.powermanager_kasa"
url_plug_turn_on = "http://homeassistant.local:8123/api/services/switch/turn_on"
url_plug_turn_off = "http://homeassistant.local:8123/api/services/switch/turn_off"
url_plug_toggle = "http://homeassistant.local:8123/api/services/switch/develco/toggle"
url_iphone_battery_state = "http://homeassistant.local:8123/api/states/sensor.iphone_battery_state"
url_iphone_battery_level = "http://homeassistant.local:8123/api/states/sensor.iphone_battery_level"
# bearer = auth.bearer()
bearer = db_configs_dict['bearer']
def call_plug(url):
mydata = '{"entity_id": "switch.powermanager_kasa"}'
headers = {
"Authorization": bearer,
"content-type": "application/json",
"payload": mydata,
}
response = post(url, headers=headers, data=mydata)
return response
def get_state(url):
headers = {
"Authorization": bearer,
"content-type": "application/json",
}
response = get(url, headers=headers)
return response