Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 5c4d901

Browse files
committed
Revert accumulative usage sensor
1 parent 8f9b6bb commit 5c4d901

File tree

1 file changed

+9
-65
lines changed

1 file changed

+9
-65
lines changed

custom_components/ohme/sensor.py

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def async_setup_entry(
3939
VoltageSensor(coordinator, hass, client),
4040
CTSensor(adv_coordinator, hass, client),
4141
EnergyUsageSensor(coordinator, hass, client),
42-
AccumulativeEnergyUsageSensor(coordinator, stats_coordinator, hass, client),
42+
AccumulativeEnergyUsageSensor(stats_coordinator, hass, client),
4343
NextSlotEndSensor(coordinator, hass, client),
4444
NextSlotStartSensor(coordinator, hass, client),
4545
SlotListSensor(coordinator, hass, client),
@@ -214,7 +214,7 @@ def native_value(self):
214214
return self.coordinator.data['clampAmps']
215215

216216

217-
class AccumulativeEnergyUsageSensor(SensorEntity):
217+
class AccumulativeEnergyUsageSensor(CoordinatorEntity[OhmeStatisticsCoordinator], SensorEntity):
218218
"""Sensor for total energy usage."""
219219
_attr_name = "Accumulative Energy Usage"
220220
_attr_native_unit_of_measurement = UnitOfEnergy.WATT_HOUR
@@ -225,16 +225,12 @@ class AccumulativeEnergyUsageSensor(SensorEntity):
225225

226226
def __init__(
227227
self,
228-
coordinator_sessions,
229-
coordinator_statistics,
228+
coordinator: OhmeStatisticsCoordinator,
230229
hass: HomeAssistant,
231230
client):
232-
self.coordinator_statistics = coordinator_statistics
233-
self.coordinator_sessions = coordinator_sessions
231+
super().__init__(coordinator=coordinator)
234232

235233
self._state = None
236-
self._stats_state = None
237-
238234
self._attributes = {}
239235
self._last_updated = None
240236
self._client = client
@@ -245,62 +241,6 @@ def __init__(
245241
self._attr_device_info = hass.data[DOMAIN][DATA_CLIENT].get_device_info(
246242
)
247243

248-
async def async_added_to_hass(self) -> None:
249-
"""When entity is added to hass."""
250-
# Handle the coordinator listeners manually as we have 2
251-
self.async_on_remove(
252-
self.coordinator_sessions.async_add_listener(
253-
self._handle_sessions_update, None
254-
)
255-
)
256-
self.async_on_remove(
257-
self.coordinator_statistics.async_add_listener(
258-
self._handle_statistics_update, None
259-
)
260-
)
261-
262-
# Stats only run every hour so make sure they run at init
263-
self._handle_sessions_update()
264-
self._handle_statistics_update()
265-
266-
@callback
267-
def _handle_statistics_update(self) -> None:
268-
"""Handle updated statistics (all time) data."""
269-
# Guard
270-
if self.coordinator_statistics.data is None or self.coordinator_sessions.data is None or not self.coordinator_statistics.data['energyChargedTotalWh']:
271-
_LOGGER.debug(f"Stats: not enough data")
272-
return
273-
274-
# Store the stats state
275-
self._stats_state = self.coordinator_statistics.data['energyChargedTotalWh']
276-
277-
# If session not in progress, use the statistics data alone
278-
if self.coordinator_sessions.data["mode"] == "DISCONNECTED":
279-
_LOGGER.debug(f"Stats: using stats data only")
280-
self._state = self._stats_state
281-
self._last_updated = utcnow()
282-
self.async_write_ha_state()
283-
284-
@callback
285-
def _handle_sessions_update(self) -> None:
286-
"""Handle updated charge sessions (live data)."""
287-
# Guard
288-
if self.coordinator_sessions.data is None or self.coordinator_statistics.data is None or not self.coordinator_sessions.data['batterySoc']:
289-
_LOGGER.debug(f"Sessions: not enough data")
290-
return
291-
292-
# If session in progress, use statistics + charge data
293-
if self._stats_state and not self.coordinator_sessions.data["mode"] == "DISCONNECTED":
294-
_LOGGER.debug(f"Sessions: using stats + session")
295-
# Calculate new state as stats total + session total
296-
new_state = self._stats_state + max(0, self.coordinator_sessions.data['batterySoc']['wh'])
297-
298-
# This tends to go backwards? Make sure it only goes up
299-
self._state = max(self._state or 0, new_state)
300-
301-
self._last_updated = utcnow()
302-
self.async_write_ha_state()
303-
304244
@property
305245
def unique_id(self) -> str:
306246
"""Return the unique ID of the sensor."""
@@ -313,7 +253,11 @@ def icon(self):
313253

314254
@property
315255
def native_value(self):
316-
return self._state
256+
"""Get value from data returned from API by coordinator"""
257+
if self.coordinator.data and self.coordinator.data['energyChargedTotalWh']:
258+
return self.coordinator.data['energyChargedTotalWh']
259+
260+
return None
317261

318262

319263
class EnergyUsageSensor(CoordinatorEntity[OhmeChargeSessionsCoordinator], SensorEntity):

0 commit comments

Comments
 (0)