From 18a6ac26276034dd113709ef8883f6766e6138fd Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Mon, 31 Aug 2026 15:44:15 +0200 Subject: [PATCH] Use reactive power device class for iotawatt VAR sensors (#180913) Co-authored-by: Claude Fable 5 --- homeassistant/components/iotawatt/const.py | 1 - homeassistant/components/iotawatt/sensor.py | 7 +++--- tests/components/iotawatt/__init__.py | 10 +++++++++ tests/components/iotawatt/test_sensor.py | 24 ++++++++++++++++++++- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/iotawatt/const.py b/homeassistant/components/iotawatt/const.py index 01034ae2a703..a343ed2a5afe 100644 --- a/homeassistant/components/iotawatt/const.py +++ b/homeassistant/components/iotawatt/const.py @@ -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) diff --git a/homeassistant/components/iotawatt/sensor.py b/homeassistant/components/iotawatt/sensor.py index 444e7e010ba1..4f874db65f4b 100644 --- a/homeassistant/components/iotawatt/sensor.py +++ b/homeassistant/components/iotawatt/sensor.py @@ -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( diff --git a/tests/components/iotawatt/__init__.py b/tests/components/iotawatt/__init__.py index 5233ce6fc83e..b93de348db55 100644 --- a/tests/components/iotawatt/__init__.py +++ b/tests/components/iotawatt/__init__.py @@ -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", +) diff --git a/tests/components/iotawatt/test_sensor.py b/tests/components/iotawatt/test_sensor.py index 1d07e416976b..96d864bfa82d 100644 --- a/tests/components/iotawatt/test_sensor.py +++ b/tests/components/iotawatt/test_sensor.py @@ -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