From 0b2475a5b745ad60fd25b930b0287282e4ab067a Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 27 Dec 2023 21:50:01 +0000 Subject: [PATCH] Tidy up and move api_client --- custom_components/ohme/__init__.py | 2 +- .../ohme/{client/__init__.py => api_client.py} | 4 +--- custom_components/ohme/config_flow.py | 2 +- custom_components/ohme/sensor.py | 5 ++++- custom_components/ohme/utils.py | 8 ++++---- 5 files changed, 11 insertions(+), 10 deletions(-) rename custom_components/ohme/{client/__init__.py => api_client.py} (99%) diff --git a/custom_components/ohme/__init__.py b/custom_components/ohme/__init__.py index 1bedc3b..798ba8e 100644 --- a/custom_components/ohme/__init__.py +++ b/custom_components/ohme/__init__.py @@ -1,6 +1,6 @@ from homeassistant import core from .const import * -from .client import OhmeApiClient +from .api_client import OhmeApiClient from .coordinator import OhmeUpdateCoordinator, OhmeStatisticsUpdateCoordinator diff --git a/custom_components/ohme/client/__init__.py b/custom_components/ohme/api_client.py similarity index 99% rename from custom_components/ohme/client/__init__.py rename to custom_components/ohme/api_client.py index 40af91b..6f8c735 100644 --- a/custom_components/ohme/client/__init__.py +++ b/custom_components/ohme/api_client.py @@ -1,10 +1,8 @@ import aiohttp -import asyncio import logging -import json from datetime import datetime, timedelta from homeassistant.helpers.entity import DeviceInfo -from ..const import DOMAIN +from .const import DOMAIN _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/ohme/config_flow.py b/custom_components/ohme/config_flow.py index 3ef583e..7d49c30 100644 --- a/custom_components/ohme/config_flow.py +++ b/custom_components/ohme/config_flow.py @@ -1,7 +1,7 @@ import voluptuous as vol from homeassistant.config_entries import (ConfigFlow, OptionsFlow) from .const import DOMAIN -from .client import OhmeApiClient +from .api_client import OhmeApiClient class OhmeConfigFlow(ConfigFlow, domain=DOMAIN): diff --git a/custom_components/ohme/sensor.py b/custom_components/ohme/sensor.py index 3765ab0..6f52f6c 100644 --- a/custom_components/ohme/sensor.py +++ b/custom_components/ohme/sensor.py @@ -14,6 +14,7 @@ from .coordinator import OhmeUpdateCoordinator, OhmeStatisticsUpdateCoordinator from .utils import charge_graph_next_slot + async def async_setup_entry( hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry, @@ -114,6 +115,7 @@ def native_value(self): return None + class NextSlotSensor(CoordinatorEntity[OhmeStatisticsUpdateCoordinator], SensorEntity): """Sensor for next smart charge slot.""" _attr_name = "Next Smart Charge Slot" @@ -158,7 +160,8 @@ def _handle_coordinator_update(self) -> None: if self.coordinator.data is None: self._state = None else: - self._state = charge_graph_next_slot(self.coordinator.data['startTime'], self.coordinator.data['chargeGraph']['points']) + self._state = charge_graph_next_slot( + self.coordinator.data['startTime'], self.coordinator.data['chargeGraph']['points']) self._last_updated = utcnow() diff --git a/custom_components/ohme/utils.py b/custom_components/ohme/utils.py index 63d6bfa..28dd6d6 100644 --- a/custom_components/ohme/utils.py +++ b/custom_components/ohme/utils.py @@ -2,18 +2,19 @@ from datetime import datetime import pytz + def charge_graph_next_slot(charge_start, points): """Get the next charge slot from a list of graph points.""" # Get start and current timestamp in seconds charge_start = round(charge_start / 1000) now = int(time()) - + # Replace relative timestamp (seconds) with real timestamp data = [{"t": x["x"] + charge_start, "y": x["y"]} for x in points] - + # Filter to points from now onwards data = [x for x in data if x["t"] > now] - + # Give up if we have less than 3 points if len(data) < 3: return None @@ -33,4 +34,3 @@ def charge_graph_next_slot(charge_start, points): # This needs to be presented with tzinfo or Home Assistant will reject it return None if next_ts is None else datetime.utcfromtimestamp(next_ts).replace(tzinfo=pytz.utc) -