Skip to content

Commit

Permalink
Update sensor.py (#267)
Browse files Browse the repository at this point in the history
Added sensor Energy Generated Cumulative

Re-factored old energy generation classes
  • Loading branch information
FozzieUK1 authored Sep 17, 2024
1 parent f980a47 commit 01ca885
Showing 1 changed file with 15 additions and 47 deletions.
62 changes: 15 additions & 47 deletions custom_components/foxess/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ async def async_update_data():
FoxESSPowerString(coordinator, name, deviceID, "Bat Discharge Power", "bat-discharge-power", "batDischargePower"),
FoxESSPowerString(coordinator, name, deviceID, "Bat Charge Power", "bat-charge-power", "batChargePower"),
FoxESSPowerString(coordinator, name, deviceID, "Load Power", "load-power", "loadsPower"),
FoxESSEnergyGenerated(coordinator, name, deviceID),
FoxESSEnergyGeneratedMonth(coordinator, name, deviceID),
FoxESSEnergyGenerated(coordinator, name, deviceID, "Energy Generated", "energy-generated", "value"),
FoxESSEnergyGenerated(coordinator, name, deviceID, "Energy Generated Month", "energy-generated-month", "month"),
FoxESSEnergyGenerated(coordinator, name, deviceID, "Energy Generated Cumulative", "energy-generated-cumulative", "cumulative"),
FoxESSEnergyGridConsumption(coordinator, name, deviceID),
FoxESSEnergyFeedin(coordinator, name, deviceID),
FoxESSEnergyBatCharge(coordinator, name, deviceID),
Expand Down Expand Up @@ -903,53 +904,20 @@ def native_value(self) -> float | None:
return self.coordinator.data["raw"]["PowerFactor"]
return None


class FoxESSEnergyGenerated(CoordinatorEntity, SensorEntity):

_attr_state_class: SensorStateClass = SensorStateClass.TOTAL_INCREASING
_attr_device_class = SensorDeviceClass.ENERGY
_attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR

def __init__(self, coordinator, name, deviceID):
super().__init__(coordinator=coordinator)
_LOGGER.debug("Initiating Entity - Energy Generated")
self._attr_name = name+" - Energy Generated"
self._attr_unique_id = deviceID+"energy-generated"
self.status = namedtuple(
"status",
[
ATTR_DATE,
ATTR_TIME,
],
)

@property
def native_value(self) -> str | None:
if "value" not in self.coordinator.data["reportDailyGeneration"]:
_LOGGER.debug("reportDailyGeneration value None")
else:
if self.coordinator.data["reportDailyGeneration"]["value"] == 0:
energygenerated = 0
else:
energygenerated = self.coordinator.data["reportDailyGeneration"]["value"]
if energygenerated > 0:
energygenerated = round(energygenerated,3)
else:
energygenerated = 0
return energygenerated
return None

class FoxESSEnergyGeneratedMonth(CoordinatorEntity, SensorEntity):

_attr_state_class: SensorStateClass = SensorStateClass.TOTAL_INCREASING
_attr_device_class = SensorDeviceClass.ENERGY
_attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR

def __init__(self, coordinator, name, deviceID):
def __init__(self, coordinator, name, deviceID, nameValue, uniqueValue, keyValue):
super().__init__(coordinator=coordinator)
_LOGGER.debug("Initiating Entity - Energy Generated Month")
self._attr_name = name+" - Energy Generated Month"
self._attr_unique_id = deviceID+"energy-generated-month"
self._nameValue = nameValue
self._uniqueValue = uniqueValue
self._keyValue = keyValue
_LOGGER.debug(f"Initiating Entity - {self._nameValue}")
self._attr_name = f"{name} - {self._nameValue}"
self._attr_unique_id = f"{deviceID}{self._uniqueValue}"
self.status = namedtuple(
"status",
[
Expand All @@ -959,14 +927,14 @@ def __init__(self, coordinator, name, deviceID):
)

@property
def native_value(self) -> str | None:
if "month" not in self.coordinator.data["reportDailyGeneration"]:
_LOGGER.debug("reportDailyGeneration month None")
def native_value(self) -> float | None:
if self._keyValue not in self.coordinator.data["reportDailyGeneration"]:
_LOGGER.debug(f"{self._keyValue} None")
else:
if self.coordinator.data["reportDailyGeneration"]["month"] == 0:
if self.coordinator.data["reportDailyGeneration"][self._keyValue] == 0:
energygenerated = 0
else:
energygenerated = self.coordinator.data["reportDailyGeneration"]["month"]
energygenerated = self.coordinator.data["reportDailyGeneration"][self._keyValue]
if energygenerated > 0:
energygenerated = round(energygenerated,3)
else:
Expand Down

0 comments on commit 01ca885

Please sign in to comment.