Fix default entity name not the device default entity when no name set on MQTT subentry entity (#144263)

This commit is contained in:
Jan Bouwhuis 2025-05-05 21:26:56 +02:00 committed by GitHub
parent 1879b8c27f
commit 0bf807b96e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 3 deletions

View File

@ -465,7 +465,7 @@ class PlatformField:
required: bool
validator: Callable[..., Any]
error: str | None = None
default: str | int | bool | vol.Undefined = vol.UNDEFINED
default: str | int | bool | None | vol.Undefined = vol.UNDEFINED
is_schema_default: bool = False
exclude_from_reconfig: bool = False
conditions: tuple[dict[str, Any], ...] | None = None
@ -515,6 +515,7 @@ COMMON_ENTITY_FIELDS = {
required=False,
validator=str,
exclude_from_reconfig=True,
default=None,
),
CONF_ENTITY_PICTURE: PlatformField(
selector=TEXT_SELECTOR, required=False, validator=cv.url, error="invalid_url"
@ -1324,7 +1325,10 @@ def data_schema_from_fields(
vol.Required(field_name, default=field_details.default)
if field_details.required
else vol.Optional(
field_name, default=field_details.default
field_name,
default=field_details.default
if field_details.default is not None
else vol.UNDEFINED,
): field_details.selector(component_data_with_user_input) # type: ignore[operator]
if field_details.custom_filtering
else field_details.selector
@ -1375,12 +1379,14 @@ def data_schema_from_fields(
@callback
def subentry_schema_default_data_from_fields(
data_schema_fields: dict[str, PlatformField],
component_data: dict[str, Any],
) -> dict[str, Any]:
"""Generate custom data schema from platform fields or device data."""
return {
key: field.default
for key, field in data_schema_fields.items()
if field.is_schema_default
or (field.default is not vol.UNDEFINED and key not in component_data)
}
@ -2206,7 +2212,7 @@ class MQTTSubentryFlowHandler(ConfigSubentryFlow):
for component_data in self._subentry_data["components"].values():
platform = component_data[CONF_PLATFORM]
subentry_default_data = subentry_schema_default_data_from_fields(
PLATFORM_ENTITY_FIELDS[platform]
PLATFORM_ENTITY_FIELDS[platform] | COMMON_ENTITY_FIELDS, component_data
)
component_data.update(subentry_default_data)

View File

@ -87,6 +87,7 @@ MOCK_SUBENTRY_NOTIFY_COMPONENT2 = {
MOCK_SUBENTRY_NOTIFY_COMPONENT_NO_NAME = {
"5269352dd9534c908d22812ea5d714cd": {
"platform": "notify",
"name": None,
"command_topic": "test-topic",
"command_template": "{{ value }}",
"entity_picture": "https://example.com/5269352dd9534c908d22812ea5d714cd",