Skip to content

Commit

Permalink
adding docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
flip555 committed Sep 19, 2023
1 parent 9df09c7 commit 06d0c47
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 2 deletions.
4 changes: 4 additions & 0 deletions custom_components/bms_connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
PLATFORMS = ["sensor"]

async def async_setup(hass, config):
"""Set up the BMS Connector integration."""
hass.data[DOMAIN] = {}
return True

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a BMS Connector entry."""
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = entry.data # You can store entry data if needed

Expand All @@ -26,10 +28,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a BMS Connector entry."""
if unloaded := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unloaded

async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload a BMS Connector entry."""
await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)
1 change: 1 addition & 0 deletions custom_components/bms_connector/bms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Holding File."""
1 change: 1 addition & 0 deletions custom_components/bms_connector/bms/seplos/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Holding File."""
1 change: 1 addition & 0 deletions custom_components/bms_connector/bms/seplos/v2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Holding File."""
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Alarms:
"""Represents an object to store alarm information."""

def __init__(self):
"""Initialize an empty Alarms object with default values."""
self.cellsCount = 0
self.cellAlarm = []
self.tempCount = 0
Expand All @@ -23,6 +26,7 @@ def __init__(self):
self.alarmEvent7 = 0

def __str__(self):
"""Return a string representation of the Alarms object."""
return (
f"cellsCount: {self.cellsCount}, "
f"cellAlarm: {self.cellAlarm}, "
Expand All @@ -48,6 +52,7 @@ def __str__(self):
)

def parse_teledata_info(info_str):
"""Parse a string containing teledata information and return an Alarms object."""
result = Alarms()
cursor = 4

Expand Down
159 changes: 157 additions & 2 deletions custom_components/bms_connector/bms/seplos/v2/calc_functions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
def extract_data(data):
"""
Extract telemetry data from the given input.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The telemetry data.
"""
battery_address, telemetry, alarms, system_details, protection_settings = data
return telemetry

def battery_watts(data):
"""
Calculate the battery watts based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The calculated battery watts.
"""
telemetry = extract_data(data)
volts = getattr(telemetry, 'portVoltage', 0.0)
amps = getattr(telemetry, 'current', 0.0)
Expand All @@ -11,24 +29,61 @@ def battery_watts(data):
return 0.0 # Handle the case when volts or amps are not numeric values

def remaining_watts(data):
"""
Calculate the remaining watts based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The calculated remaining watts.
"""
telemetry = extract_data(data)
volts = getattr(telemetry, 'voltage', 0.0)
amps = getattr(telemetry, 'resCap', 0.0)
return volts * amps


def capacity_watts(data):
"""
Calculate the capacity watts based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The calculated capacity watts.
"""
telemetry = extract_data(data)
volts = getattr(telemetry, 'voltage', 0.0)
amps = getattr(telemetry, 'capacity', 0.0)
return volts * amps

def full_charge_amps(data):
"""
Calculate the full charge amps based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The calculated full charge amps.
"""
telemetry = extract_data(data)
remaining = getattr(telemetry, 'resCap', 0.0)
capacity = getattr(telemetry, 'capacity', 0.0)
return capacity - remaining

def full_charge_watts(data):
"""
Calculate the full charge watts based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The calculated full charge watts.
"""
telemetry = extract_data(data)
voltage = getattr(telemetry, 'voltage', 0.0)
resCap = getattr(telemetry, 'resCap', 0.0)
Expand All @@ -38,6 +93,16 @@ def full_charge_watts(data):
return cap_w - remaining_w

def get_cell_extremes_and_difference(data):
"""
Calculate the highest cell voltage, lowest cell voltage, voltage difference, highest cell number, and lowest cell number
based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
A tuple containing the highest cell voltage, lowest cell voltage, voltage difference, highest cell number, and lowest cell number.
"""
telemetry = extract_data(data)
cell_voltages = getattr(telemetry, f"cellVoltage", 0.0)
highest_cell_voltage = max(cell_voltages)
Expand All @@ -48,50 +113,140 @@ def get_cell_extremes_and_difference(data):
return highest_cell_voltage, lowest_cell_voltage, difference, highest_cell_number, lowest_cell_number

def highest_cell_voltage(data):
"""
Get the highest cell voltage based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The highest cell voltage.
"""
telemetry = extract_data(data)
return get_cell_extremes_and_difference(data)[0]

def lowest_cell_voltage(data):
"""
Get the lowest cell voltage based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The lowest cell voltage.
"""
telemetry = extract_data(data)
return get_cell_extremes_and_difference(data)[1]

def cell_voltage_difference(data):
"""
Calculate the cell voltage difference based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The cell voltage difference.
"""
telemetry = extract_data(data)
return get_cell_extremes_and_difference(data)[2]

def highest_cell_number(data):
"""
Get the highest cell number based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The highest cell number.
"""
telemetry = extract_data(data)
return get_cell_extremes_and_difference(data)[3]

def lowest_cell_number(data):
"""
Get the lowest cell number based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The lowest cell number.
"""
telemetry = extract_data(data)
return get_cell_extremes_and_difference(data)[4]

def highest_temp(data):
"""
Get the highest temperature based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The highest temperature.
"""
telemetry = extract_data(data)
return max(getattr(telemetry, 'temperatures', [0.0]))

def lowest_temp(data):
"""
Get the lowest temperature based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The lowest temperature.
"""
telemetry = extract_data(data)
return min(getattr(telemetry, 'temperatures', [0.0]))

def delta_temp(data):
"""
Calculate the temperature difference based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The temperature difference.
"""
telemetry = extract_data(data)
temps = getattr(telemetry, 'temperatures', [])
if temps:
return max(temps) - min(temps)
return 0.0

def highest_temp_sensor(data):
"""
Get the sensor with the highest temperature based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The sensor with the highest temperature.
"""
telemetry = extract_data(data)
temps = getattr(telemetry, 'temperatures', [])
if temps:
return f"Sensor {temps.index(max(temps)) + 1}"
return "N/A"

def lowest_temp_sensor(data):
"""
Get the sensor with the lowest temperature based on telemetry data.
Args:
data: A tuple containing battery_address, telemetry, alarms, system_details, and protection_settings.
Returns:
The sensor with the lowest temperature.
"""
telemetry = extract_data(data)
temps = getattr(telemetry, 'temperatures', [])
if temps:
return f"Sensor {temps.index(min(temps)) + 1}"
return "N/A"
return "N/A"

0 comments on commit 06d0c47

Please sign in to comment.