Skip to content

Commit

Permalink
Fix missing state class for reactive power sensors after #1417 (#1451)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
N3rdix and drc38 authored Jan 5, 2025
1 parent e74c918 commit 22d09ea
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion custom_components/ocpp/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
1 change: 1 addition & 0 deletions custom_components/ocpp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def state_class(self):
SensorDeviceClass.CURRENT,
SensorDeviceClass.VOLTAGE,
SensorDeviceClass.POWER,
SensorDeviceClass.REACTIVE_POWER,
SensorDeviceClass.TEMPERATURE,
SensorDeviceClass.BATTERY,
SensorDeviceClass.FREQUENCY,
Expand Down
4 changes: 3 additions & 1 deletion tests/test_charge_point_v16.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 22d09ea

Please sign in to comment.