Skip to content

Commit

Permalink
Revert computed fields
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Ott <[email protected]>
  • Loading branch information
DerOetzi committed Jan 4, 2024
1 parent 2f7b8b9 commit ea68dd4
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions solaredge2mqtt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from enum import Enum

from pydantic import BaseModel, model_serializer, computed_field
from pydantic import BaseModel, model_serializer
from solaredge_modbus import (
BATTERY_STATUS_MAP,
C_SUNSPEC_DID_MAP,
Expand Down Expand Up @@ -73,7 +73,7 @@ def map(cls, data: Dict[str, str | int]) -> Dict[str, str]:
class InfluxDBModel(BaseModel):
def influxdb_fields(self, prefix: Optional[str] = None) -> Dict[str, Any]:
fields = {}
for attr, value in self.model_dump().items():
for attr, value in self.__dict__.items():
if attr == "info":
continue
if isinstance(value, InfluxDBModel):
Expand Down Expand Up @@ -391,6 +391,8 @@ def calc(

class GridPowerflow(InfluxDBModel):
power: int
consumption: int
delivery: int

@staticmethod
def calc(meters_data: Dict[str, SunSpecMeter]) -> GridPowerflow:
Expand All @@ -399,54 +401,49 @@ def calc(meters_data: Dict[str, SunSpecMeter]) -> GridPowerflow:
if "Import" in meter.info.option and "Export" in meter.info.option:
grid += meter.power.actual

return GridPowerflow(power=grid)

@computed_field
@property
def consumption(self) -> int:
return abs(self.power) if self.power < 0 else 0
if grid >= 0:
consumption = 0
delivery = grid
else:
consumption = int(abs(grid))
delivery = 0

@computed_field
@property
def delivery(self) -> int:
return self.power if self.power >= 0 else 0
return GridPowerflow(power=grid, consumption=consumption, delivery=delivery)


class BatteryPowerflow(InfluxDBModel):
power: int
charge: int
discharge: int

@staticmethod
def calc(batteries_data: Dict[str, SunSpecBattery]) -> BatteryPowerflow:
batteries_power = 0
for battery in batteries_data.values():
batteries_power += battery.power

return BatteryPowerflow(power=batteries_power)

@computed_field
@property
def charge(self) -> int:
return self.power if self.power >= 0 else 0
if batteries_power >= 0:
charge = batteries_power
discharge = 0
else:
charge = 0
discharge = abs(batteries_power)

@computed_field
@property
def discharge(self) -> int:
return abs(self.power) if self.power < 0 else 0
return BatteryPowerflow(
power=batteries_power, charge=charge, discharge=discharge
)


class ConsumerPowerflow(InfluxDBModel):
house: int
evcharger: int = 0
inverter: int

total: int

used_pv_production: int
used_battery_production: int

@computed_field
@property
def total(self) -> int:
return self.house + self.evcharger + self.inverter

@staticmethod
def calc(
inverter: InverterPowerflow, grid: GridPowerflow, evcharger: int
Expand All @@ -458,6 +455,8 @@ def calc(
# Happens when EV Charger starts up and meters are not yet updated
evcharger = 0

total = house + evcharger + inverter.consumption

if inverter.pv_production > inverter.production - grid.delivery:
pv_production = inverter.pv_production - grid.delivery
else:
Expand All @@ -474,6 +473,7 @@ def calc(
used_pv_production=pv_production,
used_battery_production=battery_production,
inverter=inverter.consumption,
total=total,
)

def is_valid(self) -> bool:
Expand Down

0 comments on commit ea68dd4

Please sign in to comment.