Fix group sensor uom's in not convertable device classes (#109580)
This commit is contained in:
parent
17f1aa644b
commit
baa511b808
2 changed files with 121 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
||||||
"""Platform allowing several sensors to be grouped into one sensor to provide numeric combinations."""
|
"""Platform allowing several sensors to be grouped into one sensor to provide numeric combinations."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
@ -13,6 +14,7 @@ from homeassistant.components.input_number import DOMAIN as INPUT_NUMBER_DOMAIN
|
||||||
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN
|
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
CONF_STATE_CLASS,
|
CONF_STATE_CLASS,
|
||||||
|
DEVICE_CLASS_UNITS,
|
||||||
DEVICE_CLASSES_SCHEMA,
|
DEVICE_CLASSES_SCHEMA,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
||||||
|
@ -313,6 +315,7 @@ class SensorGroup(GroupEntity, SensorEntity):
|
||||||
self._device_class = device_class
|
self._device_class = device_class
|
||||||
self._native_unit_of_measurement = unit_of_measurement
|
self._native_unit_of_measurement = unit_of_measurement
|
||||||
self._valid_units: set[str | None] = set()
|
self._valid_units: set[str | None] = set()
|
||||||
|
self._can_convert: bool = False
|
||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
if name == DEFAULT_NAME:
|
if name == DEFAULT_NAME:
|
||||||
self._attr_name = f"{DEFAULT_NAME} {sensor_type}".capitalize()
|
self._attr_name = f"{DEFAULT_NAME} {sensor_type}".capitalize()
|
||||||
|
@ -352,10 +355,18 @@ class SensorGroup(GroupEntity, SensorEntity):
|
||||||
self._valid_units
|
self._valid_units
|
||||||
and (uom := state.attributes["unit_of_measurement"])
|
and (uom := state.attributes["unit_of_measurement"])
|
||||||
in self._valid_units
|
in self._valid_units
|
||||||
|
and self._can_convert is True
|
||||||
):
|
):
|
||||||
numeric_state = UNIT_CONVERTERS[self.device_class].convert(
|
numeric_state = UNIT_CONVERTERS[self.device_class].convert(
|
||||||
numeric_state, uom, self.native_unit_of_measurement
|
numeric_state, uom, self.native_unit_of_measurement
|
||||||
)
|
)
|
||||||
|
if (
|
||||||
|
self._valid_units
|
||||||
|
and (uom := state.attributes["unit_of_measurement"])
|
||||||
|
not in self._valid_units
|
||||||
|
):
|
||||||
|
raise HomeAssistantError("Not a valid unit")
|
||||||
|
|
||||||
sensor_values.append((entity_id, numeric_state, state))
|
sensor_values.append((entity_id, numeric_state, state))
|
||||||
if entity_id in self._state_incorrect:
|
if entity_id in self._state_incorrect:
|
||||||
self._state_incorrect.remove(entity_id)
|
self._state_incorrect.remove(entity_id)
|
||||||
|
@ -536,8 +547,21 @@ class SensorGroup(GroupEntity, SensorEntity):
|
||||||
unit_of_measurements.append(_unit_of_measurement)
|
unit_of_measurements.append(_unit_of_measurement)
|
||||||
|
|
||||||
# Ensure only valid unit of measurements for the specific device class can be used
|
# Ensure only valid unit of measurements for the specific device class can be used
|
||||||
if (device_class := self.device_class) in UNIT_CONVERTERS and all(
|
if (
|
||||||
x in UNIT_CONVERTERS[device_class].VALID_UNITS for x in unit_of_measurements
|
# Test if uom's in device class is convertible
|
||||||
|
(device_class := self.device_class) in UNIT_CONVERTERS
|
||||||
|
and all(
|
||||||
|
uom in UNIT_CONVERTERS[device_class].VALID_UNITS
|
||||||
|
for uom in unit_of_measurements
|
||||||
|
)
|
||||||
|
) or (
|
||||||
|
# Test if uom's in device class is not convertible
|
||||||
|
device_class
|
||||||
|
and device_class not in UNIT_CONVERTERS
|
||||||
|
and device_class in DEVICE_CLASS_UNITS
|
||||||
|
and all(
|
||||||
|
uom in DEVICE_CLASS_UNITS[device_class] for uom in unit_of_measurements
|
||||||
|
)
|
||||||
):
|
):
|
||||||
async_delete_issue(
|
async_delete_issue(
|
||||||
self.hass, DOMAIN, f"{self.entity_id}_uoms_not_matching_device_class"
|
self.hass, DOMAIN, f"{self.entity_id}_uoms_not_matching_device_class"
|
||||||
|
@ -546,6 +570,7 @@ class SensorGroup(GroupEntity, SensorEntity):
|
||||||
self.hass, DOMAIN, f"{self.entity_id}_uoms_not_matching_no_device_class"
|
self.hass, DOMAIN, f"{self.entity_id}_uoms_not_matching_no_device_class"
|
||||||
)
|
)
|
||||||
return unit_of_measurements[0]
|
return unit_of_measurements[0]
|
||||||
|
|
||||||
if device_class:
|
if device_class:
|
||||||
async_create_issue(
|
async_create_issue(
|
||||||
self.hass,
|
self.hass,
|
||||||
|
@ -587,5 +612,13 @@ class SensorGroup(GroupEntity, SensorEntity):
|
||||||
if (
|
if (
|
||||||
device_class := self.device_class
|
device_class := self.device_class
|
||||||
) in UNIT_CONVERTERS and self.native_unit_of_measurement:
|
) in UNIT_CONVERTERS and self.native_unit_of_measurement:
|
||||||
|
self._can_convert = True
|
||||||
return UNIT_CONVERTERS[device_class].VALID_UNITS
|
return UNIT_CONVERTERS[device_class].VALID_UNITS
|
||||||
|
if (
|
||||||
|
device_class
|
||||||
|
and (device_class) in DEVICE_CLASS_UNITS
|
||||||
|
and self.native_unit_of_measurement
|
||||||
|
):
|
||||||
|
valid_uoms: set = DEVICE_CLASS_UNITS[device_class]
|
||||||
|
return valid_uoms
|
||||||
return set()
|
return set()
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
"""The tests for the Group Sensor platform."""
|
"""The tests for the Group Sensor platform."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from math import prod
|
from math import prod
|
||||||
|
@ -27,6 +28,7 @@ from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
ATTR_ICON,
|
ATTR_ICON,
|
||||||
ATTR_UNIT_OF_MEASUREMENT,
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
|
PERCENTAGE,
|
||||||
SERVICE_RELOAD,
|
SERVICE_RELOAD,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
|
@ -557,6 +559,90 @@ async def test_sensor_calculated_result_fails_on_uom(hass: HomeAssistant) -> Non
|
||||||
assert state.attributes.get("unit_of_measurement") == "kWh"
|
assert state.attributes.get("unit_of_measurement") == "kWh"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_sensor_calculated_properties_not_convertible_device_class(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
"""Test the sensor calculating device_class, state_class and unit of measurement when device class not convertible."""
|
||||||
|
config = {
|
||||||
|
SENSOR_DOMAIN: {
|
||||||
|
"platform": GROUP_DOMAIN,
|
||||||
|
"name": "test_sum",
|
||||||
|
"type": "sum",
|
||||||
|
"entities": ["sensor.test_1", "sensor.test_2", "sensor.test_3"],
|
||||||
|
"unique_id": "very_unique_id_sum_sensor",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
entity_ids = config["sensor"]["entities"]
|
||||||
|
|
||||||
|
hass.states.async_set(
|
||||||
|
entity_ids[0],
|
||||||
|
VALUES[0],
|
||||||
|
{
|
||||||
|
"device_class": SensorDeviceClass.HUMIDITY,
|
||||||
|
"state_class": SensorStateClass.MEASUREMENT,
|
||||||
|
"unit_of_measurement": PERCENTAGE,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
hass.states.async_set(
|
||||||
|
entity_ids[1],
|
||||||
|
VALUES[1],
|
||||||
|
{
|
||||||
|
"device_class": SensorDeviceClass.HUMIDITY,
|
||||||
|
"state_class": SensorStateClass.MEASUREMENT,
|
||||||
|
"unit_of_measurement": PERCENTAGE,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
hass.states.async_set(
|
||||||
|
entity_ids[2],
|
||||||
|
VALUES[2],
|
||||||
|
{
|
||||||
|
"device_class": SensorDeviceClass.HUMIDITY,
|
||||||
|
"state_class": SensorStateClass.MEASUREMENT,
|
||||||
|
"unit_of_measurement": PERCENTAGE,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get("sensor.test_sum")
|
||||||
|
assert state.state == str(sum(VALUES))
|
||||||
|
assert state.attributes.get("device_class") == "humidity"
|
||||||
|
assert state.attributes.get("state_class") == "measurement"
|
||||||
|
assert state.attributes.get("unit_of_measurement") == "%"
|
||||||
|
|
||||||
|
assert (
|
||||||
|
"Unable to use state. Only entities with correct unit of measurement is"
|
||||||
|
" supported when having a device class"
|
||||||
|
) not in caplog.text
|
||||||
|
|
||||||
|
hass.states.async_set(
|
||||||
|
entity_ids[2],
|
||||||
|
VALUES[2],
|
||||||
|
{
|
||||||
|
"device_class": SensorDeviceClass.HUMIDITY,
|
||||||
|
"state_class": SensorStateClass.MEASUREMENT,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get("sensor.test_sum")
|
||||||
|
assert state.state == STATE_UNKNOWN
|
||||||
|
assert state.attributes.get("device_class") == "humidity"
|
||||||
|
assert state.attributes.get("state_class") == "measurement"
|
||||||
|
assert state.attributes.get("unit_of_measurement") == "%"
|
||||||
|
|
||||||
|
assert (
|
||||||
|
"Unable to use state. Only entities with correct unit of measurement is"
|
||||||
|
" supported when having a device class, entity sensor.test_3, value 15.3 with"
|
||||||
|
" device class humidity and unit of measurement None excluded from calculation"
|
||||||
|
" in sensor.test_sum"
|
||||||
|
) in caplog.text
|
||||||
|
|
||||||
|
|
||||||
async def test_last_sensor(hass: HomeAssistant) -> None:
|
async def test_last_sensor(hass: HomeAssistant) -> None:
|
||||||
"""Test the last sensor."""
|
"""Test the last sensor."""
|
||||||
config = {
|
config = {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue