Skip to content

Commit

Permalink
Merge pull request #126 from Poshy163/main
Browse files Browse the repository at this point in the history
Support State of Charge for Storion-S5
  • Loading branch information
CharlesGillanders authored Aug 20, 2024
2 parents 9afd1b7 + bd47d18 commit 9b8c2ba
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 22 deletions.
10 changes: 5 additions & 5 deletions custom_components/alphaess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,24 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

client = alphaess.alphaess(entry.data["AppID"], entry.data["AppSecret"])

coordinator = AlphaESSDataUpdateCoordinator(hass, client=client)
await coordinator.async_config_entry_first_refresh()
_coordinator = AlphaESSDataUpdateCoordinator(hass, client=client)
await _coordinator.async_config_entry_first_refresh()

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = _coordinator

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

entry.async_on_unload(entry.add_update_listener(update_listener))

async def async_battery_charge_handler(call):
await client.updateChargeConfigInfo(call.data.get('serial'), call.data.get('chargestopsoc'),
int(call.data.get('enabled') == True), call.data.get('cp1end'),
int(call.data.get('enabled') is True), call.data.get('cp1end'),
call.data.get('cp2end'), call.data.get('cp1start'),
call.data.get('cp2start'))

async def async_battery_discharge_handler(call):
await client.updateDisChargeConfigInfo(call.data.get('serial'), call.data.get('dischargecutoffsoc'),
int(call.data.get('enabled') == True), call.data.get('dp1end'),
int(call.data.get('enabled') is True), call.data.get('dp1end'),
call.data.get('dp2end'), call.data.get('dp1start'),
call.data.get('dp2start'))

Expand Down
18 changes: 17 additions & 1 deletion custom_components/alphaess/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
DOMAIN = "alphaess"
PLATFORMS = [Platform.SENSOR]
SCAN_INTERVAL = timedelta(minutes=1)
THROTTLE_MULTIPLIER = 1.25
THROTTLE_MULTIPLIER = 1.35
INVERTER_COUNT = 0
INVERTER_LIST = []

NAME = "Alpha ESS"
ISSUE_URL = "https://github.com/CharlesGillanders/homeassistant-alphaESS/issues"
Expand All @@ -30,3 +31,18 @@ def increment_inverter_count():

def get_inverter_count():
return INVERTER_COUNT


# If no Storion-S5, make the throttle amount smaller
def set_throttle_count_lower():
global THROTTLE_MULTIPLIER
THROTTLE_MULTIPLIER = 1.25


def add_inverter_to_list(inverter):
global INVERTER_LIST
INVERTER_LIST.append(inverter)


def get_inverter_list():
return INVERTER_LIST
19 changes: 16 additions & 3 deletions custom_components/alphaess/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DOMAIN, SCAN_INTERVAL, THROTTLE_MULTIPLIER, get_inverter_count
from .const import DOMAIN, SCAN_INTERVAL, THROTTLE_MULTIPLIER, get_inverter_count, set_throttle_count_lower, get_inverter_list

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand All @@ -32,19 +32,26 @@ def __init__(self, hass: HomeAssistant, client: alphaess.alphaess) -> None:
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
self.api = client
self.update_method = self._async_update_data
self.has_throttle = True
self.data: dict[str, dict[str, float]] = {}
self.LOCAL_INVERTER_COUNT = 0

async def _async_update_data(self):
"""Update data via library."""

model_list = get_inverter_list()
if "Storion-S5" not in model_list and len(model_list) > 0:
self.has_throttle = False
set_throttle_count_lower()

inverter_count = get_inverter_count()
if inverter_count == 1:
LOCAL_INVERTER_COUNT = 0
else:
LOCAL_INVERTER_COUNT = inverter_count

try:
jsondata = await self.api.getdata(THROTTLE_MULTIPLIER * LOCAL_INVERTER_COUNT)
jsondata = await self.api.getdata(self.has_throttle, THROTTLE_MULTIPLIER * LOCAL_INVERTER_COUNT)
if jsondata is not None:
for invertor in jsondata:

Expand All @@ -61,6 +68,7 @@ async def _async_update_data(self):
_sumdata = invertor.get("SumData", {})
_onedateenergy = invertor.get("OneDateEnergy", {})
_powerdata = invertor.get("LastPower", {})
_onedatepower = invertor.get("OneDayPower", {})

inverterdata["Total Load"] = await safe_get(_sumdata, "eload")
inverterdata["Total Income"] = await safe_get(_sumdata, "totalIncome")
Expand All @@ -87,7 +95,12 @@ async def _async_update_data(self):
_pvpowerdetails = _powerdata.get("ppvDetail", {})

inverterdata["Instantaneous Battery SOC"] = _soc
inverterdata["State of Charge"] = _soc

if _onedatepower and _soc is 0:
first_entry = _onedatepower[0]
_cbat = first_entry.get("cbat", 0)
inverterdata["State of Charge"] = _cbat

inverterdata["Instantaneous Battery I/O"] = await safe_get(_powerdata, "pbat")
inverterdata["Instantaneous Load"] = await safe_get(_powerdata, "pload")
inverterdata["Instantaneous Generation"] = await safe_get(_powerdata, "ppv")
Expand Down
17 changes: 13 additions & 4 deletions custom_components/alphaess/manifest.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
{
"domain": "alphaess",
"name": "Alpha ESS",
"after_dependencies": ["rest"],
"codeowners": ["@CharlesGillanders"],
"after_dependencies": [
"rest"
],
"codeowners": [
"@CharlesGillanders",
"@Poshy163"
],
"config_flow": true,
"documentation": "https://github.com/CharlesGillanders/homeassistant-alphaESS",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/CharlesGillanders/homeassistant-alphaESS/issues",
"requirements": ["alphaessopenapi==0.0.10"],
"version": "0.5.3"
"requirements": [
"alphaessopenapi==0.0.11"
],
"version": "0.5.4"
}


3 changes: 2 additions & 1 deletion custom_components/alphaess/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN, increment_inverter_count
from .const import DOMAIN, increment_inverter_count, add_inverter_to_list
from .coordinator import AlphaESSDataUpdateCoordinator

_LOGGER: logging.Logger = logging.getLogger(__package__)
Expand All @@ -39,6 +39,7 @@ async def async_setup_entry(hass, entry, async_add_entities) -> None:
for serial, data in coordinator.data.items():
model = data.get("Model")
_LOGGER.info(f"Serial: {serial}, Model: {model}")
add_inverter_to_list(model)
increment_inverter_count()

if model == "Storion-S5":
Expand Down
16 changes: 8 additions & 8 deletions custom_components/alphaess/sensorlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
AlphaESSSensorDescription(
key=AlphaESSNames.StateOfCharge,
name="State of Charge",
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.BATTERY,
state_class=SensorStateClass.MEASUREMENT,
),
AlphaESSSensorDescription(
key=AlphaESSNames.Charge,
name="Charge",
Expand Down Expand Up @@ -223,6 +216,13 @@
]

LIMITED_SENSOR_DESCRIPTIONS: List[AlphaESSSensorDescription] = [
AlphaESSSensorDescription(
key=AlphaESSNames.StateOfCharge,
name="State of Charge",
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.BATTERY,
state_class=SensorStateClass.MEASUREMENT,
),
AlphaESSSensorDescription(
key=AlphaESSNames.SolarProduction,
name="Solar Production",
Expand Down Expand Up @@ -338,4 +338,4 @@
device_class=SensorDeviceClass.ENERGY,
entity_category=EntityCategory.DIAGNOSTIC
)
]
]

0 comments on commit 9b8c2ba

Please sign in to comment.