Skip to content

Commit

Permalink
Scipy dependency removed
Browse files Browse the repository at this point in the history
  • Loading branch information
odya committed Aug 25, 2024
1 parent 4875f15 commit 46f2191
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
2 changes: 1 addition & 1 deletion custom_components/ina219_ups_hat/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"integration_type": "device",
"iot_class": "local_polling",
"issue_tracker": "https://github.com/odya/hass-ina219-ups-hat/issues",
"requirements": ["smbus2>=0.4.2","numpy>=1.21.4","pandas","scipy"],
"requirements": ["smbus2>=0.4.2","numpy","pandas"],
"version": "0.3.11"
}
30 changes: 12 additions & 18 deletions custom_components/ina219_ups_hat/soc/provider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import pandas as pd
from scipy.interpolate import interp1d

from homeassistant import core


Expand All @@ -16,12 +16,8 @@ def __init__(self, hass: core.HomeAssistant, ocv_map) -> None:
)

# Create the cubic interpolation function
cubic_interp_func = interp1d(
soc_voltage_df["SOC"],
soc_voltage_df["Voltage"],
kind="cubic",
fill_value="extrapolate",
)
coefficients = np.polyfit(soc_voltage_df["SOC"], soc_voltage_df["Voltage"], 3)
cubic_interp_func = np.poly1d(coefficients)
# Precalculate detailed SOC-Voltage data
detailed_soc = np.linspace(0, 100, 100)
detailed_voltage = cubic_interp_func(detailed_soc)
Expand All @@ -30,20 +26,18 @@ def __init__(self, hass: core.HomeAssistant, ocv_map) -> None:
{"SOC": detailed_soc, "Voltage": detailed_voltage}
)

# Interpolation function
self._linear_interp_func = interp1d(
self._detailed_soc_voltage_df["Voltage"],
self._detailed_soc_voltage_df["SOC"],
kind="linear",
fill_value="extrapolate",
)

def get_soc_from_voltage(self, cell_voltage: float) -> float:
# Ensure the voltage is within the measurable range
if cell_voltage > self._detailed_soc_voltage_df["Voltage"].max():
if cell_voltage >= self._detailed_soc_voltage_df["Voltage"].max():
return 100
if cell_voltage < self._detailed_soc_voltage_df["Voltage"].min():
if cell_voltage <= self._detailed_soc_voltage_df["Voltage"].min():
return 0

# Interpolate the SOC based on the voltage
return float(self._linear_interp_func(cell_voltage))
return float(
np.interp(
cell_voltage,
self._detailed_soc_voltage_df["Voltage"],
self._detailed_soc_voltage_df["SOC"],
)
)

0 comments on commit 46f2191

Please sign in to comment.