-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonoff_control.py
43 lines (35 loc) · 987 Bytes
/
sonoff_control.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
#!/usr/bin/python3
#
# Sonoff switch control
#
# MIT License
# Copyright (c) 2019 SrdjanStankovic
import logging
import requests
import xml.etree.ElementTree as ET
# Read switches from file
tree = ET.parse("Sonoff-Switch-Control/config.xml")
root = tree.getroot()
number_of_children = len(root.getchildren())
# Switch1
SWITCH1_REF = "SW1"
SWITCH1_ADD = root[0][1].text
# Switch2
SWITCH2_REF = "SW2"
SWITCH2_ADD = root[1][1].text
def sonoff_switch(ip_add, value):
base_url = "http://" + ip_add + "/control?cmd=event,Turn"
if value == 1 or value == "true":
base_url = base_url + "On"
else:
base_url = base_url + "Off"
logging.debug(base_url)
payload = {}
try:
response = requests.post(base_url, data=payload)
except:
logging.error("Couldn't change switch state.")
return False
logging.info(response.text) # TEXT/HTML
logging.info(str(response.status_code) + str(response.reason)) # HTTP
return True