Don't allow entity category "CONFIG" for sensors (#5505)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Robert Resch 2023-10-10 09:00:05 +02:00 committed by GitHub
parent 511af5845e
commit c77a9ad363
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View file

@ -85,6 +85,7 @@ from esphome.const import (
DEVICE_CLASS_WATER,
DEVICE_CLASS_WEIGHT,
DEVICE_CLASS_WIND_SPEED,
ENTITY_CATEGORY_CONFIG,
)
from esphome.core import CORE, coroutine_with_priority
from esphome.cpp_generator import MockObjClass
@ -188,6 +189,15 @@ def validate_datapoint(value):
return validate_datapoint({CONF_FROM: cv.float_(a), CONF_TO: cv.float_(b)})
_SENSOR_ENTITY_CATEGORIES = {
k: v for k, v in cv.ENTITY_CATEGORIES.items() if k != ENTITY_CATEGORY_CONFIG
}
def sensor_entity_category(value):
return cv.enum(_SENSOR_ENTITY_CATEGORIES, lower=True)(value)
# Base
Sensor = sensor_ns.class_("Sensor", cg.EntityBase)
SensorPtr = Sensor.operator("ptr")
@ -246,6 +256,7 @@ SENSOR_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMPONENT_SCHEMA).extend(
cv.Optional(CONF_ACCURACY_DECIMALS): validate_accuracy_decimals,
cv.Optional(CONF_DEVICE_CLASS): validate_device_class,
cv.Optional(CONF_STATE_CLASS): validate_state_class,
cv.Optional(CONF_ENTITY_CATEGORY): sensor_entity_category,
cv.Optional("last_reset_type"): cv.invalid(
"last_reset_type has been removed since 2021.9.0. state_class: total_increasing should be used for total values."
),
@ -301,7 +312,7 @@ def sensor_schema(
(CONF_ACCURACY_DECIMALS, accuracy_decimals, validate_accuracy_decimals),
(CONF_DEVICE_CLASS, device_class, validate_device_class),
(CONF_STATE_CLASS, state_class, validate_state_class),
(CONF_ENTITY_CATEGORY, entity_category, cv.entity_category),
(CONF_ENTITY_CATEGORY, entity_category, sensor_entity_category),
]:
if default is not _UNDEF:
schema[cv.Optional(key, default=default)] = validator

View file

@ -1672,7 +1672,7 @@ def maybe_simple_value(*validators, **kwargs):
return validate
_ENTITY_CATEGORIES = {
ENTITY_CATEGORIES = {
ENTITY_CATEGORY_NONE: cg.EntityCategory.ENTITY_CATEGORY_NONE,
ENTITY_CATEGORY_CONFIG: cg.EntityCategory.ENTITY_CATEGORY_CONFIG,
ENTITY_CATEGORY_DIAGNOSTIC: cg.EntityCategory.ENTITY_CATEGORY_DIAGNOSTIC,
@ -1680,7 +1680,7 @@ _ENTITY_CATEGORIES = {
def entity_category(value):
return enum(_ENTITY_CATEGORIES, lower=True)(value)
return enum(ENTITY_CATEGORIES, lower=True)(value)
MQTT_COMPONENT_AVAILABILITY_SCHEMA = Schema(