From 0d48d5a5ec945a0615d361322b0170c0b825050a Mon Sep 17 00:00:00 2001 From: dougiteixeira <31328123+dougiteixeira@users.noreply.github.com> Date: Sun, 7 Jul 2024 11:40:02 -0300 Subject: [PATCH] Remove device ID from config schema via YAML in helper template (#120708) --- homeassistant/components/template/binary_sensor.py | 11 ++++++++--- homeassistant/components/template/sensor.py | 14 +++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/template/binary_sensor.py b/homeassistant/components/template/binary_sensor.py index 4618e30b1f3..68b3cd6d35a 100644 --- a/homeassistant/components/template/binary_sensor.py +++ b/homeassistant/components/template/binary_sensor.py @@ -88,10 +88,15 @@ BINARY_SENSOR_SCHEMA = vol.Schema( vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA, vol.Required(CONF_STATE): cv.template, vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string, - vol.Optional(CONF_DEVICE_ID): selector.DeviceSelector(), } ).extend(TEMPLATE_ENTITY_COMMON_SCHEMA.schema) +BINARY_SENSOR_CONFIG_SCHEMA = BINARY_SENSOR_SCHEMA.extend( + { + vol.Optional(CONF_DEVICE_ID): selector.DeviceSelector(), + } +) + LEGACY_BINARY_SENSOR_SCHEMA = vol.All( cv.deprecated(ATTR_ENTITY_ID), vol.Schema( @@ -206,7 +211,7 @@ async def async_setup_entry( """Initialize config entry.""" _options = dict(config_entry.options) _options.pop("template_type") - validated_config = BINARY_SENSOR_SCHEMA(_options) + validated_config = BINARY_SENSOR_CONFIG_SCHEMA(_options) async_add_entities( [BinarySensorTemplate(hass, validated_config, config_entry.entry_id)] ) @@ -217,7 +222,7 @@ def async_create_preview_binary_sensor( hass: HomeAssistant, name: str, config: dict[str, Any] ) -> BinarySensorTemplate: """Create a preview sensor.""" - validated_config = BINARY_SENSOR_SCHEMA(config | {CONF_NAME: name}) + validated_config = BINARY_SENSOR_CONFIG_SCHEMA(config | {CONF_NAME: name}) return BinarySensorTemplate(hass, validated_config, None) diff --git a/homeassistant/components/template/sensor.py b/homeassistant/components/template/sensor.py index 51669f11afe..70a2d5dd650 100644 --- a/homeassistant/components/template/sensor.py +++ b/homeassistant/components/template/sensor.py @@ -88,7 +88,6 @@ SENSOR_SCHEMA = vol.All( { vol.Required(CONF_STATE): cv.template, vol.Optional(ATTR_LAST_RESET): cv.template, - vol.Optional(CONF_DEVICE_ID): selector.DeviceSelector(), } ) .extend(TEMPLATE_SENSOR_BASE_SCHEMA.schema) @@ -97,6 +96,15 @@ SENSOR_SCHEMA = vol.All( ) +SENSOR_CONFIG_SCHEMA = vol.All( + vol.Schema( + { + vol.Required(CONF_STATE): cv.template, + vol.Optional(CONF_DEVICE_ID): selector.DeviceSelector(), + } + ).extend(TEMPLATE_SENSOR_BASE_SCHEMA.schema), +) + LEGACY_SENSOR_SCHEMA = vol.All( cv.deprecated(ATTR_ENTITY_ID), vol.Schema( @@ -230,7 +238,7 @@ async def async_setup_entry( """Initialize config entry.""" _options = dict(config_entry.options) _options.pop("template_type") - validated_config = SENSOR_SCHEMA(_options) + validated_config = SENSOR_CONFIG_SCHEMA(_options) async_add_entities([SensorTemplate(hass, validated_config, config_entry.entry_id)]) @@ -239,7 +247,7 @@ def async_create_preview_sensor( hass: HomeAssistant, name: str, config: dict[str, Any] ) -> SensorTemplate: """Create a preview sensor.""" - validated_config = SENSOR_SCHEMA(config | {CONF_NAME: name}) + validated_config = SENSOR_CONFIG_SCHEMA(config | {CONF_NAME: name}) return SensorTemplate(hass, validated_config, None)