Skip to content

Commit

Permalink
Tidy up and move api_client
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Dec 27, 2023
1 parent baa4608 commit 0b2475a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion custom_components/ohme/__init__.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
Original file line number Diff line number Diff line change
@@ -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__)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/ohme/config_flow.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
5 changes: 4 additions & 1 deletion custom_components/ohme/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down
8 changes: 4 additions & 4 deletions custom_components/ohme/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

0 comments on commit 0b2475a

Please sign in to comment.