Fix group sensor uom's in not convertable device classes (#109580)

This commit is contained in:
G Johansson 2024-02-04 08:56:23 -05:00 committed by GitHub
parent 17f1aa644b
commit baa511b808
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 121 additions and 2 deletions

View file

@ -1,4 +1,5 @@
"""The tests for the Group Sensor platform."""
from __future__ import annotations
from math import prod
@ -27,6 +28,7 @@ from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
PERCENTAGE,
SERVICE_RELOAD,
STATE_UNAVAILABLE,
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"
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:
"""Test the last sensor."""
config = {