diff --git a/homeassistant/components/file/__init__.py b/homeassistant/components/file/__init__.py index 7d0b8bf0fc186..15a99542bd5e2 100644 --- a/homeassistant/components/file/__init__.py +++ b/homeassistant/components/file/__init__.py @@ -2,7 +2,7 @@ from homeassistant.components.notify import migrate_notify_issue from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from homeassistant.const import CONF_FILE_PATH, CONF_PLATFORM, Platform +from homeassistant.const import CONF_FILE_PATH, CONF_NAME, CONF_PLATFORM, Platform from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import ( @@ -81,7 +81,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await hass.config_entries.async_forward_entry_setups( entry, [Platform(entry.data[CONF_PLATFORM])] ) - if entry.data[CONF_PLATFORM] == Platform.NOTIFY: + if entry.data[CONF_PLATFORM] == Platform.NOTIFY and CONF_NAME in entry.data: # The notify platform is not yet set up as entry, so # forward setup config through discovery to ensure setup notify service. # This is needed as long as the legacy service is not migrated diff --git a/homeassistant/components/file/config_flow.py b/homeassistant/components/file/config_flow.py index 9c6bcb4df003e..45d8360a9cf1c 100644 --- a/homeassistant/components/file/config_flow.py +++ b/homeassistant/components/file/config_flow.py @@ -42,7 +42,6 @@ FILE_NOTIFY_SCHEMA = vol.Schema( { - vol.Optional(CONF_NAME, default=DEFAULT_NAME): TEXT_SELECTOR, vol.Required(CONF_FILE_PATH): TEXT_SELECTOR, vol.Optional(CONF_TIMESTAMP, default=False): BOOLEAN_SELECTOR, } diff --git a/homeassistant/components/file/notify.py b/homeassistant/components/file/notify.py index 7f5ac7b1f9f0f..b36cca6d10aa5 100644 --- a/homeassistant/components/file/notify.py +++ b/homeassistant/components/file/notify.py @@ -25,7 +25,6 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from homeassistant.util import slugify import homeassistant.util.dt as dt_util from .const import CONF_TIMESTAMP, DOMAIN, FILE_ICON @@ -107,7 +106,8 @@ async def async_setup_entry( ) -> None: """Set up notify entity.""" config: dict[str, Any] = dict(entry.data) - async_add_entities([FileNotifyEntity(config)]) + unique_id = entry.entry_id + async_add_entities([FileNotifyEntity(unique_id, config)]) class FileNotifyEntity(NotifyEntity): @@ -116,12 +116,13 @@ class FileNotifyEntity(NotifyEntity): _attr_icon = FILE_ICON _attr_supported_features = NotifyEntityFeature.TITLE - def __init__(self, config: dict[str, Any]) -> None: + def __init__(self, unique_id: str, config: dict[str, Any]) -> None: """Initialize the service.""" self._file_path: str = config[CONF_FILE_PATH] self._add_timestamp: bool = config.get(CONF_TIMESTAMP, False) - self._attr_name = config[CONF_NAME] - self._attr_unique_id = slugify(f"{config[CONF_NAME]}_{config[CONF_FILE_PATH]}") + # Only import a name from an imported entity + self._attr_name = config.get(CONF_NAME) + self._attr_unique_id = unique_id def send_message(self, message: str, title: str | None = None) -> None: """Send a message to a file.""" diff --git a/homeassistant/components/file/strings.json b/homeassistant/components/file/strings.json index 243695b79cb7b..9d49e6300e95d 100644 --- a/homeassistant/components/file/strings.json +++ b/homeassistant/components/file/strings.json @@ -12,13 +12,11 @@ "title": "File sensor", "description": "Set up a file based sensor", "data": { - "name": "Name", "file_path": "File path", "value_template": "Value template", "unit_of_measurement": "Unit of measurement" }, "data_description": { - "name": "Name of the file based sensor", "file_path": "The local file path to retrieve the sensor value from", "value_template": "A template to render the the sensors value based on the file content", "unit_of_measurement": "Unit of measurement for the sensor" @@ -29,12 +27,10 @@ "description": "Set up a service that allows to write notification to a file.", "data": { "file_path": "[%key:component::file::config::step::sensor::data::file_path%]", - "name": "[%key:component::file::config::step::sensor::data::name%]", "timestamp": "Timestamp" }, "data_description": { "file_path": "A local file path to write the notification to", - "name": "Name of the notify service", "timestamp": "Add a timestamp to the notification" } } diff --git a/tests/components/file/test_config_flow.py b/tests/components/file/test_config_flow.py index 1378793f9bdb5..9bf9cd1b113fe 100644 --- a/tests/components/file/test_config_flow.py +++ b/tests/components/file/test_config_flow.py @@ -16,7 +16,6 @@ "platform": "notify", "file_path": "some_file", "timestamp": True, - "name": "File", } MOCK_CONFIG_SENSOR = { "platform": "sensor",