Skip to content

Commit fdc931f

Browse files
authored
v1.0.10
Introduces support for third-party PV input via plant_third_party_photovoltaic_power. Updates calculate_total_pv_power to return a safe float aggregate of plant and third-party sources. Refactors binary sensor logic in binary_sensor.py to rely on dynamic PV power calculations instead of static keys. Improves accuracy of plant_consumed_power by utilizing the updated total PV power metric. Bumps version to 1.0.10 in manifest.json to reflect major functional improvement.
2 parents a30ee66 + cb8ba52 commit fdc931f

3 files changed

Lines changed: 34 additions & 14 deletions

File tree

custom_components/sigen/binary_sensor.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .coordinator import SigenergyDataUpdateCoordinator
2222
from .sigen_entity import SigenergyEntity
2323
from .common import generate_sigen_entity
24+
from .calculated_sensor import SigenergyCalculations
2425

2526
_LOGGER = logging.getLogger(__name__)
2627

@@ -42,9 +43,16 @@ class SigenergyBinarySensorEntityDescription(
4243
name="PV Generating",
4344
device_class=BinarySensorDeviceClass.POWER,
4445
icon="mdi:solar-power",
45-
source_key="plant_photovoltaic_power",
46-
value_fn=lambda data: (val := data.get("plant_photovoltaic_power")) \
47-
is not None and Decimal(str(val)) > Decimal("0.01"),
46+
source_key=None, # No longer a direct key, calculated from multiple values
47+
value_fn=lambda data: (
48+
(
49+
power := SigenergyCalculations.calculate_total_pv_power(
50+
None, coordinator_data={"plant": data}
51+
)
52+
)
53+
is not None
54+
and power > 0.01
55+
),
4856
),
4957
SigenergyBinarySensorEntityDescription(
5058
key="plant_battery_charging",
@@ -161,11 +169,17 @@ def is_on(self) -> bool | None:
161169

162170
plant_data = self.coordinator.data["plant"]
163171

164-
# Check if the source key exists in the plant data
165-
if self.entity_description.source_key not in plant_data:
166-
_LOGGER.debug("[%s] Source key '%s' not found in plant data",
167-
self.entity_id, self.entity_description.source_key)
168-
return None # Source data missing
172+
# If a source key is defined, check if it exists in the plant data
173+
if (
174+
self.entity_description.source_key is not None
175+
and self.entity_description.source_key not in plant_data
176+
):
177+
_LOGGER.debug(
178+
"[%s] Source key '%s' not found in plant data",
179+
self.entity_id,
180+
self.entity_description.source_key,
181+
)
182+
return None # Source data missing
169183

170184
# Check if value_fn is defined
171185
if self.entity_description.value_fn is None:

custom_components/sigen/calculated_sensor.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def calculate_total_pv_power(
139139
plant_data = coordinator_data.get("plant", {})
140140

141141
plant_pv_power = safe_float(
142-
plant_data.get("plant_photovoltaic_power"))
142+
plant_data.get("plant_sigen_photovoltaic_power"))
143143
thirdparty_pv_power = safe_float(
144144
plant_data.get("plant_third_party_photovoltaic_power"))
145145

@@ -149,7 +149,7 @@ def calculate_total_pv_power(
149149
_LOGGER.debug("[CS][Total PV Power] Both plant_photovoltaic_power and thirdparty_pv_power are unavailable.")
150150
return None
151151

152-
return (plant_pv_power or 0.0) + (thirdparty_pv_power or 0.0)
152+
return safe_float((plant_pv_power or 0.0) + (thirdparty_pv_power or 0.0))
153153

154154
@staticmethod
155155
def calculate_pv_power(
@@ -327,10 +327,11 @@ def calculate_plant_consumed_power(
327327
# Get the required values from coordinator data
328328
plant_data = coordinator_data["plant"]
329329

330-
# Get PV power
331-
pv_power = plant_data.get("plant_photovoltaic_power")
330+
# Use the correct calculation for total PV power
331+
pv_power = SigenergyCalculations.calculate_total_pv_power(
332+
None, coordinator_data=coordinator_data
333+
)
332334

333-
# Get grid active power and calculate import/export
334335
grid_power = plant_data.get("plant_grid_sensor_active_power")
335336

336337
# Get battery power
@@ -342,6 +343,11 @@ def calculate_plant_consumed_power(
342343

343344
# Validate input types
344345
if not isinstance(pv_power, (int, float)):
346+
_LOGGER.warning(
347+
"[CS][Plant Consumed] PV power is not a number: %s (type: %s)",
348+
pv_power,
349+
type(pv_power).__name__,
350+
)
345351
return None
346352
if not isinstance(grid_power, (int, float)):
347353
_LOGGER.warning(

custom_components/sigen/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
"loggers": ["custom_components.sigen"],
2222
"quality_scale": "custom",
2323
"requirements": ["pymodbus>=3.0.0"],
24-
"version": "1.0.8"
24+
"version": "1.0.10"
2525
}

0 commit comments

Comments
 (0)