Skip to content

Commit

Permalink
add directional entities for battery and grid
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarv committed Mar 29, 2024
1 parent 534a5e3 commit 1b32c2a
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions custom_components/lg_ess/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ async def async_setup_entry(
"bat_use",
icon=_BATTERYHOME,
),
# 1: CHARGING, 2: DISCHARGING
MeasurementSensor(
home_coordinator,
device_info,
Expand Down Expand Up @@ -586,6 +587,20 @@ async def async_setup_entry(
icon=_TOGRID,
),
EssSensor(common_coordinator, device_info, "PCS", "operation_mode"),
DirectionalPowerSensor(
home_coordinator,
device_info,
"batt_directional",
"is_battery_charging_",
"batconv_power",
),
DirectionalPowerSensor(
home_coordinator,
device_info,
"grid_directional",
"is_grid_selling_",
"grid_power",
),
]
)

Expand Down Expand Up @@ -623,7 +638,6 @@ def __init__(
)
self._attr_translation_key = entity
self._attr_unique_id = f"${device_info["serial_number"]}_${entity}"
# self._attr_unique_id = entity
self._attr_icon = icon
self.entity_id = f"sensor.${DOMAIN}_${entity}"

Expand Down Expand Up @@ -672,7 +686,6 @@ def __init__(
entity = group + "_" + key
self._attr_translation_key = entity
self._attr_unique_id = f"${device_info["serial_number"]}_${entity}"
# self._attr_unique_id = entity
self._attr_icon = icon
self.entity_id = f"binary_sensor.${DOMAIN}_${entity}"

Expand Down Expand Up @@ -745,5 +758,39 @@ def __init__(
self._attr_suggested_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR


class DirectionalPowerSensor(CoordinatorEntity[ESSCoordinator], SensorEntity):
"""Calculate the directional power."""

_attr_state_class = SensorStateClass.MEASUREMENT

def __init__(
self,
coordinator,
device_info: DeviceInfo,
key: str,
direction_key: str,
source_key: str,
) -> None:
"""Initialize the sensor with the common coordinator."""
super().__init__(coordinator)
self._attr_device_info = device_info
self._direction_key = direction_key
self._source_key = source_key
self._attr_translation_key = key
self._attr_unique_id = f"${device_info["serial_number"]}_${key}"
self.entity_id = f"sensor.${DOMAIN}_${key}"

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
factor = 1
if self.coordinator.data["direction"][self._direction_key] == "1":
factor = -1
self._attr_native_value = (
int(self.coordinator.data["statistics"][self._source_key]) * factor
)
self.async_write_ha_state()


def _parse_date(raw_input: str) -> date:
return datetime.strptime(raw_input, "%Y-%m-%d").date()

0 comments on commit 1b32c2a

Please sign in to comment.