Skip to content

Commit

Permalink
Add check that sensors don't have EntityCategory.CONFIG set (#101471)
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored Oct 9, 2023
1 parent 27b6325 commit f7292d5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
6 changes: 3 additions & 3 deletions homeassistant/components/nextcloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,15 @@ class NextcloudSensorEntityDescription(SensorEntityDescription):
key="server_php_max_execution_time",
translation_key="nextcloud_server_php_max_execution_time",
device_class=SensorDeviceClass.DURATION,
entity_category=EntityCategory.CONFIG,
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:language-php",
native_unit_of_measurement=UnitOfTime.SECONDS,
),
NextcloudSensorEntityDescription(
key="server_php_memory_limit",
translation_key="nextcloud_server_php_memory_limit",
device_class=SensorDeviceClass.DATA_SIZE,
entity_category=EntityCategory.CONFIG,
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:language-php",
native_unit_of_measurement=UnitOfInformation.BYTES,
suggested_display_precision=1,
Expand All @@ -366,7 +366,7 @@ class NextcloudSensorEntityDescription(SensorEntityDescription):
key="server_php_upload_max_filesize",
translation_key="nextcloud_server_php_upload_max_filesize",
device_class=SensorDeviceClass.DATA_SIZE,
entity_category=EntityCategory.CONFIG,
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:language-php",
native_unit_of_measurement=UnitOfInformation.BYTES,
suggested_display_precision=1,
Expand Down
7 changes: 7 additions & 0 deletions homeassistant/components/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@
DEVICE_CLASS_TIMESTAMP,
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
DEVICE_CLASS_VOLTAGE,
EntityCategory,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, State, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.config_validation import (
PLATFORM_SCHEMA,
Expand Down Expand Up @@ -251,6 +253,11 @@ def add_to_platform_start(
async def async_internal_added_to_hass(self) -> None:
"""Call when the sensor entity is 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:
return
self._async_read_entity_options()
Expand Down
4 changes: 2 additions & 2 deletions tests/components/esphome/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async def test_generic_numeric_sensor_with_entity_category_and_icon(
key=1,
name="my sensor",
unique_id="my_sensor",
entity_category=ESPHomeEntityCategory.CONFIG,
entity_category=ESPHomeEntityCategory.DIAGNOSTIC,
icon="mdi:leaf",
)
]
Expand All @@ -117,7 +117,7 @@ async def test_generic_numeric_sensor_with_entity_category_and_icon(
entry = entity_reg.async_get("sensor.test_mysensor")
assert entry is not None
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(
Expand Down
7 changes: 4 additions & 3 deletions tests/components/mqtt/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ATTR_ENTITY_ID,
SERVICE_RELOAD,
STATE_UNAVAILABLE,
EntityCategory,
)
from homeassistant.core import HomeAssistant
from homeassistant.generated.mqtt import MQTT
Expand Down Expand Up @@ -1635,17 +1636,17 @@ async def help_test_entity_category(
entry = ent_registry.async_get(entity_id)
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"
config["entity_category"] = "config"
config["entity_category"] = EntityCategory.DIAGNOSTIC
config["unique_id"] = unique_id
data = json.dumps(config)
async_fire_mqtt_message(hass, f"homeassistant/{domain}/{unique_id}/config", data)
await hass.async_block_till_done()
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)
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"
unique_id = "veryunique3"
Expand Down
23 changes: 23 additions & 0 deletions tests/components/sensor/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
ATTR_UNIT_OF_MEASUREMENT,
PERCENTAGE,
STATE_UNKNOWN,
EntityCategory,
UnitOfEnergy,
UnitOfLength,
UnitOfMass,
Expand Down Expand Up @@ -2496,3 +2497,25 @@ def test_device_class_units_state_classes(hass: HomeAssistant) -> None:
) - NON_NUMERIC_DEVICE_CLASSES - {SensorDeviceClass.MONETARY}
# DEVICE_CLASS_STATE_CLASSES should include all device classes
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")

0 comments on commit f7292d5

Please sign in to comment.