Skip to content

Commit

Permalink
Logic to collapse slots
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Aug 12, 2024
1 parent 7bd0d37 commit a74de09
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion custom_components/ohme/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Component constants"""
DOMAIN = "ohme"
USER_AGENT = "dan-r-homeassistant-ohme"
INTEGRATION_VERSION = "0.9.0"
INTEGRATION_VERSION = "0.9.1"
CONFIG_VERSION = 1
ENTITY_TYPES = ["sensor", "binary_sensor", "switch", "button", "number", "time"]

Expand Down
8 changes: 2 additions & 6 deletions custom_components/ohme/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Platform for sensor integration."""
from __future__ import annotations
from functools import reduce
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorStateClass,
Expand All @@ -15,7 +14,7 @@
from homeassistant.util.dt import (utcnow)
from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATORS, DATA_SLOTS, COORDINATOR_CHARGESESSIONS, COORDINATOR_STATISTICS, COORDINATOR_ADVANCED
from .coordinator import OhmeChargeSessionsCoordinator, OhmeStatisticsCoordinator, OhmeAdvancedSettingsCoordinator
from .utils import next_slot, get_option, slot_list
from .utils import next_slot, get_option, slot_list, slot_list_str

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -468,10 +467,7 @@ def _handle_coordinator_update(self) -> None:
self._hass.data[DOMAIN][DATA_SLOTS] = slots

# Convert list to text
self._state = reduce(lambda acc, slot: acc + f"{slot['start'].strftime('%H:%M')}-{slot['end'].strftime('%H:%M')}, ", slots, "")[:-2]

# Make sure we return None/Unknown if the list is empty
self._state = None if self._state == "" else self._state
self._state = slot_list_str(slots)

self._last_updated = utcnow()
self.async_write_ha_state()
Expand Down
22 changes: 22 additions & 0 deletions custom_components/ohme/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from time import time
from functools import reduce
from datetime import datetime, timedelta
from .const import DOMAIN, DATA_OPTIONS
import pytz
Expand Down Expand Up @@ -54,6 +55,27 @@ def slot_list(data):
return slots


def slot_list_str(slots):
# Convert list to tuples of times
t_slots = []
for slot in slots:
t_slots.append((slot['start'].strftime('%H:%M'), slot['end'].strftime('%H:%M')))

# Collapse slots so consecutive slots become one
state = []
for i in range(len(t_slots)):
if not state or state[-1][1] != t_slots[i][0]:
state.append(t_slots[i])
else:
state[-1] = (state[-1][0], t_slots[i][1])

# Convert list of tuples to string
state = reduce(lambda acc, slot: acc + f"{slot[0]}-{slot[1]}, ", state, "")[:-2]

# Make sure we return None/Unknown if the list is empty
return None if state == "" else state


def in_slot(data):
"""Are we currently in a charge slot?"""
slots = slot_list(data)
Expand Down

0 comments on commit a74de09

Please sign in to comment.