diff --git a/homeassistant/components/renault/sensor.py b/homeassistant/components/renault/sensor.py index b2161fc9adf..4cb0d723234 100644 --- a/homeassistant/components/renault/sensor.py +++ b/homeassistant/components/renault/sensor.py @@ -20,9 +20,11 @@ from homeassistant.components.sensor import ( from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( DEVICE_CLASS_BATTERY, + DEVICE_CLASS_CURRENT, DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, DEVICE_CLASS_TEMPERATURE, + ELECTRIC_CURRENT_AMPERE, ENERGY_KILO_WATT_HOUR, LENGTH_KILOMETERS, PERCENTAGE, @@ -39,6 +41,7 @@ from .const import DEVICE_CLASS_CHARGE_STATE, DEVICE_CLASS_PLUG_STATE, DOMAIN from .renault_coordinator import T from .renault_entities import RenaultDataEntity, RenaultEntityDescription from .renault_hub import RenaultHub +from .renault_vehicle import RenaultVehicleProxy @dataclass @@ -56,6 +59,7 @@ class RenaultSensorEntityDescription( """Class describing Renault sensor entities.""" icon_lambda: Callable[[RenaultSensor[T]], str] | None = None + condition_lambda: Callable[[RenaultVehicleProxy], bool] | None = None requires_fuel: bool = False value_lambda: Callable[[RenaultSensor[T]], StateType] | None = None @@ -73,6 +77,7 @@ async def async_setup_entry( for description in SENSOR_TYPES if description.coordinator in vehicle.coordinators and (not description.requires_fuel or vehicle.details.uses_fuel()) + and (not description.condition_lambda or description.condition_lambda(vehicle)) ] async_add_entities(entities) @@ -106,9 +111,7 @@ class RenaultSensor(RenaultDataEntity[T], SensorEntity): def _get_charging_power(entity: RenaultSensor[T]) -> StateType: """Return the charging_power of this entity.""" - if entity.vehicle.details.reports_charging_power_in_watts(): - return cast(float, entity.data) / 1000 - return entity.data + return cast(float, entity.data) / 1000 def _get_charge_state_formatted(entity: RenaultSensor[T]) -> str | None: @@ -177,6 +180,18 @@ SENSOR_TYPES: tuple[RenaultSensorEntityDescription, ...] = ( ), RenaultSensorEntityDescription( key="charging_power", + condition_lambda=lambda a: not a.details.reports_charging_power_in_watts(), + coordinator="battery", + data_key="chargingInstantaneousPower", + device_class=DEVICE_CLASS_CURRENT, + entity_class=RenaultSensor[KamereonVehicleBatteryStatusData], + name="Charging Power", + native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, + state_class=STATE_CLASS_MEASUREMENT, + ), + RenaultSensorEntityDescription( + key="charging_power", + condition_lambda=lambda a: a.details.reports_charging_power_in_watts(), coordinator="battery", data_key="chargingInstantaneousPower", device_class=DEVICE_CLASS_POWER, diff --git a/tests/components/renault/const.py b/tests/components/renault/const.py index 4ffc08587e3..f9fb765dab3 100644 --- a/tests/components/renault/const.py +++ b/tests/components/renault/const.py @@ -29,9 +29,11 @@ from homeassistant.const import ( CONF_PASSWORD, CONF_USERNAME, DEVICE_CLASS_BATTERY, + DEVICE_CLASS_CURRENT, DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, DEVICE_CLASS_TEMPERATURE, + ELECTRIC_CURRENT_AMPERE, ENERGY_KILO_WATT_HOUR, LENGTH_KILOMETERS, PERCENTAGE, @@ -313,10 +315,10 @@ MOCK_VEHICLES = { "entity_id": "sensor.charging_power", "unique_id": "vf1aaaaa555777999_charging_power", "result": STATE_UNKNOWN, - ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + ATTR_DEVICE_CLASS: DEVICE_CLASS_CURRENT, ATTR_LAST_UPDATE: "2020-11-17T08:06:48+00:00", ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, - ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT, + ATTR_UNIT_OF_MEASUREMENT: ELECTRIC_CURRENT_AMPERE, }, { "entity_id": "sensor.charging_remaining_time", @@ -450,10 +452,10 @@ MOCK_VEHICLES = { "entity_id": "sensor.charging_power", "unique_id": "vf1aaaaa555777123_charging_power", "result": "27.0", - ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + ATTR_DEVICE_CLASS: DEVICE_CLASS_CURRENT, ATTR_LAST_UPDATE: "2020-01-12T21:40:16+00:00", ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, - ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT, + ATTR_UNIT_OF_MEASUREMENT: ELECTRIC_CURRENT_AMPERE, }, { "entity_id": "sensor.charging_remaining_time",