Compare commits
16 Commits
feature/re
...
master
Author | SHA1 | Date |
---|---|---|
sqozz | 8c372513d8 | |
sqozz | de79c2bca5 | |
sqozz | 06f2cfe33e | |
sqozz | df9a355ee8 | |
sqozz | 95983d464f | |
sqozz | 3755f26081 | |
sqozz | f488869f3d | |
sqozz | 6cc5804017 | |
sqozz | 9b49abd66e | |
sqozz | 80c0c5dd4b | |
sqozz | aea3cc32f5 | |
sqozz | be6528f074 | |
sqozz | b80c640e61 | |
sqozz | 271e1bc5bf | |
sqozz | b931fb0a41 | |
sqozz | 7d16b7b33b |
29
example.py
29
example.py
|
@ -11,22 +11,25 @@ while True:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
try:
|
try:
|
||||||
if socket == None:
|
if socket == None:
|
||||||
print("Connecting...")
|
print("Connecting... ", end="")
|
||||||
|
|
||||||
# auto_reconnect_timeout enabled auto reconnect if sending a command fails. Valid values:
|
|
||||||
# None (default): everything that fails throws NotConnectedException's
|
|
||||||
# -1: infinite retries
|
|
||||||
# integer: seconds before exception is thrown
|
|
||||||
|
|
||||||
socket = SEMSocket('f0:c7:7f:0d:e7:17')
|
socket = SEMSocket('f0:c7:7f:0d:e7:17')
|
||||||
print("Connected.")
|
print("Success!")
|
||||||
|
print("You're now connected to: {} (Icon: {})".format(socket.name, socket.icons[0]))
|
||||||
#socket.login("1337")
|
if socket.login("1234") and socket.authenticated:
|
||||||
#socket.changePassword("1234")
|
print("Login successful!")
|
||||||
#socket.login("1234")
|
socket.getSynConfig()
|
||||||
|
print()
|
||||||
|
print("=== Tariff settings ===")
|
||||||
|
print("Default charge:", socket.default_charge)
|
||||||
|
print("Night charge:", socket.night_charge)
|
||||||
|
print("Night tariff from {} to {}".format(socket.night_charge_start_time.tm_hour, socket.night_charge_end_time.tm_hour))
|
||||||
|
print()
|
||||||
|
print("=== Other settings ===")
|
||||||
|
print("Night mode:", "active" if socket.night_mode else "inactive")
|
||||||
|
print("Power protection:", socket.power_protect)
|
||||||
|
print()
|
||||||
|
|
||||||
socket.getStatus()
|
socket.getStatus()
|
||||||
socket.setStatus(True)
|
|
||||||
print("=== {} ({}) ===".format(socket.mac_address, "on" if socket.powered else "off"))
|
print("=== {} ({}) ===".format(socket.mac_address, "on" if socket.powered else "off"))
|
||||||
print("\t{}V {}A → {}W@{}Hz (PF: {})".format(socket.voltage, socket.current, socket.power, socket.frequency, socket.power_factor))
|
print("\t{}V {}A → {}W@{}Hz (PF: {})".format(socket.voltage, socket.current, socket.power, socket.frequency, socket.power_factor))
|
||||||
except (SEMSocket.NotConnectedException, bluepy.btle.BTLEDisconnectError, BrokenPipeError):
|
except (SEMSocket.NotConnectedException, bluepy.btle.BTLEDisconnectError, BrokenPipeError):
|
||||||
|
|
153
sem6000.py
153
sem6000.py
|
@ -4,6 +4,7 @@ import datetime
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
class SEMSocket():
|
class SEMSocket():
|
||||||
|
icons = ["plug", "speaker", "flatscreen", "desk lamp", "oven", "kitchen machine", "canning pot", "stanging lamp", "kettle", "mixer", "hanging lamp", "toaster", "washing machine", "fan", "fridge", "iron", "printer", "monitor", "notebook", "workstation", "video recorder", "curling iron", "heater"]
|
||||||
password = "0000"
|
password = "0000"
|
||||||
powered = False
|
powered = False
|
||||||
voltage = 0
|
voltage = 0
|
||||||
|
@ -15,9 +16,11 @@ class SEMSocket():
|
||||||
mac_address = ""
|
mac_address = ""
|
||||||
custom_service = None
|
custom_service = None
|
||||||
authenticated = False
|
authenticated = False
|
||||||
_read_char = None
|
_icon_idx = None
|
||||||
|
_name = None
|
||||||
|
_version_char = None
|
||||||
_write_char = None
|
_write_char = None
|
||||||
_notify_char = None
|
_name_char = None
|
||||||
_btle_device = None
|
_btle_device = None
|
||||||
|
|
||||||
def __init__(self, mac):
|
def __init__(self, mac):
|
||||||
|
@ -36,6 +39,13 @@ class SEMSocket():
|
||||||
msg = self.BTLEMessage(self, cmd, payload)
|
msg = self.BTLEMessage(self, cmd, payload)
|
||||||
return msg.send()
|
return msg.send()
|
||||||
|
|
||||||
|
def getSynConfig(self):
|
||||||
|
#15, 5, 16, 0, 0, 0, 17, -1, -1
|
||||||
|
cmd = bytearray([0x10])
|
||||||
|
payload = bytearray([0x00, 0x00, 0x00])
|
||||||
|
msg = self.BTLEMessage(self, cmd, payload)
|
||||||
|
msg.send()
|
||||||
|
|
||||||
def setStatus(self, status):
|
def setStatus(self, status):
|
||||||
# 0f 06 03 00 01 00 00 05 ff ff -> on
|
# 0f 06 03 00 01 00 00 05 ff ff -> on
|
||||||
# 0f 06 03 00 00 00 00 04 ff ff -> off
|
# 0f 06 03 00 00 00 00 04 ff ff -> off
|
||||||
|
@ -87,6 +97,68 @@ class SEMSocket():
|
||||||
self.authenticated = self.authenticated and success
|
self.authenticated = self.authenticated and success
|
||||||
return success
|
return success
|
||||||
|
|
||||||
|
def setIcon(self, iconIdx):
|
||||||
|
cmd = bytearray([0x0f])
|
||||||
|
payload = bytearray([0x00, 0x03, iconIdx, 0x00, 0x00, 0x00, 0x00])
|
||||||
|
msg = self.BTLEMessage(self, cmd, payload)
|
||||||
|
|
||||||
|
success = msg.send()
|
||||||
|
return success
|
||||||
|
|
||||||
|
def enableLED(self, status):
|
||||||
|
cmd = bytearray([0x0f])
|
||||||
|
payload = bytearray([0x00, 0x05, status, 0x00, 0x00, 0x00, 0x00])
|
||||||
|
msg = self.BTLEMessage(self, cmd, payload)
|
||||||
|
|
||||||
|
success = msg.send()
|
||||||
|
return success
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
self._name = self._name_char.read().decode("UTF-8")
|
||||||
|
return self._name
|
||||||
|
|
||||||
|
@name.setter
|
||||||
|
def name(self, newName):
|
||||||
|
newNameBytes = newName.encode("UTF-8")
|
||||||
|
cmd = bytearray([0x02])
|
||||||
|
payload = bytearray()
|
||||||
|
payload.append(0x02)
|
||||||
|
for i in range(20):
|
||||||
|
if i <= (len(newNameBytes) - 1):
|
||||||
|
payload.append(newNameBytes[i])
|
||||||
|
else:
|
||||||
|
payload.append(0x00)
|
||||||
|
msg = self.BTLEMessage(self, cmd, payload)
|
||||||
|
success = msg.send()
|
||||||
|
# For some reason the original app sets the first 7 bytes of the payload to zero and sends it again.
|
||||||
|
# However, a first test showed, that this really doesn't change anything. If it becomes neccessary here's a first draft:
|
||||||
|
#for i in range(7):
|
||||||
|
# payload[i+1] = 0x00
|
||||||
|
#msg = self.BTLEMessage(self, cmd, payload)
|
||||||
|
if not success:
|
||||||
|
raise self.SendMessageFailed
|
||||||
|
if self.name != newName:
|
||||||
|
raise self.NotLoggedIn
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serial(self):
|
||||||
|
# 15, 5, 17, 0, 0, 0, 18, -1, -1
|
||||||
|
cmd = bytearray([0x11])
|
||||||
|
payload = bytearray([0x00, 0x00, 0x00])
|
||||||
|
msg = self.BTLEMessage(self, cmd, payload)
|
||||||
|
success = msg.send()
|
||||||
|
if not success:
|
||||||
|
raise self.SendMessageFailed
|
||||||
|
return self._serial
|
||||||
|
|
||||||
|
@property
|
||||||
|
def firmware_version(self):
|
||||||
|
char_value = self._version_char.read()
|
||||||
|
major = char_value[11]
|
||||||
|
minor = char_value[12]
|
||||||
|
return "{}.{}".format(major, minor)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def connected(self):
|
def connected(self):
|
||||||
try:
|
try:
|
||||||
|
@ -104,44 +176,30 @@ class SEMSocket():
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self._btle_device.connect(self.mac_address)
|
self._btle_device.connect(self.mac_address)
|
||||||
self._btle_handler = self.BTLEHandler(self)
|
self._btle_handler = self.BTLEHandler(self)
|
||||||
|
self._btle_device.setDelegate(self._btle_handler)
|
||||||
|
|
||||||
self._custom_service = self._btle_device.getServiceByUUID(0xfff0)
|
self._custom_service = self._btle_device.getServiceByUUID(0xfff0)
|
||||||
self._read_char = self._custom_service.getCharacteristics("0000fff1-0000-1000-8000-00805f9b34fb")[0]
|
self._version_char = self._custom_service.getCharacteristics("0000fff1-0000-1000-8000-00805f9b34fb")[0] #contains firmware version info
|
||||||
self._write_char = self._custom_service.getCharacteristics("0000fff3-0000-1000-8000-00805f9b34fb")[0]
|
self._write_char = self._custom_service.getCharacteristics("0000fff3-0000-1000-8000-00805f9b34fb")[0] #is used to write commands
|
||||||
self._notify_char = self._custom_service.getCharacteristics("0000fff4-0000-1000-8000-00805f9b34fb")[0]
|
self._name_char = self._custom_service.getCharacteristics("0000fff6-0000-1000-8000-00805f9b34fb")[0]
|
||||||
self._btle_device.setDelegate(self._btle_handler)
|
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
if self.connected == True:
|
if self.connected == True:
|
||||||
self._btle_device.disconnect()
|
self._btle_device.disconnect()
|
||||||
|
|
||||||
#def SynVer(self):
|
|
||||||
# print("SynVer")
|
|
||||||
# self.read_char.read_value()
|
|
||||||
|
|
||||||
#def GetSynConfig(self):
|
|
||||||
# print("GetSynConfig")
|
|
||||||
# #15, 5, 16, 0, 0, 0, 17, -1, -1
|
|
||||||
# self.write_char.write_value(bytearray(b'\x0f\x05\x10\x00\x00\x00\x11\xff\xff'))
|
|
||||||
|
|
||||||
#def ______RESET(self):
|
#def ______RESET(self):
|
||||||
# #15, 5, 16, 0, 0, 0, 17, -1, -1 ??? maybe reset?
|
# #15, 5, 16, 0, 0, 0, 17, -1, -1 ??? maybe reset?
|
||||||
# pass
|
# pass
|
||||||
|
|
||||||
#def GetSN(self):
|
|
||||||
# print("GetSN")
|
|
||||||
# #15, 5, 17, 0, 0, 0, 18, -1, -1
|
|
||||||
# self.write_char.write_value(bytearray(b'\x0f\x05\x11\x00\x00\x00\x12\xff\xff'))
|
|
||||||
|
|
||||||
# self.SynVer()
|
|
||||||
# self.notify_char.enable_notifications()
|
|
||||||
# self.Login("1337")
|
|
||||||
# self.GetSynConfig()
|
|
||||||
# #self.GetSN()
|
|
||||||
|
|
||||||
class NotConnectedException(Exception):
|
class NotConnectedException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class SendMessageFailed(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class NotLoggedIn(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
class BTLEMessage():
|
class BTLEMessage():
|
||||||
MAGIC_START = bytearray([0x0f])
|
MAGIC_START = bytearray([0x0f])
|
||||||
MAGIC_END = bytearray([0xff, 0xff])
|
MAGIC_END = bytearray([0xff, 0xff])
|
||||||
|
@ -199,17 +257,28 @@ class SEMSocket():
|
||||||
self.__btle_device = btle_device
|
self.__btle_device = btle_device
|
||||||
|
|
||||||
def handleNotification(self, cHandle, data):
|
def handleNotification(self, cHandle, data):
|
||||||
|
if len(data) <= 3:
|
||||||
|
print("Notification data seems invalid or incomplete. Could not parse: ", end="")
|
||||||
|
print(data)
|
||||||
|
return
|
||||||
|
|
||||||
message_type = data[2]
|
message_type = data[2]
|
||||||
if message_type == 0x00:
|
if message_type == 0x00:
|
||||||
if data[4] == 0x01:
|
if data[4] == 0x01:
|
||||||
print("Checksum error!")
|
print("Checksum error!")
|
||||||
else:
|
else:
|
||||||
print("Unknown error:", data)
|
print("Unknown error:", data)
|
||||||
elif message_type == 0x01:
|
elif message_type == 0x01: #sync time response
|
||||||
print("Time synced")
|
if not data[3:] == b'\x00\x00\x02\xff\xff':
|
||||||
elif message_type == 0x03: #switch toggle
|
print("Time synced failed with unknown data: ", end="")
|
||||||
|
print(data)
|
||||||
|
elif message_type == 0x02: #set name response
|
||||||
|
if not data[3:] == b'\x00\x00\x03\xff\xff':
|
||||||
|
print("Set name failed with unknown data: ", end="")
|
||||||
|
print(data)
|
||||||
|
elif message_type == 0x03: #switch toggle response
|
||||||
self.__btle_device.getStatus()
|
self.__btle_device.getStatus()
|
||||||
elif message_type == 0x04: #status related data
|
elif message_type == 0x04: #status related response
|
||||||
voltage = data[8]
|
voltage = data[8]
|
||||||
current = (data[9] << 8 | data[10]) / 1000
|
current = (data[9] << 8 | data[10]) / 1000
|
||||||
power = (data[5] << 16 | data[6] << 8 | data[7]) / 1000
|
power = (data[5] << 16 | data[6] << 8 | data[7]) / 1000
|
||||||
|
@ -227,7 +296,19 @@ class SEMSocket():
|
||||||
self.__btle_device.power_factor = power / (voltage * current)
|
self.__btle_device.power_factor = power / (voltage * current)
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
self.__btle_device.power_factor = None
|
self.__btle_device.power_factor = None
|
||||||
elif message_type == 0x17:
|
elif message_type == 0x10: #plug settings response
|
||||||
|
self.__btle_device.default_charge = (data[5] / 100)
|
||||||
|
self.__btle_device.night_charge = (data[6] / 100)
|
||||||
|
night_charge_start_time = int((data[7] << 8 | data[8]) / 60)
|
||||||
|
night_charge_end_time = int((data[9] << 8 | data[10]) / 60)
|
||||||
|
self.__btle_device.night_charge_start_time = time.strptime(str(night_charge_start_time), "%H")
|
||||||
|
self.__btle_device.night_charge_end_time = time.strptime(str(night_charge_end_time), "%H")
|
||||||
|
self.__btle_device.night_mode = not bool(data[11])
|
||||||
|
self.__btle_device.icon_idx = data[12]
|
||||||
|
self.__btle_device.power_protect = (data[13] << 8 | data[14])
|
||||||
|
elif message_type == 0x11: #serial number response
|
||||||
|
self.__btle_device._serial = data[-16:].decode("UTF-8")
|
||||||
|
elif message_type == 0x17: #authentication related response
|
||||||
if data[5] == 0x00 or data[5] == 0x01:
|
if data[5] == 0x00 or data[5] == 0x01:
|
||||||
# in theory the fifth byte indicates a login attempt response (0) or a response to a password change (1)
|
# in theory the fifth byte indicates a login attempt response (0) or a response to a password change (1)
|
||||||
# but since a password change requires a valid login and a successful password changes logs you in,
|
# but since a password change requires a valid login and a successful password changes logs you in,
|
||||||
|
@ -235,6 +316,16 @@ class SEMSocket():
|
||||||
self.__btle_device.authenticated = not data[4]
|
self.__btle_device.authenticated = not data[4]
|
||||||
else:
|
else:
|
||||||
print("5th byte of login-response is > 1:", data)
|
print("5th byte of login-response is > 1:", data)
|
||||||
|
elif message_type == 0x0f: #set icon response
|
||||||
|
if data[3:6] == b'\x00\x03\x00':
|
||||||
|
# LED set successfully
|
||||||
|
pass
|
||||||
|
elif data[3:6] == b'\x00\x05\x00':
|
||||||
|
# Icon set successfully
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
print("Unknown response for setting icon: ", end="")
|
||||||
|
print(data[3:])
|
||||||
else:
|
else:
|
||||||
print ("Unknown message from Handle: 0x" + format(cHandle,'02X') + " Value: "+ format(data))
|
print ("Unknown message from Handle: 0x" + format(cHandle,'02X') + " Value: "+ format(data))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue