mirror of
https://github.com/RustyRaptor/sengled-python.git
synced 2026-12-03 15:35:17 -07:00
First commit
This commit is contained in:
commit
f303d63477
10 files changed
+204
No files matched your search
@@ -0,0 +1,9 @@
|
||||
# Python Sengled Library
|
||||
This is a Python library for interfacing with the Sengled Element devices
|
||||
|
||||
|
||||
## Devices Tested
|
||||
* Element A19 bulb
|
||||
|
||||
## Getting Started
|
||||
See examples for an example of how to use the library to control Sengled Element bulbs.
|
||||
@@ -0,0 +1,6 @@
|
||||
sengled_base_url = 'https://ec2-34-211-169-253.us-west-2.compute.amazonaws.com/'
|
||||
zigbee_url = 'zigbee/'
|
||||
customer_url = 'customer/'
|
||||
device_url = 'device/'
|
||||
room_url = 'room/'
|
||||
update_url = 'update/'
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,69 @@
|
||||
import requests
|
||||
from SengledElement.devices import devices
|
||||
from SengledElement.rooms import rooms
|
||||
from SengledElement import sengled_base_url, zigbee_url, customer_url, room_url
|
||||
|
||||
|
||||
class client:
|
||||
|
||||
headers = {'Content-type': 'application/json'}
|
||||
|
||||
username = None
|
||||
password = None
|
||||
jsessionid = None
|
||||
logged_in = False
|
||||
|
||||
rooms = None
|
||||
devices = None
|
||||
|
||||
def login(self):
|
||||
if self.logged_in:
|
||||
return
|
||||
login_data = {'os_type': 'android', 'pwd': self.password, 'user': self.username, 'uuid': 'xxxxxx'}
|
||||
resp = requests.post(sengled_base_url + zigbee_url + customer_url + 'login.json',
|
||||
headers=self.headers, verify=False, json=login_data)
|
||||
if resp.status_code == 200:
|
||||
resp_json = resp.json()
|
||||
if 'msg' in resp_json and resp_json['msg'] == 'success':
|
||||
self.headers['Cookie'] = 'JSESSIONID={}'.format(resp_json['jsessionid'])
|
||||
self.jsessionid = resp_json['jsessionid']
|
||||
self.logged_in = True
|
||||
else:
|
||||
key = None
|
||||
if 'msg' in resp_json:
|
||||
key = 'msg'
|
||||
elif 'info' in resp_json:
|
||||
key = 'info'
|
||||
if key is not None:
|
||||
print('Login unsuccessful: {}'.format(resp_json[key]))
|
||||
else:
|
||||
print('Could not login successfully')
|
||||
else:
|
||||
print('Could not login successfully')
|
||||
|
||||
def update(self):
|
||||
self.login()
|
||||
resp = requests.post(sengled_base_url + zigbee_url + room_url + 'getUserRoomsDetail.json',
|
||||
headers=self.headers, verify=False, json={})
|
||||
if resp.status_code == 200:
|
||||
resp_json = resp.json()
|
||||
if 'roomList' in resp_json:
|
||||
self.parse_room_list(resp_json['roomList'])
|
||||
else:
|
||||
print('Could not get rooms: {}'.format(resp.status_code))
|
||||
|
||||
def parse_room_list(self, room_list):
|
||||
self.rooms.clear_rooms()
|
||||
for room in room_list:
|
||||
if 'deviceList' in room:
|
||||
for device in room['deviceList']:
|
||||
self.devices.add_device(device, room['roomId'])
|
||||
del room['deviceList']
|
||||
self.rooms.add_room(room)
|
||||
|
||||
def __init__(self, username, password):
|
||||
self.username = username
|
||||
self.password = password
|
||||
self.devices = devices(self)
|
||||
self.rooms = rooms()
|
||||
self.update()
|
||||
@@ -0,0 +1,66 @@
|
||||
import requests
|
||||
from SengledElement import sengled_base_url, zigbee_url, device_url
|
||||
|
||||
|
||||
class devices:
|
||||
devices = None
|
||||
client = None
|
||||
|
||||
def add_device(self, device, room_id):
|
||||
device['roomId'] = room_id
|
||||
self.devices.append(device)
|
||||
|
||||
def clear_device_list(self):
|
||||
self.devices = []
|
||||
|
||||
def get_names(self):
|
||||
device_names = []
|
||||
for device in self.devices:
|
||||
device_names.append(device['deviceName'])
|
||||
return device_names
|
||||
|
||||
def get_device_id(self, device_name):
|
||||
for device in self.devices:
|
||||
if 'deviceName' in device and device['deviceName'] == device_name and 'deviceUuid' in device:
|
||||
return device['deviceUuid']
|
||||
return None
|
||||
|
||||
def get_device(self, device_id):
|
||||
for device in self.devices:
|
||||
if device['deviceUuid'] == device_id:
|
||||
return device
|
||||
|
||||
def update_value(self, device_id, name, value):
|
||||
for device in self.devices:
|
||||
if device['deviceUuid'] == device_id:
|
||||
device[name] = value
|
||||
|
||||
|
||||
def toggle_device(self, device_id=None, device_name=None):
|
||||
if device_id is None:
|
||||
device_id = self.get_device_id(device_name)
|
||||
device = self.get_device(device_id)
|
||||
toggle_value = 0
|
||||
if device['onoff'] == 0:
|
||||
toggle_value = 1
|
||||
self.set_device_state(toggle_value, device_id=device_id)
|
||||
|
||||
def set_device_state(self, state, device_id=None, device_name=None):
|
||||
if device_id is None:
|
||||
device_id = self.get_device_id(device_name)
|
||||
|
||||
self.client.login()
|
||||
toggle_json = {'deviceUuid': device_id,
|
||||
'onoff': state}
|
||||
resp = requests.post(sengled_base_url + zigbee_url + device_url + '/deviceSetOnOff.json',
|
||||
headers=self.client.headers, json=toggle_json, verify=False)
|
||||
if resp.status_code == 200:
|
||||
self.update_value(device_id, 'onoff', state)
|
||||
return True
|
||||
else:
|
||||
print('Could not toggle device')
|
||||
return False
|
||||
|
||||
def __init__(self, client):
|
||||
self.devices = []
|
||||
self.client = client
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
|
||||
class rooms:
|
||||
rooms = None
|
||||
|
||||
def clear_rooms(self):
|
||||
self.rooms = []
|
||||
|
||||
def add_room(self, room):
|
||||
self.rooms.append(room)
|
||||
|
||||
def __init__(self):
|
||||
self.rooms = []
|
||||
@@ -0,0 +1,41 @@
|
||||
from SengledElement import client
|
||||
import sys
|
||||
|
||||
home = None
|
||||
|
||||
|
||||
def list_devices():
|
||||
for name in home.devices.get_names():
|
||||
print(name)
|
||||
|
||||
|
||||
def set_state(name=None, state=None):
|
||||
if name is None:
|
||||
name = input("Which bulb?")
|
||||
if state is None:
|
||||
state = input("What state?")
|
||||
home.devices.set_device_state(state, device_name=name)
|
||||
|
||||
|
||||
def main_input():
|
||||
max_option = 3
|
||||
while True:
|
||||
print("1)\tList Bublbs")
|
||||
print("2)\tSet State")
|
||||
print("3)\tExit)")
|
||||
select = input("Select option: ")
|
||||
select = int(select)
|
||||
if 0 < select <= max_option:
|
||||
if select == 1:
|
||||
list_devices()
|
||||
elif select == 2:
|
||||
set_state()
|
||||
elif select == 3:
|
||||
return
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
home = client.client(sys.argv[1], sys.argv[2])
|
||||
|
||||
main_input()
|
||||
|
||||
Reference in new issue
Block a user