Compare commits

...

3 commits

Author SHA1 Message Date
sqozz 4fbd13a7b3 Add reset functions 2020-05-09 16:39:43 +02:00
sqozz de7cbecb77 Add serial and fw version property 2020-05-09 16:28:14 +02:00
sqozz 38272656cf Cleanup characteristics 2020-05-09 16:27:05 +02:00

View file

@ -18,9 +18,8 @@ class SEMSocket():
authenticated = False
_icon_idx = None
_name = None
_read_char = None
_version_char = None
_write_char = None
_notify_char = None
_name_char = None
_btle_device = None
@ -47,13 +46,6 @@ class SEMSocket():
msg = self.BTLEMessage(self, cmd, payload)
msg.send()
def getSN(self):
# 15, 5, 17, 0, 0, 0, 18, -1, -1
cmd = bytearray([0x11])
payload = bytearray([0x00, 0x00, 0x00])
msg = self.BTLEMessage(self, cmd, payload)
msg.send()
def setStatus(self, status):
# 0f 06 03 00 01 00 00 05 ff ff -> on
# 0f 06 03 00 00 00 00 04 ff ff -> off
@ -105,6 +97,20 @@ class SEMSocket():
self.authenticated = self.authenticated and success
return success
def clearHistory(self):
cmd = bytearray([0x15])
payload = bytearray([0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00])
msg = self.BTLEMessage(self, cmd, payload)
success = msg.send()
return success
def factoryReset(self):
cmd = bytearray([0x15])
payload = bytearray([0x00, 0x00, 0x00, 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")
@ -128,8 +134,28 @@ class SEMSocket():
#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
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
def connected(self):
@ -148,23 +174,17 @@ class SEMSocket():
self.disconnect()
self._btle_device.connect(self.mac_address)
self._btle_handler = self.BTLEHandler(self)
self._btle_device.setDelegate(self._btle_handler)
self._custom_service = self._btle_device.getServiceByUUID(0xfff0)
self._read_char = self._custom_service.getCharacteristics("0000fff1-0000-1000-8000-00805f9b34fb")[0]
self._write_char = self._custom_service.getCharacteristics("0000fff3-0000-1000-8000-00805f9b34fb")[0]
self._notify_char = self._custom_service.getCharacteristics("0000fff4-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] #is used to write commands
self._name_char = self._custom_service.getCharacteristics("0000fff6-0000-1000-8000-00805f9b34fb")[0]
self._btle_device.setDelegate(self._btle_handler)
def disconnect(self):
if self.connected == True:
self._btle_device.disconnect()
#def SynVer(self):
# print("SynVer")
# self.read_char.read_value()
#def ______RESET(self):
# #15, 5, 16, 0, 0, 0, 17, -1, -1 ??? maybe reset?
# pass
@ -274,7 +294,7 @@ class SEMSocket():
self.__btle_device.power_factor = power / (voltage * current)
except ZeroDivisionError:
self.__btle_device.power_factor = None
elif message_type == 0x10:
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)
@ -284,6 +304,8 @@ class SEMSocket():
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:
# in theory the fifth byte indicates a login attempt response (0) or a response to a password change (1)