Skip to content

Commit

Permalink
Refactor coordinators
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Dec 29, 2023
1 parent 7147ac0 commit 56747a9
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 26 deletions.
17 changes: 8 additions & 9 deletions custom_components/ohme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ async def async_setup_entry(hass, entry):

await async_setup_dependencies(hass, config)

hass.data[DOMAIN][DATA_CHARGESESSIONS_COORDINATOR] = OhmeChargeSessionsCoordinator(
hass=hass)
await hass.data[DOMAIN][DATA_CHARGESESSIONS_COORDINATOR].async_config_entry_first_refresh()
coordinators = [
OhmeChargeSessionsCoordinator(hass=hass), # COORDINATOR_CHARGESESSIONS
OhmeAccountInfoCoordinator(hass=hass), # COORDINATOR_ACCOUNTINFO
OhmeStatisticsCoordinator(hass=hass) # COORDINATOR_STATISTICS
]

hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR] = OhmeStatisticsCoordinator(
hass=hass)
await hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR].async_config_entry_first_refresh()
for coordinator in coordinators:
await coordinator.async_config_entry_first_refresh()

hass.data[DOMAIN][DATA_ACCOUNTINFO_COORDINATOR] = OhmeAccountInfoCoordinator(
hass=hass)
await hass.data[DOMAIN][DATA_ACCOUNTINFO_COORDINATOR].async_config_entry_first_refresh()
hass.data[DOMAIN][DATA_COORDINATORS] = coordinators

# Create tasks for each entity type
hass.async_create_task(
Expand Down
6 changes: 6 additions & 0 deletions custom_components/ohme/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ async def async_get_charge_statistics(self):

return resp['totalStats']

async def async_get_ct_reading(self):
"""Get CT clamp reading."""
resp = await self._get_request(f"/v1/chargeDevices/{self._serial}/advancedSettings")

return resp['clampAmps']



# Exceptions
Expand Down
4 changes: 2 additions & 2 deletions custom_components/ohme/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import generate_entity_id
from .const import DOMAIN, DATA_CHARGESESSIONS_COORDINATOR, DATA_CLIENT
from .const import DOMAIN, DATA_COORDINATORS, COORDINATOR_CHARGESESSIONS, DATA_CLIENT
from .coordinator import OhmeChargeSessionsCoordinator


Expand All @@ -19,7 +19,7 @@ async def async_setup_entry(
):
"""Setup sensors and configure coordinator."""
client = hass.data[DOMAIN][DATA_CLIENT]
coordinator = hass.data[DOMAIN][DATA_CHARGESESSIONS_COORDINATOR]
coordinator = hass.data[DOMAIN][DATA_COORDINATORS][COORDINATOR_CHARGESESSIONS]

sensors = [ConnectedSensor(coordinator, hass, client),
ChargingSensor(coordinator, hass, client),
Expand Down
4 changes: 2 additions & 2 deletions custom_components/ohme/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.components.button import ButtonEntity

from .const import DOMAIN, DATA_CLIENT, DATA_CHARGESESSIONS_COORDINATOR
from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATORS, COORDINATOR_CHARGESESSIONS
from .coordinator import OhmeChargeSessionsCoordinator

_LOGGER = logging.getLogger(__name__)
Expand All @@ -19,7 +19,7 @@ async def async_setup_entry(
):
"""Setup switches."""
client = hass.data[DOMAIN][DATA_CLIENT]
coordinator = hass.data[DOMAIN][DATA_CHARGESESSIONS_COORDINATOR]
coordinator = hass.data[DOMAIN][DATA_COORDINATORS][COORDINATOR_CHARGESESSIONS]

buttons = []

Expand Down
7 changes: 4 additions & 3 deletions custom_components/ohme/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

DOMAIN = "ohme"
DATA_CLIENT = "client"
DATA_CHARGESESSIONS_COORDINATOR = "coordinator"
DATA_STATISTICS_COORDINATOR = "statistics_coordinator"
DATA_ACCOUNTINFO_COORDINATOR = "accountinfo_coordinator"
DATA_COORDINATORS = "coordinators"
COORDINATOR_CHARGESESSIONS = 0
COORDINATOR_ACCOUNTINFO = 1
COORDINATOR_STATISTICS = 2
29 changes: 25 additions & 4 deletions custom_components/ohme/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@


class OhmeChargeSessionsCoordinator(DataUpdateCoordinator):
"""Coordinator to pull from API periodically."""
"""Coordinator to pull main charge state and power/current draw."""

def __init__(self, hass):
"""Initialise coordinator."""
super().__init__(
hass,
_LOGGER,
name="Ohme Charger",
name="Ohme Charge Sessions",
update_interval=timedelta(seconds=30),
)
self._client = hass.data[DOMAIN][DATA_CLIENT]
Expand All @@ -34,7 +34,7 @@ async def _async_update_data(self):


class OhmeAccountInfoCoordinator(DataUpdateCoordinator):
"""Coordinator to pull from API periodically."""
"""Coordinator to pull charger settings."""

def __init__(self, hass):
"""Initialise coordinator."""
Expand All @@ -57,7 +57,7 @@ async def _async_update_data(self):

class OhmeStatisticsCoordinator(DataUpdateCoordinator):
"""Coordinator to update statistics from API periodically.
(But less so than OhmeUpdateCoordinator)"""
(But less so than the others)"""

def __init__(self, hass):
"""Initialise coordinator."""
Expand All @@ -76,3 +76,24 @@ async def _async_update_data(self):

except BaseException:
raise UpdateFailed("Error communicating with API")

class OhmeAdvancedSettingsCoordinator(DataUpdateCoordinator):
"""Coordinator to pull CT clamp reading."""

def __init__(self, hass):
"""Initialise coordinator."""
super().__init__(
hass,
_LOGGER,
name="Ohme Advanced Settings",
update_interval=timedelta(minutes=1),
)
self._client = hass.data[DOMAIN][DATA_CLIENT]

async def _async_update_data(self):
"""Fetch data from API endpoint."""
try:
return await self._client.async_get_ct_reading()

except BaseException:
raise UpdateFailed("Error communicating with API")
8 changes: 5 additions & 3 deletions custom_components/ohme/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.util.dt import (utcnow)
from .const import DOMAIN, DATA_CLIENT, DATA_CHARGESESSIONS_COORDINATOR, DATA_STATISTICS_COORDINATOR
from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATORS, COORDINATOR_CHARGESESSIONS, COORDINATOR_STATISTICS
from .coordinator import OhmeChargeSessionsCoordinator, OhmeStatisticsCoordinator
from .utils import charge_graph_next_slot

Expand All @@ -22,8 +22,10 @@ async def async_setup_entry(
):
"""Setup sensors and configure coordinator."""
client = hass.data[DOMAIN][DATA_CLIENT]
coordinator = hass.data[DOMAIN][DATA_CHARGESESSIONS_COORDINATOR]
stats_coordinator = hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR]
coordinators = hass.data[DOMAIN][DATA_COORDINATORS]

coordinator = coordinators[COORDINATOR_CHARGESESSIONS]
stats_coordinator = coordinators[COORDINATOR_STATISTICS]

sensors = [PowerDrawSensor(coordinator, hass, client),
CurrentDrawSensor(coordinator, hass, client),
Expand Down
8 changes: 5 additions & 3 deletions custom_components/ohme/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from homeassistant.components.switch import SwitchEntity
from homeassistant.util.dt import (utcnow)

from .const import DOMAIN, DATA_CLIENT, DATA_CHARGESESSIONS_COORDINATOR, DATA_ACCOUNTINFO_COORDINATOR
from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATORS, COORDINATOR_CHARGESESSIONS, COORDINATOR_ACCOUNTINFO
from .coordinator import OhmeChargeSessionsCoordinator, OhmeAccountInfoCoordinator

_LOGGER = logging.getLogger(__name__)
Expand All @@ -23,8 +23,10 @@ async def async_setup_entry(
async_add_entities
):
"""Setup switches and configure coordinator."""
coordinator = hass.data[DOMAIN][DATA_CHARGESESSIONS_COORDINATOR]
accountinfo_coordinator = hass.data[DOMAIN][DATA_ACCOUNTINFO_COORDINATOR]
coordinators = hass.data[DOMAIN][DATA_COORDINATORS]

coordinator = coordinators[COORDINATOR_CHARGESESSIONS]
accountinfo_coordinator = coordinators[COORDINATOR_ACCOUNTINFO]
client = hass.data[DOMAIN][DATA_CLIENT]

switches = [OhmePauseChargeSwitch(coordinator, hass, client),
Expand Down

0 comments on commit 56747a9

Please sign in to comment.