From a74de090f4ac05c332ffeda7410118b6f274b1ea Mon Sep 17 00:00:00 2001 From: Daniel Raper Date: Mon, 12 Aug 2024 14:55:28 +0100 Subject: [PATCH] Logic to collapse slots --- custom_components/ohme/const.py | 2 +- custom_components/ohme/sensor.py | 8 ++------ custom_components/ohme/utils.py | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/custom_components/ohme/const.py b/custom_components/ohme/const.py index b4d8ca8..8c76a37 100644 --- a/custom_components/ohme/const.py +++ b/custom_components/ohme/const.py @@ -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"] diff --git a/custom_components/ohme/sensor.py b/custom_components/ohme/sensor.py index 73f3d8c..28f76db 100644 --- a/custom_components/ohme/sensor.py +++ b/custom_components/ohme/sensor.py @@ -1,6 +1,5 @@ """Platform for sensor integration.""" from __future__ import annotations -from functools import reduce from homeassistant.components.sensor import ( SensorDeviceClass, SensorStateClass, @@ -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__) @@ -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() diff --git a/custom_components/ohme/utils.py b/custom_components/ohme/utils.py index baf4bd8..b5a87cc 100644 --- a/custom_components/ohme/utils.py +++ b/custom_components/ohme/utils.py @@ -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 @@ -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)