Skip to content

Commit

Permalink
Add battery SOC sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Feb 2, 2024
1 parent 92d77df commit b35ce7c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ This integration exposes the following entities:
* Sensors (Other)
* CT Reading (Amps) - Reading from attached CT clamp
* Accumulative Energy Usage (kWh) - Total energy used by the charger
* Battery State of Charge (%) - If your car is API connected this is read from the car, if not it is how much charge Ohme thinks it has added
* Switches (Settings) - **Only options available to your charger model will show**
* Lock Buttons - Locks buttons on charger
* Require Approval - Require approval to start a charge
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ohme/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Component constants"""
DOMAIN = "ohme"
USER_AGENT = "dan-r-homeassistant-ohme"
INTEGRATION_VERSION = "0.5.1"
INTEGRATION_VERSION = "0.5.2"
CONFIG_VERSION = 1
ENTITY_TYPES = ["sensor", "binary_sensor", "switch", "button", "number", "time"]

Expand Down
60 changes: 58 additions & 2 deletions custom_components/ohme/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
)
import json
import hashlib
import math
import logging
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.const import UnitOfPower, UnitOfEnergy, UnitOfElectricCurrent, UnitOfElectricPotential
from homeassistant.const import UnitOfPower, UnitOfEnergy, UnitOfElectricCurrent, UnitOfElectricPotential, PERCENTAGE
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.util.dt import (utcnow)
Expand Down Expand Up @@ -40,7 +41,8 @@ async def async_setup_entry(
EnergyUsageSensor(stats_coordinator, hass, client),
NextSlotEndSensor(coordinator, hass, client),
NextSlotStartSensor(coordinator, hass, client),
SlotListSensor(coordinator, hass, client)]
SlotListSensor(coordinator, hass, client),
BatterySOCSensor(coordinator, hass, client)]

async_add_entities(sensors, update_before_add=True)

Expand Down Expand Up @@ -433,3 +435,57 @@ def _handle_coordinator_update(self) -> None:

self._last_updated = utcnow()
self.async_write_ha_state()


class BatterySOCSensor(CoordinatorEntity[OhmeChargeSessionsCoordinator], SensorEntity):
"""Sensor for car battery SOC."""
_attr_name = "Battery SOC"
_attr_native_unit_of_measurement = PERCENTAGE
_attr_device_class = SensorDeviceClass.BATTERY
_attr_suggested_display_precision = 0

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_battery_soc", 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("battery_soc")

@property
def icon(self):
"""Icon of the sensor. Round up to the nearest 10% icon."""
nearest = math.ceil((self._state or 0) / 10.0) * 10
if nearest == 0:
return "mdi:battery-outline"
elif nearest == 100:
return "mdi:battery"
else:
return "mdi:battery-" + str(nearest)

@callback
def _handle_coordinator_update(self) -> None:
"""Get value from data returned from API by coordinator"""
if self.coordinator.data and self.coordinator.data['car'] and self.coordinator.data['car']['batterySoc']:
self._state = self.coordinator.data['car']['batterySoc']['percent']

self._last_updated = utcnow()
self.async_write_ha_state()

@property
def native_value(self):
return self._state

0 comments on commit b35ce7c

Please sign in to comment.