add file notification platform
parent
d7dcee737b
commit
3abb185e16
|
@ -11,28 +11,22 @@ to your config/configuration.yaml
|
||||||
|
|
||||||
notify:
|
notify:
|
||||||
platform: file
|
platform: file
|
||||||
path: PATH_TO_FILE
|
|
||||||
filename: FILENAME
|
filename: FILENAME
|
||||||
timestamp: 1 or 0
|
timestamp: 1 or 0
|
||||||
|
|
||||||
Variables:
|
Variables:
|
||||||
|
|
||||||
path
|
|
||||||
*Required
|
|
||||||
Path to the directory that contains your file. You need to have write
|
|
||||||
permission for that directory. The directory will be created if it doesn't
|
|
||||||
exist.
|
|
||||||
|
|
||||||
filename
|
filename
|
||||||
*Required
|
*Required
|
||||||
Name of the file to use. The file will be created if it doesn't exist.
|
Name of the file to use. The file will be created if it doesn't exist and saved
|
||||||
|
in your config/ folder.
|
||||||
|
|
||||||
date
|
timestamp
|
||||||
*Required
|
*Required
|
||||||
Add a timestamp to the entry, valid entries are 1 or 0.
|
Add a timestamp to the entry, valid entries are 1 or 0.
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
from pathlib import (Path, PurePath)
|
import os
|
||||||
|
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
from homeassistant.helpers import validate_config
|
from homeassistant.helpers import validate_config
|
||||||
|
@ -46,51 +40,37 @@ def get_service(hass, config):
|
||||||
""" Get the file notification service. """
|
""" Get the file notification service. """
|
||||||
|
|
||||||
if not validate_config(config,
|
if not validate_config(config,
|
||||||
{DOMAIN: ['path',
|
{DOMAIN: ['filename',
|
||||||
'filename',
|
|
||||||
'timestamp']},
|
'timestamp']},
|
||||||
_LOGGER):
|
_LOGGER):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
path = config[DOMAIN]['path']
|
|
||||||
filename = config[DOMAIN]['filename']
|
filename = config[DOMAIN]['filename']
|
||||||
filepath = Path(path, filename)
|
timestamp = config[DOMAIN]['timestamp']
|
||||||
|
|
||||||
# pylint: disable=no-member
|
return FileNotificationService(hass, filename, timestamp)
|
||||||
if not filepath.parent.exists():
|
|
||||||
try:
|
|
||||||
filepath.parent.mkdir(parents=True)
|
|
||||||
filepath.touch(mode=0o644, exist_ok=True)
|
|
||||||
except:
|
|
||||||
_LOGGER.exception("No write permission to given location.")
|
|
||||||
# raise PermissionError('') from None
|
|
||||||
# raise FileNotFoundError('') from None
|
|
||||||
return None
|
|
||||||
|
|
||||||
return FileNotificationService(filepath, config[DOMAIN]['timestamp'])
|
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
class FileNotificationService(BaseNotificationService):
|
class FileNotificationService(BaseNotificationService):
|
||||||
""" Implements notification service for the File service. """
|
""" Implements notification service for the File service. """
|
||||||
|
|
||||||
# pylint: disable=no-member
|
def __init__(self, hass, filename, add_timestamp):
|
||||||
def __init__(self, filepath, add_timestamp):
|
self.filepath = os.path.join(hass.config.config_dir, filename)
|
||||||
self._filepath = str(PurePath(filepath))
|
self.add_timestamp = add_timestamp
|
||||||
self._add_timestamp = add_timestamp
|
|
||||||
|
|
||||||
def send_message(self, message="", **kwargs):
|
def send_message(self, message="", **kwargs):
|
||||||
""" Send a message to a file. """
|
""" Send a message to a file. """
|
||||||
|
|
||||||
file = open(self._filepath, 'a')
|
file = open(self.filepath, 'a')
|
||||||
if not Path(self._filepath).stat().st_size:
|
if os.stat(self.filepath).st_size == 0:
|
||||||
title = '{} notifications (Log started: {})\n{}\n'.format(
|
title = '{} notifications (Log started: {})\n{}\n'.format(
|
||||||
kwargs.get(ATTR_TITLE),
|
kwargs.get(ATTR_TITLE),
|
||||||
dt_util.strip_microseconds(dt_util.utcnow()),
|
dt_util.strip_microseconds(dt_util.utcnow()),
|
||||||
'-'*80)
|
'-'*80)
|
||||||
file.write(title)
|
file.write(title)
|
||||||
|
|
||||||
if self._add_timestamp == 1:
|
if self.add_timestamp == 1:
|
||||||
text = '{} {}\n'.format(dt_util.utcnow(), message)
|
text = '{} {}\n'.format(dt_util.utcnow(), message)
|
||||||
file.write(text)
|
file.write(text)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue