Skip to content

Commit

Permalink
Use 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 3, 2024
1 parent fcde94f commit 49f22f1
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 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
from pydantic import BaseModel, model_serializer, computed_field
from solaredge_modbus import (
BATTERY_STATUS_MAP,
C_SUNSPEC_DID_MAP,
Expand Down Expand Up @@ -391,8 +391,6 @@ def calc(

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

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

if grid >= 0:
consumption = 0
delivery = grid
else:
consumption = abs(grid)
delivery = 0
return GridPowerflow(power=grid)

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

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


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

if batteries_power >= 0:
charge = batteries_power
discharge = 0
else:
charge = 0
discharge = abs(batteries_power)
return BatteryPowerflow(power=batteries_power)

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

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


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

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

@staticmethod
def calc(
inverter: InverterPowerflow, grid: GridPowerflow, evcharger: int
Expand Down

0 comments on commit 49f22f1

Please sign in to comment.