From 22d09ea1da35a2c06e3769dd15927b5456428f8c Mon Sep 17 00:00:00 2001 From: Nerdix <70015952+N3rdix@users.noreply.github.com> Date: Sun, 5 Jan 2025 10:36:08 +0100 Subject: [PATCH] Fix missing state class for reactive power sensors after #1417 (#1451) * fix state_class * add test for reactive sensor * Revert test_init.py * Add HA unit * Add OCPP to HA unit mapping * add assert vor reactive unit * add separate sensor test --------- Co-authored-by: drc38 <20024196+drc38@users.noreply.github.com> --- custom_components/ocpp/const.py | 3 ++- custom_components/ocpp/sensor.py | 1 + tests/test_charge_point_v16.py | 4 +++- tests/test_sensor.py | 35 ++++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/test_sensor.py diff --git a/custom_components/ocpp/const.py b/custom_components/ocpp/const.py index 3a924868..6bf52c49 100644 --- a/custom_components/ocpp/const.py +++ b/custom_components/ocpp/const.py @@ -113,7 +113,7 @@ UnitOfMeasure.kw: ha.UnitOfPower.KILO_WATT, UnitOfMeasure.va: ha.UnitOfApparentPower.VOLT_AMPERE, UnitOfMeasure.kva: UnitOfMeasure.kva, - UnitOfMeasure.var: UnitOfMeasure.var, + UnitOfMeasure.var: ha.UnitOfReactivePower.VOLT_AMPERE_REACTIVE, UnitOfMeasure.kvar: UnitOfMeasure.kvar, UnitOfMeasure.a: ha.UnitOfElectricCurrent.AMPERE, UnitOfMeasure.v: ha.UnitOfElectricPotential.VOLT, @@ -130,5 +130,6 @@ SensorDeviceClass.FREQUENCY: ha.UnitOfFrequency.HERTZ, SensorDeviceClass.BATTERY: ha.PERCENTAGE, SensorDeviceClass.POWER: ha.UnitOfPower.KILO_WATT, + SensorDeviceClass.REACTIVE_POWER: ha.UnitOfReactivePower.VOLT_AMPERE_REACTIVE, SensorDeviceClass.ENERGY: ha.UnitOfEnergy.KILO_WATT_HOUR, } diff --git a/custom_components/ocpp/sensor.py b/custom_components/ocpp/sensor.py index d577ee09..f4299a58 100644 --- a/custom_components/ocpp/sensor.py +++ b/custom_components/ocpp/sensor.py @@ -137,6 +137,7 @@ def state_class(self): SensorDeviceClass.CURRENT, SensorDeviceClass.VOLTAGE, SensorDeviceClass.POWER, + SensorDeviceClass.REACTIVE_POWER, SensorDeviceClass.TEMPERATURE, SensorDeviceClass.BATTERY, SensorDeviceClass.FREQUENCY, diff --git a/tests/test_charge_point_v16.py b/tests/test_charge_point_v16.py index 3e63c39e..d511a525 100644 --- a/tests/test_charge_point_v16.py +++ b/tests/test_charge_point_v16.py @@ -268,6 +268,8 @@ async def test_services(hass, cs, socket_enabled): assert int(cs.get_metric("test_cpid", "Current.Import")) == 0 assert int(cs.get_metric("test_cpid", "Voltage")) == 228 assert cs.get_unit("test_cpid", "Energy.Active.Import.Register") == "kWh" + assert cs.get_ha_unit("test_cpid", "Power.Reactive.Import") == "var" + assert cs.get_unit("test_cpid", "Power.Reactive.Import") == "var" assert cs.get_metric("unknown_cpid", "Energy.Active.Import.Register") is None assert cs.get_unit("unknown_cpid", "Energy.Active.Import.Register") is None assert cs.get_extra_attr("unknown_cpid", "Energy.Active.Import.Register") is None @@ -821,7 +823,7 @@ async def send_meter_periodic_data(self): "value": "89.00", "context": "Sample.Periodic", "measurand": "Power.Reactive.Import", - "unit": "W", + "unit": "var", }, { "value": "0.010", diff --git a/tests/test_sensor.py b/tests/test_sensor.py new file mode 100644 index 00000000..ec921e7f --- /dev/null +++ b/tests/test_sensor.py @@ -0,0 +1,35 @@ +"""Test sensor for ocpp integration.""" + +from pytest_homeassistant_custom_component.common import MockConfigEntry + +from custom_components.ocpp.const import DOMAIN as OCPP_DOMAIN + +from homeassistant.const import ATTR_DEVICE_CLASS +from homeassistant.components.sensor.const import ( + SensorDeviceClass, + SensorStateClass, + ATTR_STATE_CLASS, +) +from .const import MOCK_CONFIG_DATA + + +async def test_sensor(hass, socket_enabled): + """Test sensor.""" + config_entry = MockConfigEntry( + domain=OCPP_DOMAIN, + data=MOCK_CONFIG_DATA, + entry_id="test_cms", + title="test_cms", + ) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + # Test reactive power sensor + state = hass.states.get("sensor.test_cpid_power_reactive_import") + assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.REACTIVE_POWER + assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT + # Test reactive energx sensor, not having own device class yet + state = hass.states.get("sensor.test_cpid_energy_reactive_import_register") + assert state.attributes.get(ATTR_DEVICE_CLASS) is None + assert state.attributes.get(ATTR_STATE_CLASS) is None