Skip to content

Commit

Permalink
Add battery capacity sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasddn committed Dec 20, 2024
1 parent cd8b168 commit 2486105
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
2 changes: 2 additions & 0 deletions custom_components/volvo_cars/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
CONF_VCC_API_KEY = "vcc_api_key"
CONF_VIN = "vin"

DATA_BATTERY_CAPACITY = "battery_capacity_kwh"

MANUFACTURER = "Volvo"

OPT_FUEL_CONSUMPTION_UNIT = "fuel_consumption_unit"
Expand Down
9 changes: 8 additions & 1 deletion custom_components/volvo_cars/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DOMAIN, MANUFACTURER
from .const import DATA_BATTERY_CAPACITY, DOMAIN, MANUFACTURER
from .entity_description import VolvoCarsDescription
from .entry_data import StoreData, VolvoCarsStore
from .volvo.api import VolvoCarsApi
Expand Down Expand Up @@ -205,6 +205,13 @@ async def _async_update_data(self) -> dict[str, VolvoCarsApiBaseModel | None]:
for result in results:
data |= cast(dict[str, VolvoCarsApiBaseModel | None], result)

data[DATA_BATTERY_CAPACITY] = VolvoCarsValueField.from_dict(
{
"value": self.vehicle.battery_capacity_kwh,
"timestamp": self.config_entry.modified_at,
}
)

except VolvoAuthException as ex:
# Raising ConfigEntryAuthFailed will cancel future updates
# and start a config flow with SOURCE_REAUTH (async_step_reauth)
Expand Down
34 changes: 24 additions & 10 deletions custom_components/volvo_cars/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import OPT_FUEL_CONSUMPTION_UNIT, OPT_UNIT_MPG_UK, OPT_UNIT_MPG_US
from .const import (
DATA_BATTERY_CAPACITY,
OPT_FUEL_CONSUMPTION_UNIT,
OPT_UNIT_MPG_UK,
OPT_UNIT_MPG_US,
)
from .coordinator import VolvoCarsConfigEntry, VolvoCarsDataCoordinator
from .entity import VolvoCarsEntity, value_to_translation_key
from .entity_description import VolvoCarsDescription
Expand Down Expand Up @@ -132,15 +137,6 @@ def _convert_fuel_consumption(
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:speedometer",
),
VolvoCarsSensorDescription(
key="battery_charge_level",
translation_key="battery_charge_level",
api_field="batteryChargeLevel",
native_unit_of_measurement="%",
device_class=SensorDeviceClass.BATTERY,
state_class=SensorStateClass.MEASUREMENT,
available_fn=lambda vehicle: vehicle.has_battery_engine(),
),
VolvoCarsSensorDescription(
key="average_speed_automatic",
translation_key="average_speed_automatic",
Expand All @@ -150,6 +146,24 @@ def _convert_fuel_consumption(
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:speedometer",
),
VolvoCarsSensorDescription(
key="battery_capacity",
translation_key="battery_capacity",
api_field=DATA_BATTERY_CAPACITY,
native_unit_of_measurement="kWh",
device_class=SensorDeviceClass.ENERGY,
available_fn=lambda vehicle: vehicle.has_battery_engine(),
icon="mdi:car-battery",
),
VolvoCarsSensorDescription(
key="battery_charge_level",
translation_key="battery_charge_level",
api_field="batteryChargeLevel",
native_unit_of_measurement="%",
device_class=SensorDeviceClass.BATTERY,
state_class=SensorStateClass.MEASUREMENT,
available_fn=lambda vehicle: vehicle.has_battery_engine(),
),
VolvoCarsSensorDescription(
key="charging_connection_status",
translation_key="charging_connection_status",
Expand Down
5 changes: 4 additions & 1 deletion custom_components/volvo_cars/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,11 @@
"average_speed_automatic": {
"name": "Avg. speed automatic"
},
"battery_capacity": {
"name": "Battery capacity"
},
"battery_charge_level": {
"name": "SoC"
"name": "Battery charge level"
},
"charging_connection_status": {
"name": "Charging connection status",
Expand Down

0 comments on commit 2486105

Please sign in to comment.