Use reactive power device class for iotawatt VAR sensors (#180913)

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Stefan Agner
2026-08-31 15:44:15 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 448e78613e
commit 18a6ac2627
4 changed files with 37 additions and 5 deletions
@@ -5,7 +5,6 @@ import json
import httpx
DOMAIN = "iotawatt"
VOLT_AMPERE_REACTIVE = "VAR"
VOLT_AMPERE_REACTIVE_HOURS = "VARh"
CONNECTION_ERRORS = (KeyError, json.JSONDecodeError, httpx.HTTPError)
+4 -3
View File
@@ -21,6 +21,7 @@ from homeassistant.const import (
UnitOfEnergy,
UnitOfFrequency,
UnitOfPower,
UnitOfReactivePower,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr, entity_registry as er
@@ -29,7 +30,7 @@ from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt as dt_util
from .const import VOLT_AMPERE_REACTIVE, VOLT_AMPERE_REACTIVE_HOURS
from .const import VOLT_AMPERE_REACTIVE_HOURS
from .coordinator import IotawattConfigEntry, IotawattUpdater
_LOGGER = logging.getLogger(__name__)
@@ -87,9 +88,9 @@ ENTITY_DESCRIPTION_KEY_MAP: dict[str, IotaWattSensorEntityDescription] = {
),
"VAR": IotaWattSensorEntityDescription(
key="VAR",
native_unit_of_measurement=VOLT_AMPERE_REACTIVE,
native_unit_of_measurement=UnitOfReactivePower.VOLT_AMPERE_REACTIVE,
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:flash",
device_class=SensorDeviceClass.REACTIVE_POWER,
entity_registry_enabled_default=False,
),
"VARh": IotaWattSensorEntityDescription(
+10
View File
@@ -23,3 +23,13 @@ OUTPUT_SENSOR = Sensor(
mac_addr="mock-mac",
fromStart=True,
)
VAR_OUTPUT_SENSOR = Sensor(
channel="N/A",
base_name="My VAR Sensor",
suffix=None,
io_type="Output",
unit="VAR",
value=500,
begin="",
mac_addr="mock-mac",
)
+23 -1
View File
@@ -18,12 +18,13 @@ from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
UnitOfEnergy,
UnitOfPower,
UnitOfReactivePower,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.setup import async_setup_component
from . import INPUT_SENSOR, OUTPUT_SENSOR
from . import INPUT_SENSOR, OUTPUT_SENSOR, VAR_OUTPUT_SENSOR
from tests.common import MockConfigEntry, async_fire_time_changed
@@ -122,3 +123,24 @@ async def test_output_sensor_not_attached_to_device(
assert entity_registry.async_get("sensor.my_watthour_sensor") is None
assert "attempts to attach a device to an entity" not in caplog.text
async def test_sensor_type_output_reactive_power(
hass: HomeAssistant, mock_iotawatt: MagicMock
) -> None:
"""Test reactive power output sensors work."""
mock_iotawatt.getSensors.return_value["sensors"]["my_var_sensor_key"] = (
VAR_OUTPUT_SENSOR
)
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
state = hass.states.get("sensor.my_var_sensor")
assert state is not None
assert state.state == "500"
assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.MEASUREMENT
assert (
state.attributes[ATTR_UNIT_OF_MEASUREMENT]
== UnitOfReactivePower.VOLT_AMPERE_REACTIVE
)
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.REACTIVE_POWER