Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing state class for reactive power sensors after #1417 #1451

Merged
merged 8 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
N3rdix marked this conversation as resolved.
Show resolved Hide resolved
},
{
"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
Loading