Skip to content

Commit 26e6c8b

Browse files
committed
Revert "bump to 0.3.0"
This reverts commit e0ad649.
1 parent a717c70 commit 26e6c8b

File tree

1 file changed

+35
-112
lines changed

1 file changed

+35
-112
lines changed

pytes_serial.py

Lines changed: 35 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525

2626
MQTT_active = config.get('MQTT', 'MQTT_active')
2727
MQTT_broker = config.get('MQTT', 'MQTT_broker')
28-
MQTT_port = int(config.get('MQTT', 'MQTT_port'))
29-
MQTT_username = config.get('MQTT', 'MQTT_username')
30-
MQTT_password = config.get('MQTT', 'MQTT_password')
3128

3229
start_time = time.time()
3330
up_time = time.time()
@@ -36,7 +33,7 @@
3633
errors_no = 0 # used to count no of errors and to calculate %
3734
errors = 'false'
3835

39-
print('PytesSerial build: v0.3.0_20230123')
36+
print('PytesSerial build: v0.2.3_20230114')
4037

4138
# ------------------------functions area----------------------------
4239
def log (str) :
@@ -48,32 +45,23 @@ def log (str) :
4845
return
4946
except Exception as e:
5047
print("Errorhandling: double error in EventLog", e)
51-
48+
5249
def parsing_serial():
53-
global errors
54-
volt_st = None # initiate non critical variable to ensure various firmaware combatibility
55-
current_st = None
56-
temp_st = None
57-
coul_st = None
58-
soh_st = None
59-
heater_st = None
60-
bat_events = None
61-
power_events = None
62-
sys_events = None
63-
64-
try:
65-
if ser.is_open != True:
66-
ser.open()
67-
print ('...serial opened')
68-
69-
for power in range (1, powers+1): # do the loop for each battery
70-
line_str = "" # clear line_str
71-
line = "" # clear line
72-
power_bytes = bytes(str(power), 'ascii') # convert to bytes
73-
ser.write(b'pwr '+ power_bytes + b'\n') # write on serial port 'pwr x' command
50+
global errors
51+
if ser.is_open != True:
52+
ser.open()
53+
time.sleep(0.2)
54+
print ('...serial opened')
55+
for power in range (1, powers+1): # do the loop for each battery
56+
try:
57+
# parsing pwr x commmand - reading power bank x
58+
line_str = "" # clear line_str
59+
line = "" # clear line
60+
power_bytes = bytes(str(power), 'ascii') # convert to bytes
61+
ser.write(b'pwr '+ power_bytes + b'\n') # write on serial port 'pwr x' command
7462
ser.flush()
7563

76-
time.sleep(0.5) # calm down a bit ...
64+
time.sleep(0.2) # calm down a bit ...
7765
buffer = ser.in_waiting
7866
print ('...writing complete ', 'in buffer:', buffer)
7967

@@ -85,12 +73,12 @@ def parsing_serial():
8573
print('...suspicious data set, trying again')
8674
errors = 'true'
8775

88-
log('*'+str(errors_no)+'*'+str(buffer)+'**>'+str(line)) # [DPO] for debug purpose
76+
log('*'+str(errors_no)+'*'+str(buffer)+'**>'+str(line)) # [DPO] for debug purpose remark the line
8977

9078
if ser.is_open == True:
9179
ser.close()
9280
print ('...serial closed')
93-
return # do not move forward if no end of the parsing group detected
81+
return # do not move forward if no end of the parsing group detected
9482

9583
if line_str[1:18] == 'Voltage :': voltage = int(line_str[19:27])/1000
9684
if line_str[1:18] == 'Current :': current = int(line_str[19:27])/1000
@@ -105,7 +93,7 @@ def parsing_serial():
10593
if line_str[1:18] == 'Heater Status :': heater_st = line_str[19:27]
10694
if line_str[1:18] == 'Bat Events :': bat_events = int(line_str[19:27],16)
10795
if line_str[1:18] == 'Power Events :': power_events = int(line_str[19:27],16)
108-
if line_str[1:18] == 'System Fault :': sys_events = int(line_str[19:27],16)
96+
if line_str[1:18] == 'System Fault :': sys_events = int(line_str[19:27],16)
10997
if line_str[1:18] == 'Command completed':
11098
break
11199

@@ -144,62 +132,28 @@ def parsing_serial():
144132
'sys_events': sys_events}
145133

146134
pwr.append(pwr_array)
135+
136+
except Exception as e:
137+
print("...serial parsing error: " + str(e))
138+
errors = 'true'
139+
140+
log('*'+str(errors_no)+'*'+str(buffer)+'**>'+str(line)) # [DPO] for debug purpose remark the line
147141

148-
statistics()
149-
150-
print ('...serial parsing: ok')
151-
152-
except Exception as e:
153-
print("...serial parsing error: " + str(e))
154-
errors = 'true'
155-
if ser.is_open == True:
156-
ser.close()
157-
print ('...serial closed')
158-
return
159-
160-
log('*'+str(errors_no)+'*'+str(buffer)+'**>'+str(line)) # [DPO] for debug purpose
142+
if ser.is_open == True:
143+
ser.close()
144+
print ('...serial closed')
145+
return
161146

162-
def statistics():
163-
global sys_voltage
164-
global sys_current
165-
global sys_soc
166-
global sys_temp
167-
global sys_basic_st
168-
sys_voltage = 0
169-
sys_current = 0
170-
sys_soc = 0
171-
sys_temp = 0
172-
sys_basic_st = ""
173-
174-
for power in range (1, powers+1):
175-
sys_voltage = sys_voltage + pwr[power-1]['voltage'] # vontage will be the average of all batteries
176-
sys_current = round((sys_current + pwr[power-1]['current']),3) # current will be sum of all banks
177-
sys_soc = sys_soc + pwr[power-1]['soc'] # soc will be the average of all batteries
178-
sys_temp = sys_temp + pwr[power-1]['temperature'] # temperature will be the average of all batteries
179-
180-
sys_voltage = round((sys_voltage / powers), 3)
181-
sys_soc = int(sys_soc / powers)
182-
sys_basic_st = pwr[0]['basic_st'] # status will be the master status
183-
sys_temp = round((sys_temp / powers), 1)
147+
print ('...serial parsing: ok')
184148

185149
def json_serialize():
186150
global errors
187151
global json_data
188152
try:
189-
json_data={'relay_local_time':TimeStamp,
190-
'serial_uptime':uptime,
191-
'powers' : powers,
192-
'voltage': sys_voltage,
193-
'current': sys_current,
194-
'temperature': sys_temp,
195-
'soc': sys_soc,
196-
'basic_st': sys_basic_st,
197-
'pytes':pwr}
198-
153+
json_data={'relay_local_time':TimeStamp, 'serial_uptime':uptime, 'pytes':pwr}
199154
with open(output_path + 'pytes_status.json', 'w') as outfile:
200155
json.dump(json_data, outfile)
201-
print('...json creation: ok')
202-
156+
print('...json creation: ok')
203157
except Exception as e:
204158
print('...json serailization error: ' + str(e))
205159
errors = 'true'
@@ -254,39 +208,13 @@ def maria_db():
254208

255209
def mqtt_discovery():
256210
try:
257-
MQTT_auth = None
258-
if len(MQTT_username) >0:
259-
MQTT_auth = { 'username': MQTT_username, 'password': MQTT_password }
260211
msg ={}
261212
config = 1
262213
names =["pytes_current", "pytes_voltage" , "pytes_temperature", "pytes_soc", "pytes_status"]
263214
ids =["current", "voltage" , "temperature", "soc", "basic_st"] #do not change the prefix "pytes_"
264215
dev_cla =["current", "voltage", "temperature", "battery","None"]
265216
unit_of_meas =["A","v","°C", "%",""]
266217

267-
# define system sensors
268-
for n in range(5):
269-
state_topic ="homeassistant/sensor/pytes/"+str(config)+"/config"
270-
msg ["name"] = names[n]
271-
msg ["stat_t"] = "homeassistant/sensor/pytes/state"
272-
msg ["uniq_id"] = "pytes_"+ids[n]
273-
if dev_cla[n] != "None":
274-
msg ["dev_cla"] = dev_cla[n]
275-
msg ["unit_of_meas"] = unit_of_meas[n]
276-
msg ["val_tpl"] = "{{ value_json." + ids[n]+ "}}"
277-
msg ["dev"] = {"identifiers": ["pytes"],"manufacturer": "PYTES","model": "E-Box48100R","name": "pytes_ebox","sw_version": "1.0"}
278-
279-
message = json.dumps(msg)
280-
publish.single(state_topic, message, hostname=MQTT_broker, port= MQTT_port, auth=MQTT_auth, qos=0, retain=True)
281-
282-
b = "...mqtt auto discovery initialization :" + str(round(config/(5*powers+5)*100)) +" %"
283-
print (b, end="\r")
284-
285-
msg ={}
286-
config = config +1
287-
time.sleep(2)
288-
289-
# define individual batteries sensors
290218
for power in range (1, powers+1):
291219
for n in range(5):
292220
state_topic ="homeassistant/sensor/pytes/"+str(config)+"/config"
@@ -300,28 +228,23 @@ def mqtt_discovery():
300228
msg ["dev"] = {"identifiers": ["pytes"],"manufacturer": "PYTES","model": "E-Box48100R","name": "pytes_ebox","sw_version": "1.0"}
301229

302230
message = json.dumps(msg)
303-
publish.single(state_topic, message, hostname=MQTT_broker, port= MQTT_port, auth=MQTT_auth, qos=0, retain=True)
231+
publish.single(state_topic, message, hostname=MQTT_broker, qos=0, retain=True)
304232

305-
b = "...mqtt auto discovery initialization :" + str(round(config/(5*powers+5)*100)) +" %"
306-
print (b, end="\r")
307-
308233
msg ={}
309234
config = config +1
235+
b = "...mqtt auto discovery initialization" + "." * config
236+
print (b, end="\r")
310237
time.sleep(2)
311-
312238
print("...mqtt auto discovery initialization completed")
313239

314240
except Exception as e:
315241
print('...mqtt_discovery failed' + str(e))
316242

317243
def mqtt_publish():
318244
try:
319-
MQTT_auth = None
320-
if len(MQTT_username) >0:
321-
MQTT_auth = { 'username': MQTT_username, 'password': MQTT_password }
322245
state_topic = "homeassistant/sensor/pytes/state"
323246
message = json.dumps(json_data)
324-
publish.single(state_topic, message, hostname=MQTT_broker, port=MQTT_port, auth=MQTT_auth)
247+
publish.single(state_topic, message, hostname=MQTT_broker)
325248
print ('...mqtt publish : ok')
326249

327250
except Exception as e:

0 commit comments

Comments
 (0)