1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-02 08:26:41 +01:00

KNX: add config for unit_of_measurement for yaml sensor entities (#165082)

This commit is contained in:
Matthias Alphart
2026-03-10 19:27:59 +01:00
committed by GitHub
parent 11bc00038e
commit f3879335ab
3 changed files with 26 additions and 21 deletions

View File

@@ -873,6 +873,7 @@ class SensorSchema(KNXPlatformSchema):
vol.Required(CONF_TYPE): sensor_type_validator,
vol.Required(CONF_STATE_ADDRESS): ga_list_validator,
vol.Optional(CONF_DEVICE_CLASS): SENSOR_DEVICE_CLASSES_SCHEMA,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_ENTITY_CATEGORY): ENTITY_CATEGORIES_SCHEMA,
}
),

View File

@@ -216,20 +216,22 @@ class KnxYamlSensor(_KnxSensor, KnxYamlEntity):
dpt_string = self._device.sensor_value.dpt_class.dpt_number_str()
dpt_info = get_supported_dpts()[dpt_string]
if device_class := config.get(CONF_DEVICE_CLASS):
self._attr_device_class = device_class
else:
self._attr_device_class = dpt_info["sensor_device_class"]
self._attr_state_class = (
config.get(CONF_STATE_CLASS) or dpt_info["sensor_state_class"]
self._attr_device_class = config.get(
CONF_DEVICE_CLASS,
dpt_info["sensor_device_class"],
)
self._attr_native_unit_of_measurement = dpt_info["unit"]
self._attr_force_update = config[SensorSchema.CONF_ALWAYS_CALLBACK]
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
self._attr_unique_id = str(self._device.sensor_value.group_address_state)
self._attr_extra_state_attributes = {}
self._attr_force_update = config[SensorSchema.CONF_ALWAYS_CALLBACK]
self._attr_native_unit_of_measurement = config.get(
CONF_UNIT_OF_MEASUREMENT,
dpt_info["unit"],
)
self._attr_state_class = config.get(
CONF_STATE_CLASS,
dpt_info["sensor_state_class"],
)
self._attr_unique_id = str(self._device.sensor_value.group_address_state)
class KnxUiSensor(_KnxSensor, KnxUiEntity):

View File

@@ -12,11 +12,7 @@ from homeassistant.components.knx.const import (
CONF_SYNC_STATE,
)
from homeassistant.components.knx.schema import SensorSchema
from homeassistant.components.sensor import (
CONF_STATE_CLASS as CONF_SENSOR_STATE_CLASS,
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import CONF_NAME, CONF_TYPE, STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant, State
@@ -183,10 +179,19 @@ async def test_always_callback(hass: HomeAssistant, knx: KNXTestKit) -> None:
assert len(events) == 6
@pytest.mark.parametrize(
"attribute_config",
[
{"state_class": "total_increasing"}, # invalid for temperature DPT
{"unit_of_measurement": "invalid"},
{"device_class": "energy", "unit_of_measurement": "invalid"},
],
)
async def test_sensor_yaml_attribute_validation(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
knx: KNXTestKit,
attribute_config: dict[str, Any],
) -> None:
"""Test creating a sensor with invalid unit, state_class or device_class."""
with caplog.at_level(logging.ERROR):
@@ -196,17 +201,14 @@ async def test_sensor_yaml_attribute_validation(
CONF_NAME: "test",
CONF_STATE_ADDRESS: "1/1/1",
CONF_TYPE: "9.001", # temperature 2 byte float
CONF_SENSOR_STATE_CLASS: "total_increasing", # invalid for temperature
**attribute_config,
}
}
)
assert len(caplog.messages) == 2
record = caplog.records[0]
assert record.levelname == "ERROR"
assert (
"Invalid config for 'knx': State class 'total_increasing' is not valid for device class"
in record.message
)
assert "Invalid config for 'knx': " in record.message
record = caplog.records[1]
assert record.levelname == "ERROR"