Skip to content

Commit 4bffc9d

Browse files
committed
bump to 0.6.0_20231007
close #20 HomeAssitant device name can be customised now in config file (i.e pytes, pytesone, pytestwo, pylon ...), JSON filename in line with device name, MQTT state topic in line wih device name close #19 wrong 100% SOC display in log files close #21 sensor nickname display for HomeAssistant renamed one json field name for better alignment with pylon ('pytes' to 'devices')
1 parent 6973554 commit 4bffc9d

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

pytes_serial.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
serial_port = config.get('serial', 'serial_port')
1616
serial_baudrate = int(config.get('serial', 'serial_baudrate'))
1717
reading_freq = int(config.get('serial', 'reading_freq'))
18-
powers = int(config.get('general', 'powers'))
19-
output_path = config.get('general', 'output_path')
18+
output_path = config.get('general', 'output_path')
19+
powers = int(config.get('battery_info', 'powers'))
20+
dev_name = config.get('battery_info', 'dev_name')
21+
manufacturer = config.get('battery_info', 'manufacturer')
22+
model = config.get('battery_info', 'model')
23+
sw_ver = "PytesSerial v0.6.0_20231007"
24+
version = sw_ver
25+
2026
if reading_freq < 10 : reading_freq = 10
2127

2228
SQL_active = config.get('Maria DB connection', 'SQL_active')
@@ -111,8 +117,7 @@
111117
2048:["warning","0x800","Discharge MOS FAIL"]
112118
}
113119

114-
version = 'PytesSerial build: v0.5.0_20230805'
115-
print(version)
120+
print("software version:",version)
116121

117122
# ------------------------logging definiton ----------------------------
118123
formatter = logging.Formatter('%(asctime)s| %(levelname)7s| %(message)s ',datefmt='%Y%m%d %H:%M:%S') # logging formating
@@ -424,7 +429,7 @@ def json_serialize():
424429
'temperature': sys_temp,
425430
'soc': sys_soc,
426431
'basic_st': sys_basic_st,
427-
'pytes':pwr,
432+
'devices':pwr,
428433
'serial_stat': {'uptime':uptime,
429434
'loops':loops_no,
430435
'errors': errors_no,
@@ -435,7 +440,7 @@ def json_serialize():
435440
'ser_round_trip':round(parsing_time,2)}
436441
}
437442

438-
with open(output_path + 'pytes_status.json', 'w') as outfile:
443+
with open(output_path + dev_name + '_status.json', 'w') as outfile:
439444
json.dump(json_data, outfile)
440445
print('...json creation: ok')
441446

@@ -497,32 +502,34 @@ def maria_db():
497502
def mqtt_discovery():
498503
try:
499504
MQTT_auth = None
500-
if len(MQTT_username) >0:
505+
if len(MQTT_username) > 0:
501506
MQTT_auth = { 'username': MQTT_username, 'password': MQTT_password }
507+
502508
msg ={}
503509
config = 1
504-
names =["pytes_current", "pytes_voltage" , "pytes_temperature", "pytes_soc", "pytes_status"]
505-
ids =["current", "voltage" , "temperature", "soc", "basic_st"] #do not change the prefix "pytes_"
510+
names =["current", "voltage" , "temperature", "soc", "status"]
511+
ids =["current", "voltage" , "temperature", "soc", "basic_st"]
506512
dev_cla =["current", "voltage", "temperature", "battery","None"]
507513
stat_cla =["measurement","measurement","measurement","measurement","None"]
508514
unit_of_meas =["A","V","°C", "%","None"]
509-
515+
510516
# define system sensors
511517
for n in range(5):
512-
state_topic ="homeassistant/sensor/pytes/"+str(config)+"/config"
518+
state_topic = "homeassistant/sensor/" + dev_name + "/" + str(config) + "/config"
513519
msg ["name"] = names[n]
514-
msg ["stat_t"] = "homeassistant/sensor/pytes/state"
515-
msg ["uniq_id"] = "pytes_"+ids[n]
516-
if dev_cla[n] != "None":
520+
msg ["stat_t"] = "homeassistant/sensor/" + dev_name + "/state"
521+
msg ["uniq_id"] = dev_name + "_" + ids[n]
522+
if dev_cla[n] != "None":
517523
msg ["dev_cla"] = dev_cla[n]
518524
if stat_cla[n] != "None":
519-
msg ["stat_cla"] = stat_cla[n]
525+
msg ["stat_cla"] = stat_cla[n]
520526
if unit_of_meas[n] != "None":
521527
msg ["unit_of_meas"] = unit_of_meas[n]
528+
522529
msg ["val_tpl"] = "{{ value_json." + ids[n]+ "}}"
523-
msg ["dev"] = {"identifiers": ["pytes"],"manufacturer": "PYTES","model": "E-Box48100R","name": "pytes_ebox","sw_version": "1.0"}
524-
530+
msg ["dev"] = {"identifiers": [dev_name],"manufacturer": manufacturer,"model": model,"name": dev_name,"sw_version": sw_ver}
525531
message = json.dumps(msg)
532+
526533
publish.single(state_topic, message, hostname=MQTT_broker, port= MQTT_port, auth=MQTT_auth, qos=0, retain=True)
527534

528535
b = "...mqtt auto discovery initialization :" + str(round(config/(5*powers+5)*100)) +" %"
@@ -535,20 +542,21 @@ def mqtt_discovery():
535542
# define individual batteries sensors
536543
for power in range (1, powers+1):
537544
for n in range(5):
538-
state_topic ="homeassistant/sensor/pytes/"+str(config)+"/config"
545+
state_topic ="homeassistant/sensor/" + dev_name + "/" + str(config) + "/config"
539546
msg ["name"] = names[n]+"_"+str(power)
540-
msg ["stat_t"] = "homeassistant/sensor/pytes/state"
541-
msg ["uniq_id"] = "pytes_"+ids[n]+"_"+str(power)
547+
msg ["stat_t"] = "homeassistant/sensor/" + dev_name + "/state"
548+
msg ["uniq_id"] = dev_name + "_" +ids[n]+"_"+str(power)
542549
if dev_cla[n] != "None":
543550
msg ["dev_cla"] = dev_cla[n]
544551
if stat_cla[n] != "None":
545552
msg ["stat_cla"] = stat_cla[n]
546553
if unit_of_meas[n] != "None":
547554
msg ["unit_of_meas"] = unit_of_meas[n]
548-
msg ["val_tpl"] = "{{ value_json.pytes[" + str(power-1) + "]." + ids[n]+ "}}"
549-
msg ["dev"] = {"identifiers": ["pytes"],"manufacturer": "PYTES","model": "E-Box48100R","name": "pytes_ebox","sw_version": "1.0"}
550-
555+
556+
msg ["val_tpl"] = "{{ value_json.devices[" + str(power-1) + "]." + ids[n]+ "}}"
557+
msg ["dev"] = {"identifiers": [dev_name],"manufacturer": manufacturer,"model": model,"name": dev_name,"sw_version": sw_ver}
551558
message = json.dumps(msg)
559+
552560
publish.single(state_topic, message, hostname=MQTT_broker, port= MQTT_port, auth=MQTT_auth, qos=0, retain=True)
553561

554562
b = "...mqtt auto discovery initialization :" + str(round(config/(5*powers+5)*100)) +" %"
@@ -569,7 +577,7 @@ def mqtt_publish():
569577
MQTT_auth = None
570578
if len(MQTT_username) >0:
571579
MQTT_auth = { 'username': MQTT_username, 'password': MQTT_password }
572-
state_topic = "homeassistant/sensor/pytes/state"
580+
state_topic = "homeassistant/sensor/" + dev_name + "/state"
573581
message = json.dumps(json_data)
574582
publish.single(state_topic, message, hostname=MQTT_broker, port=MQTT_port, auth=MQTT_auth)
575583
print ('...mqtt publish : ok')
@@ -651,7 +659,7 @@ def parsing_bat(power):
651659
if cells == 15:
652660
coulomb_idx = line_str.find('SOC') # pylon
653661
else:
654-
coulomb_idx = line_str.find('Coulomb') + 1 # pytes
662+
coulomb_idx = line_str.find('Coulomb') # pytes
655663

656664
decode = 'true'
657665

@@ -664,7 +672,7 @@ def parsing_bat(power):
664672
volt_st = line_str[volt_st_idx:volt_st_idx+8]
665673
current_st = line_str[curr_st_idx:curr_st_idx+8]
666674
temp_st = line_str[temp_st_idx:temp_st_idx+8]
667-
coulomb = line_str[coulomb_idx:coulomb_idx+3]
675+
coulomb = line_str[coulomb_idx:coulomb_idx+4]
668676

669677
cells_data = {
670678
'cell':cell,

0 commit comments

Comments
 (0)