1 """Support for file notification."""
3 from __future__
import annotations
6 from typing
import Any, TextIO
20 from .const
import CONF_TIMESTAMP, DEFAULT_NAME, DOMAIN, FILE_ICON
26 async_add_entities: AddEntitiesCallback,
28 """Set up notify entity."""
29 unique_id = entry.entry_id
34 """Implement the notification entity platform for the File service."""
36 _attr_icon = FILE_ICON
37 _attr_supported_features = NotifyEntityFeature.TITLE
39 def __init__(self, unique_id: str, config: dict[str, Any]) ->
None:
40 """Initialize the service."""
41 self._file_path: str = config[CONF_FILE_PATH]
42 self._add_timestamp: bool = config.get(CONF_TIMESTAMP,
False)
44 self.
_attr_name_attr_name = config.get(CONF_NAME, DEFAULT_NAME)
47 def send_message(self, message: str, title: str |
None =
None) ->
None:
48 """Send a message to a file."""
50 filepath = self._file_path
52 with open(filepath,
"a", encoding=
"utf8")
as file:
53 if os.stat(filepath).st_size == 0:
55 f
"{title or ATTR_TITLE_DEFAULT} notifications (Log"
56 f
" started: {dt_util.utcnow().isoformat()})\n{'-' * 80}\n"
60 if self._add_timestamp:
61 text = f
"{dt_util.utcnow().isoformat()} {message}\n"
65 except OSError
as exc:
67 translation_domain=DOMAIN,
68 translation_key=
"write_access_failed",
69 translation_placeholders={
"filename": filepath,
"exc": f
"{exc!r}"},
None __init__(self, str unique_id, dict[str, Any] config)
None send_message(self, str message, str|None title=None)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
None open(self, **Any kwargs)