Skip to content

Commit

Permalink
Add CT reading sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Dec 29, 2023
1 parent 56747a9 commit 2ff3a85
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This integration exposes the following entities:
* Sensors
* Power Draw (Watts) - Power draw of connected car
* Current Draw (Amps) - Current draw of connected car
* CT Reading (Amps) - Reading from attached CT clamp
* Accumulative Energy Usage (kWh) - Total energy used by the charger
* Next Smart Charge Slot - The next time your car will start charging according to the Ohme-generated charge plan
* Switches (Settings) - Only options available to your charger model will show
Expand Down
5 changes: 3 additions & 2 deletions custom_components/ohme/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from homeassistant import core
from .const import *
from .api_client import OhmeApiClient
from .coordinator import OhmeChargeSessionsCoordinator, OhmeStatisticsCoordinator, OhmeAccountInfoCoordinator
from .coordinator import OhmeChargeSessionsCoordinator, OhmeStatisticsCoordinator, OhmeAccountInfoCoordinator, OhmeAdvancedSettingsCoordinator


async def async_setup(hass: core.HomeAssistant, config: dict) -> bool:
Expand Down Expand Up @@ -34,7 +34,8 @@ async def async_setup_entry(hass, entry):
coordinators = [
OhmeChargeSessionsCoordinator(hass=hass), # COORDINATOR_CHARGESESSIONS
OhmeAccountInfoCoordinator(hass=hass), # COORDINATOR_ACCOUNTINFO
OhmeStatisticsCoordinator(hass=hass) # COORDINATOR_STATISTICS
OhmeStatisticsCoordinator(hass=hass), # COORDINATOR_STATISTICS
OhmeAdvancedSettingsCoordinator(hass=hass) # COORDINATOR_ADVANCED
]

for coordinator in coordinators:
Expand Down
3 changes: 2 additions & 1 deletion custom_components/ohme/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
DATA_COORDINATORS = "coordinators"
COORDINATOR_CHARGESESSIONS = 0
COORDINATOR_ACCOUNTINFO = 1
COORDINATOR_STATISTICS = 2
COORDINATOR_STATISTICS = 2
COORDINATOR_ADVANCED = 3
46 changes: 44 additions & 2 deletions custom_components/ohme/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
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_COORDINATORS, COORDINATOR_CHARGESESSIONS, COORDINATOR_STATISTICS
from .coordinator import OhmeChargeSessionsCoordinator, OhmeStatisticsCoordinator
from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATORS, COORDINATOR_CHARGESESSIONS, COORDINATOR_STATISTICS, COORDINATOR_ADVANCED
from .coordinator import OhmeChargeSessionsCoordinator, OhmeStatisticsCoordinator, OhmeAdvancedSettingsCoordinator
from .utils import charge_graph_next_slot


Expand All @@ -26,9 +26,11 @@ async def async_setup_entry(

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

sensors = [PowerDrawSensor(coordinator, hass, client),
CurrentDrawSensor(coordinator, hass, client),
CTSensor(adv_coordinator, hass, client),
EnergyUsageSensor(stats_coordinator, hass, client),
NextSlotSensor(coordinator, hass, client)]

Expand Down Expand Up @@ -119,6 +121,46 @@ def native_value(self):
return 0


class CTSensor(CoordinatorEntity[OhmeChargeSessionsCoordinator], SensorEntity):
"""Sensor for car power draw."""
_attr_name = "CT Reading"
_attr_device_class = SensorDeviceClass.CURRENT
_attr_native_unit_of_measurement = UnitOfElectricCurrent.AMPERE

def __init__(
self,
coordinator: OhmeChargeSessionsCoordinator,
hass: HomeAssistant,
client):
super().__init__(coordinator=coordinator)

self._state = None
self._attributes = {}
self._last_updated = None
self._client = client

self.entity_id = generate_entity_id(
"sensor.{}", "ohme_ct_reading", hass=hass)

self._attr_device_info = hass.data[DOMAIN][DATA_CLIENT].get_device_info(
)

@property
def unique_id(self) -> str:
"""Return the unique ID of the sensor."""
return self._client.get_unique_id("ct_reading")

@property
def icon(self):
"""Icon of the sensor."""
return "mdi:gauge"

@property
def native_value(self):
"""Get value from data returned from API by coordinator"""
return self.coordinator.data


class EnergyUsageSensor(CoordinatorEntity[OhmeStatisticsCoordinator], SensorEntity):
"""Sensor for total energy usage."""
_attr_name = "Accumulative Energy Usage"
Expand Down

0 comments on commit 2ff3a85

Please sign in to comment.