2015-06-11 12:32:03 +00:00
|
|
|
"""
|
2016-03-08 10:46:32 +00:00
|
|
|
Support for file notification.
|
2015-06-11 12:32:03 +00:00
|
|
|
|
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
|
|
|
|
2016-08-31 16:12:34 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2015-06-11 12:32:03 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
from homeassistant.components.notify import (
|
2016-09-01 13:35:46 +00:00
|
|
|
ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService)
|
2016-08-31 16:12:34 +00:00
|
|
|
from homeassistant.const import CONF_FILENAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
CONF_TIMESTAMP = 'timestamp'
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_FILENAME): cv.string,
|
|
|
|
vol.Optional(CONF_TIMESTAMP, default=False): cv.boolean,
|
|
|
|
})
|
2015-06-11 12:32:03 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def get_service(hass, config, discovery_info=None):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Get the file notification service."""
|
2016-08-31 16:12:34 +00:00
|
|
|
filename = config[CONF_FILENAME]
|
|
|
|
timestamp = config[CONF_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
|
|
|
|
|
|
|
|
|
|
|
class FileNotificationService(BaseNotificationService):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Implement the notification service for the File service."""
|
2015-06-11 12:32:03 +00:00
|
|
|
|
2015-06-20 09:00:20 +00:00
|
|
|
def __init__(self, hass, filename, add_timestamp):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Initialize the service."""
|
2015-06-20 09:00:20 +00:00
|
|
|
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):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""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(
|
2016-09-01 13:35:46 +00:00
|
|
|
kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT),
|
2016-04-16 07:55:35 +00:00
|
|
|
dt_util.utcnow().isoformat(),
|
2016-03-18 12:33:52 +00:00
|
|
|
'-' * 80)
|
2015-06-20 21:03:53 +00:00
|
|
|
file.write(title)
|
|
|
|
|
2016-08-31 16:12:34 +00:00
|
|
|
if self.add_timestamp:
|
2016-04-16 07:55:35 +00:00
|
|
|
text = '{} {}\n'.format(dt_util.utcnow().isoformat(), message)
|
2015-06-20 21:03:53 +00:00
|
|
|
else:
|
|
|
|
text = '{}\n'.format(message)
|
2016-09-01 13:35:00 +00:00
|
|
|
file.write(text)
|