Skip to content

Commit

Permalink
Realign battery power limits with GE portal (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdpuk authored Feb 11, 2023
1 parent 2a70b3c commit e4c9718
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
32 changes: 20 additions & 12 deletions custom_components/givenergy_local/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,30 +500,38 @@ def native_value(self) -> StateType:
return "Unknown"


class BatteryChargeLimitSensor(InverterBasicSensor):
class BatteryPowerLimitSensor(InverterBasicSensor):
"""Battery power limit sensor for charging or discharging."""

def convert_raw_power_to_watts(self, raw_value: int) -> int:
"""Convert an inverter register value to a power value in Watts."""
# Here be dragons.
# Interprets the raw value in the same way as the GivEnergy web portal.
# Step values are 81W up to 30*81=2430W.
# It's possible to write raw values of 31, 32 and 50 using the portal and/or
# app, but all of these are rendered as 2600W when read back in the portal.
# At time of writing, the official app uses a slightly different step value.
# Tested on a Gen 1 inverter with +/- 2.6kW max battery power; different logic
# almost certainly required on other units.
return 2600 if raw_value > 30 else raw_value * 81


class BatteryChargeLimitSensor(BatteryPowerLimitSensor):
"""Battery charge limit sensor."""

@property
def native_value(self) -> StateType:
"""Map the low-level value to power in Watts."""
raw_value = self.data.battery_charge_limit
return self.convert_raw_power_to_watts(self.data.battery_charge_limit)

# Warning: value for batteries with max charge/discharge rates of 2.6kW
# Different logic almost certainly required on other units
return 2600 if raw_value == 50 else raw_value * 64


class BatteryDischargeLimitSensor(InverterBasicSensor):
class BatteryDischargeLimitSensor(BatteryPowerLimitSensor):
"""Battery discharge limit sensor."""

@property
def native_value(self) -> StateType:
"""Map the low-level value to power in Watts."""
raw_value = self.data.battery_discharge_limit

# Warning: value for batteries with max charge/discharge rates of 2.6kW
# Different logic almost certainly required on other units
return 2600 if raw_value == 50 else raw_value * 64
return self.convert_raw_power_to_watts(self.data.battery_discharge_limit)


class BatteryBasicSensor(BatteryEntity, SensorEntity):
Expand Down
14 changes: 8 additions & 6 deletions custom_components/givenergy_local/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ async def _async_set_charge_power_limit(
"""Set the maximum battery charge power."""

def call(client: GivEnergyClient) -> None:
target_value = int(data[_ATTR_POWER] / 64)
target_value = int(data[_ATTR_POWER] / 81)

# Numbering seems to stop at 39, then jump to 50 = 2.6kW
if target_value > 39:
# Numbering seems to stop at 30, then jump to 50 = 2.6kW
# See extensive comments in the corresponding sensor
if target_value > 30:
target_value = 50

LOGGER.debug(
Expand All @@ -176,10 +177,11 @@ async def _async_set_discharge_power_limit(
"""Set the maximum battery discharge power."""

def call(client: GivEnergyClient) -> None:
target_value = int(data[_ATTR_POWER] / 64)
target_value = int(data[_ATTR_POWER] / 81)

# Numbering seems to stop at 39, then jump to 50 = 2.6kW
if target_value > 39:
# Numbering seems to stop at 30, then jump to 50 = 2.6kW
# See extensive comments in the corresponding sensor
if target_value > 30:
target_value = 50

LOGGER.debug(
Expand Down
8 changes: 4 additions & 4 deletions custom_components/givenergy_local/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ set_charge_limit:
power:
name: Power
description: >
The value here will be rounded down to the nearest 64W, which is the
The value here will be rounded down to the nearest 81W, which is the
step size supported by the inverter. The exception is at the top end
of the scale, where 2496W and 2600W are the highest supported values.
of the scale, where 2430W and 2600W are the highest supported values.
required: true
selector:
number:
Expand All @@ -36,9 +36,9 @@ set_discharge_limit:
power:
name: Power
description: >
The value here will be rounded down to the nearest 64W, which is the
The value here will be rounded down to the nearest 81W, which is the
step size supported by the inverter. The exception is at the top end
of the scale, where 2496W and 2600W are the highest supported values.
of the scale, where 2430W and 2600W are the highest supported values.
required: true
selector:
number:
Expand Down

0 comments on commit e4c9718

Please sign in to comment.