Compare commits

..

No commits in common. "master" and "feature/ring_led" have entirely different histories.

2 changed files with 34 additions and 45 deletions

View file

@ -11,25 +11,22 @@ while True:
time.sleep(1) time.sleep(1)
try: try:
if socket == None: if socket == None:
print("Connecting... ", end="") print("Connecting...")
# 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("Success!") print("Connected.")
print("You're now connected to: {} (Icon: {})".format(socket.name, socket.icons[0]))
if socket.login("1234") and socket.authenticated: #socket.login("1337")
print("Login successful!") #socket.changePassword("1234")
socket.getSynConfig() #socket.login("1234")
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):

View file

@ -18,8 +18,9 @@ class SEMSocket():
authenticated = False authenticated = False
_icon_idx = None _icon_idx = None
_name = None _name = None
_version_char = None _read_char = None
_write_char = None _write_char = None
_notify_char = None
_name_char = None _name_char = None
_btle_device = None _btle_device = None
@ -46,6 +47,13 @@ class SEMSocket():
msg = self.BTLEMessage(self, cmd, payload) msg = self.BTLEMessage(self, cmd, payload)
msg.send() 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): 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
@ -136,28 +144,8 @@ class SEMSocket():
#for i in range(7): #for i in range(7):
# payload[i+1] = 0x00 # payload[i+1] = 0x00
#msg = self.BTLEMessage(self, cmd, payload) #msg = self.BTLEMessage(self, cmd, payload)
if not success: if not success: raise self.SendMessageFailed
raise self.SendMessageFailed if self.name != newName: raise self.NotLoggedIn
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):
@ -176,17 +164,23 @@ 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._version_char = self._custom_service.getCharacteristics("0000fff1-0000-1000-8000-00805f9b34fb")[0] #contains firmware version info 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] #is used to write commands 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._name_char = self._custom_service.getCharacteristics("0000fff6-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 ______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
@ -296,7 +290,7 @@ 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 == 0x10: #plug settings response elif message_type == 0x10:
self.__btle_device.default_charge = (data[5] / 100) self.__btle_device.default_charge = (data[5] / 100)
self.__btle_device.night_charge = (data[6] / 100) self.__btle_device.night_charge = (data[6] / 100)
night_charge_start_time = int((data[7] << 8 | data[8]) / 60) night_charge_start_time = int((data[7] << 8 | data[8]) / 60)
@ -306,8 +300,6 @@ class SEMSocket():
self.__btle_device.night_mode = not bool(data[11]) self.__btle_device.night_mode = not bool(data[11])
self.__btle_device.icon_idx = data[12] self.__btle_device.icon_idx = data[12]
self.__btle_device.power_protect = (data[13] << 8 | data[14]) 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 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)