Skip to content

Commit

Permalink
Merge pull request #23 from flip555/next-branch
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
flip555 authored Sep 13, 2023
2 parents 4e85cf6 + d39012f commit d4db574
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
29 changes: 18 additions & 11 deletions custom_components/seplos_bms_ha/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,37 @@ def unique_id(self):

@property
def state(self):
"""Return the state of the sensor."""
if not self._attribute: # Check if attribute is None or empty
return super().state

base_attribute = self._attribute.split('[')[0] if '[' in self._attribute else self._attribute

value = None
if isinstance(self.coordinator.data, tuple):
telemetry_data, alarms_data, battery_address_1_data, battery_address_2_data = self.coordinator.data
value = self.get_value(telemetry_data) or self.get_value(alarms_data)
else:
value = self.get_value(self.coordinator.data)

if value is None or value == '':
_LOGGER.warning("No data found in telemetry or alarms for %s", self._name)
return None

if value is None or value == '':
if base_attribute == 'current':
_LOGGER.debug("Current seems to be None, setting to 0.00 to fix HA reporting as unknown")
return 0.00
else:
_LOGGER.warning("No data found in telemetry or alarms for %s", self._name)
return None

# Interpret the value for alarm sensors
if base_attribute in ALARM_ATTRIBUTES:
if value == '0':
return "No Alarm"
return str(self.interpret_alarm(base_attribute, value))

interpreted_value = str(self.interpret_alarm(base_attribute, value))
_LOGGER.debug("Interpreted value for %s: %s", base_attribute, interpreted_value)
return interpreted_value

_LOGGER.debug("Sensor state for %s: %s", self._name, value)
return value


@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
Expand Down Expand Up @@ -234,4 +241,4 @@ async def async_update_data():
# Combine all sensor lists
sensors = all_sensors + derived_sensors

async_add_entities(sensors, True)
async_add_entities(sensors, True)
24 changes: 13 additions & 11 deletions custom_components/seplos_bms_ha/seplos_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging

from .serial_comms import send_serial_command
from .telemetry import Telemetry, parse_telemetry_info
from .alarms_teledata import Alarms, parse_teledata_info
Expand All @@ -12,48 +13,49 @@ def calc_check_sum(s):

def form_battery_id_str(address):
return format(address, '02x')

def extract_data_from_message(msg, telemetry_requested=True, teledata_requested=True, debug=True):
if msg.startswith("~"):
msg = msg[1:] # remove the tilde at the beginning

check_sum = msg[-4:]
msg_wo_chk_sum = msg[:-4]
calculated_check_sum = calc_check_sum(msg_wo_chk_sum)

if debug:
print(calculated_check_sum)
_LOGGER.debug("Calculated Checksum: %s", calculated_check_sum)

if check_sum != calculated_check_sum:
if debug:
print("Checksum mismatch!")
_LOGGER.debug("Checksum mismatch!")
return None, None

address = int(msg_wo_chk_sum[2:4], 16)
address_string = '0x' + form_battery_id_str(address)
info = msg_wo_chk_sum[12:]

telemetry_result = None
teledata_result = None

if telemetry_requested and not teledata_requested:
try:
telemetry_result = parse_telemetry_info(info)
if telemetry_result is not None and debug:
print(address_string, telemetry_result.__dict__)
_LOGGER.debug("Telemetry Result for %s: %s", address_string, telemetry_result.__dict__)
except Exception as e:
if debug:
print(f"Telemetry parsing error: {e}")
_LOGGER.debug("Telemetry parsing error: %s", e)
_LOGGER.debug("About to return from extract_data_from_message. Telemetry: %s", telemetry_result)

elif teledata_requested and not telemetry_requested:
try:
teledata_result = parse_teledata_info(info)
if teledata_result is not None and debug:
print(address_string, teledata_result.__dict__)
_LOGGER.debug("Teledata Result for %s: %s", address_string, teledata_result.__dict__)
except Exception as e:
if debug:
print(f"Teledata parsing error: {e}")
_LOGGER.debug("Teledata parsing error: %s", e)
_LOGGER.debug("About to return from extract_data_from_message. Teledata: %s", teledata_result)

return telemetry_result, teledata_result, address_string

3 changes: 3 additions & 0 deletions custom_components/seplos_bms_ha/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def parse_telemetry_info(info_str):
if result.current > 32767:
result.current -= 65536
result.current /= 100

# fix current to 0.00 to test making sure its not reported as "unknown" in HA
#result.current = "0.00"

cursor += 4
result.voltage = int(info_str[cursor:cursor+4], 16) / 100
Expand Down
5 changes: 4 additions & 1 deletion custom_components/seplos_bms_ha/v2_calc_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ def battery_watts(data):
telemetry, alarms, battery_address_1, battery_address_2 = data
volts = getattr(telemetry, 'portVoltage', 0.0)
amps = getattr(telemetry, 'current', 0.0)
return volts * amps
if isinstance(volts, (float, int)) and isinstance(amps, (float, int)):
return volts * amps
return 0.0 # Handle the case when volts or amps are not numeric values


def remaining_watts(data):
telemetry, alarms, battery_address_1, battery_address_2 = data
Expand Down

0 comments on commit d4db574

Please sign in to comment.