2015-06-11 12:32:03 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.notify.file
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
File notification service.
|
|
|
|
|
2015-10-13 20:25:41 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 17:33:11 +00:00
|
|
|
https://home-assistant.io/components/notify.file/
|
2015-06-11 12:32:03 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2015-06-20 09:00:20 +00:00
|
|
|
import os
|
2015-06-11 12:32:03 +00:00
|
|
|
|
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
from homeassistant.helpers import validate_config
|
|
|
|
from homeassistant.components.notify import (
|
|
|
|
DOMAIN, ATTR_TITLE, BaseNotificationService)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def get_service(hass, config):
|
|
|
|
""" Get the file notification service. """
|
|
|
|
|
2015-11-09 06:15:34 +00:00
|
|
|
if not validate_config({DOMAIN: config},
|
2015-06-20 09:00:20 +00:00
|
|
|
{DOMAIN: ['filename',
|
2015-06-11 12:32:03 +00:00
|
|
|
'timestamp']},
|
|
|
|
_LOGGER):
|
|
|
|
return None
|
|
|
|
|
2015-11-09 06:15:34 +00:00
|
|
|
filename = config['filename']
|
|
|
|
timestamp = config['timestamp']
|
2015-06-11 12:32:03 +00:00
|
|
|
|
2015-06-20 09:00:20 +00:00
|
|
|
return FileNotificationService(hass, filename, timestamp)
|
2015-06-11 12:32:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class FileNotificationService(BaseNotificationService):
|
|
|
|
""" Implements notification service for the File service. """
|
|
|
|
|
2015-06-20 09:00:20 +00:00
|
|
|
def __init__(self, hass, filename, add_timestamp):
|
|
|
|
self.filepath = os.path.join(hass.config.config_dir, filename)
|
|
|
|
self.add_timestamp = add_timestamp
|
2015-06-11 12:32:03 +00:00
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
|
|
|
""" Send a message to a file. """
|
|
|
|
|
2015-06-20 21:03:53 +00:00
|
|
|
with open(self.filepath, 'a') as file:
|
|
|
|
if os.stat(self.filepath).st_size == 0:
|
|
|
|
title = '{} notifications (Log started: {})\n{}\n'.format(
|
|
|
|
kwargs.get(ATTR_TITLE),
|
|
|
|
dt_util.strip_microseconds(dt_util.utcnow()),
|
|
|
|
'-'*80)
|
|
|
|
file.write(title)
|
|
|
|
|
|
|
|
if self.add_timestamp == 1:
|
|
|
|
text = '{} {}\n'.format(dt_util.utcnow(), message)
|
|
|
|
file.write(text)
|
|
|
|
else:
|
|
|
|
text = '{}\n'.format(message)
|
|
|
|
file.write(text)
|