Add check that sensors don't have EntityCategory.CONFIG set (#101471)
This commit is contained in:
parent
27b6325c32
commit
f7292d5b00
5 changed files with 39 additions and 8 deletions
|
@ -348,7 +348,7 @@ SENSORS: Final[list[NextcloudSensorEntityDescription]] = [
|
||||||
key="server_php_max_execution_time",
|
key="server_php_max_execution_time",
|
||||||
translation_key="nextcloud_server_php_max_execution_time",
|
translation_key="nextcloud_server_php_max_execution_time",
|
||||||
device_class=SensorDeviceClass.DURATION,
|
device_class=SensorDeviceClass.DURATION,
|
||||||
entity_category=EntityCategory.CONFIG,
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
icon="mdi:language-php",
|
icon="mdi:language-php",
|
||||||
native_unit_of_measurement=UnitOfTime.SECONDS,
|
native_unit_of_measurement=UnitOfTime.SECONDS,
|
||||||
),
|
),
|
||||||
|
@ -356,7 +356,7 @@ SENSORS: Final[list[NextcloudSensorEntityDescription]] = [
|
||||||
key="server_php_memory_limit",
|
key="server_php_memory_limit",
|
||||||
translation_key="nextcloud_server_php_memory_limit",
|
translation_key="nextcloud_server_php_memory_limit",
|
||||||
device_class=SensorDeviceClass.DATA_SIZE,
|
device_class=SensorDeviceClass.DATA_SIZE,
|
||||||
entity_category=EntityCategory.CONFIG,
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
icon="mdi:language-php",
|
icon="mdi:language-php",
|
||||||
native_unit_of_measurement=UnitOfInformation.BYTES,
|
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||||
suggested_display_precision=1,
|
suggested_display_precision=1,
|
||||||
|
@ -366,7 +366,7 @@ SENSORS: Final[list[NextcloudSensorEntityDescription]] = [
|
||||||
key="server_php_upload_max_filesize",
|
key="server_php_upload_max_filesize",
|
||||||
translation_key="nextcloud_server_php_upload_max_filesize",
|
translation_key="nextcloud_server_php_upload_max_filesize",
|
||||||
device_class=SensorDeviceClass.DATA_SIZE,
|
device_class=SensorDeviceClass.DATA_SIZE,
|
||||||
entity_category=EntityCategory.CONFIG,
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
icon="mdi:language-php",
|
icon="mdi:language-php",
|
||||||
native_unit_of_measurement=UnitOfInformation.BYTES,
|
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||||
suggested_display_precision=1,
|
suggested_display_precision=1,
|
||||||
|
|
|
@ -47,9 +47,11 @@ from homeassistant.const import ( # noqa: F401
|
||||||
DEVICE_CLASS_TIMESTAMP,
|
DEVICE_CLASS_TIMESTAMP,
|
||||||
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
|
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
|
||||||
DEVICE_CLASS_VOLTAGE,
|
DEVICE_CLASS_VOLTAGE,
|
||||||
|
EntityCategory,
|
||||||
UnitOfTemperature,
|
UnitOfTemperature,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, State, callback
|
from homeassistant.core import HomeAssistant, State, callback
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.helpers.config_validation import (
|
from homeassistant.helpers.config_validation import (
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
|
@ -251,6 +253,11 @@ class SensorEntity(Entity):
|
||||||
async def async_internal_added_to_hass(self) -> None:
|
async def async_internal_added_to_hass(self) -> None:
|
||||||
"""Call when the sensor entity is added to hass."""
|
"""Call when the sensor entity is added to hass."""
|
||||||
await super().async_internal_added_to_hass()
|
await super().async_internal_added_to_hass()
|
||||||
|
if self.entity_category == EntityCategory.CONFIG:
|
||||||
|
raise HomeAssistantError(
|
||||||
|
f"Entity {self.entity_id} cannot be added as the entity category is set to config"
|
||||||
|
)
|
||||||
|
|
||||||
if not self.registry_entry:
|
if not self.registry_entry:
|
||||||
return
|
return
|
||||||
self._async_read_entity_options()
|
self._async_read_entity_options()
|
||||||
|
|
|
@ -97,7 +97,7 @@ async def test_generic_numeric_sensor_with_entity_category_and_icon(
|
||||||
key=1,
|
key=1,
|
||||||
name="my sensor",
|
name="my sensor",
|
||||||
unique_id="my_sensor",
|
unique_id="my_sensor",
|
||||||
entity_category=ESPHomeEntityCategory.CONFIG,
|
entity_category=ESPHomeEntityCategory.DIAGNOSTIC,
|
||||||
icon="mdi:leaf",
|
icon="mdi:leaf",
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
@ -117,7 +117,7 @@ async def test_generic_numeric_sensor_with_entity_category_and_icon(
|
||||||
entry = entity_reg.async_get("sensor.test_mysensor")
|
entry = entity_reg.async_get("sensor.test_mysensor")
|
||||||
assert entry is not None
|
assert entry is not None
|
||||||
assert entry.unique_id == "my_sensor"
|
assert entry.unique_id == "my_sensor"
|
||||||
assert entry.entity_category is EntityCategory.CONFIG
|
assert entry.entity_category is EntityCategory.DIAGNOSTIC
|
||||||
|
|
||||||
|
|
||||||
async def test_generic_numeric_sensor_state_class_measurement(
|
async def test_generic_numeric_sensor_state_class_measurement(
|
||||||
|
|
|
@ -23,6 +23,7 @@ from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
SERVICE_RELOAD,
|
SERVICE_RELOAD,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
|
EntityCategory,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.generated.mqtt import MQTT
|
from homeassistant.generated.mqtt import MQTT
|
||||||
|
@ -1635,9 +1636,9 @@ async def help_test_entity_category(
|
||||||
entry = ent_registry.async_get(entity_id)
|
entry = ent_registry.async_get(entity_id)
|
||||||
assert entry is not None and entry.entity_category is None
|
assert entry is not None and entry.entity_category is None
|
||||||
|
|
||||||
# Discover an entity with entity category set to "config"
|
# Discover an entity with entity category set to "diagnostic"
|
||||||
unique_id = "veryunique2"
|
unique_id = "veryunique2"
|
||||||
config["entity_category"] = "config"
|
config["entity_category"] = EntityCategory.DIAGNOSTIC
|
||||||
config["unique_id"] = unique_id
|
config["unique_id"] = unique_id
|
||||||
data = json.dumps(config)
|
data = json.dumps(config)
|
||||||
async_fire_mqtt_message(hass, f"homeassistant/{domain}/{unique_id}/config", data)
|
async_fire_mqtt_message(hass, f"homeassistant/{domain}/{unique_id}/config", data)
|
||||||
|
@ -1645,7 +1646,7 @@ async def help_test_entity_category(
|
||||||
entity_id = ent_registry.async_get_entity_id(domain, mqtt.DOMAIN, unique_id)
|
entity_id = ent_registry.async_get_entity_id(domain, mqtt.DOMAIN, unique_id)
|
||||||
assert entity_id is not None and hass.states.get(entity_id)
|
assert entity_id is not None and hass.states.get(entity_id)
|
||||||
entry = ent_registry.async_get(entity_id)
|
entry = ent_registry.async_get(entity_id)
|
||||||
assert entry is not None and entry.entity_category == "config"
|
assert entry is not None and entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||||
|
|
||||||
# Discover an entity with entity category set to "no_such_category"
|
# Discover an entity with entity category set to "no_such_category"
|
||||||
unique_id = "veryunique3"
|
unique_id = "veryunique3"
|
||||||
|
|
|
@ -26,6 +26,7 @@ from homeassistant.const import (
|
||||||
ATTR_UNIT_OF_MEASUREMENT,
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
|
EntityCategory,
|
||||||
UnitOfEnergy,
|
UnitOfEnergy,
|
||||||
UnitOfLength,
|
UnitOfLength,
|
||||||
UnitOfMass,
|
UnitOfMass,
|
||||||
|
@ -2496,3 +2497,25 @@ def test_device_class_units_state_classes(hass: HomeAssistant) -> None:
|
||||||
) - NON_NUMERIC_DEVICE_CLASSES - {SensorDeviceClass.MONETARY}
|
) - NON_NUMERIC_DEVICE_CLASSES - {SensorDeviceClass.MONETARY}
|
||||||
# DEVICE_CLASS_STATE_CLASSES should include all device classes
|
# DEVICE_CLASS_STATE_CLASSES should include all device classes
|
||||||
assert set(DEVICE_CLASS_STATE_CLASSES) == set(SensorDeviceClass)
|
assert set(DEVICE_CLASS_STATE_CLASSES) == set(SensorDeviceClass)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_entity_category_config_raises_error(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
"""Test error is raised when entity category is set to config."""
|
||||||
|
platform = getattr(hass.components, "test.sensor")
|
||||||
|
platform.init(empty=True)
|
||||||
|
platform.ENTITIES["0"] = platform.MockSensor(
|
||||||
|
name="Test", entity_category=EntityCategory.CONFIG
|
||||||
|
)
|
||||||
|
|
||||||
|
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert (
|
||||||
|
"Entity sensor.test cannot be added as the entity category is set to config"
|
||||||
|
in caplog.text
|
||||||
|
)
|
||||||
|
|
||||||
|
assert not hass.states.get("sensor.test")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue