Skip to content

Commit

Permalink
Do not load legacy notify service for new config entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh committed May 11, 2024
1 parent 0547655 commit 3093207
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 13 deletions.
4 changes: 2 additions & 2 deletions homeassistant/components/file/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/file/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
11 changes: 6 additions & 5 deletions homeassistant/components/file/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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."""
Expand Down
4 changes: 0 additions & 4 deletions homeassistant/components/file/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/components/file/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"platform": "notify",
"file_path": "some_file",
"timestamp": True,
"name": "File",
}
MOCK_CONFIG_SENSOR = {
"platform": "sensor",
Expand Down

0 comments on commit 3093207

Please sign in to comment.