diff --git a/custom_components/volvo_cars/const.py b/custom_components/volvo_cars/const.py index 2cde194..a4890a9 100644 --- a/custom_components/volvo_cars/const.py +++ b/custom_components/volvo_cars/const.py @@ -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" diff --git a/custom_components/volvo_cars/coordinator.py b/custom_components/volvo_cars/coordinator.py index 4aec49e..6bb6282 100644 --- a/custom_components/volvo_cars/coordinator.py +++ b/custom_components/volvo_cars/coordinator.py @@ -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 @@ -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) diff --git a/custom_components/volvo_cars/sensor.py b/custom_components/volvo_cars/sensor.py index 58d8b26..0aeb1e5 100644 --- a/custom_components/volvo_cars/sensor.py +++ b/custom_components/volvo_cars/sensor.py @@ -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 @@ -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", @@ -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", diff --git a/custom_components/volvo_cars/translations/en.json b/custom_components/volvo_cars/translations/en.json index c0a8182..ab7ce70 100644 --- a/custom_components/volvo_cars/translations/en.json +++ b/custom_components/volvo_cars/translations/en.json @@ -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",