From 68a4928fb1b42418caee3f3247ccff132c25aaeb Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 11 Jun 2015 14:32:03 +0200 Subject: [PATCH 1/8] add file notification platform --- homeassistant/components/notify/file.py | 100 ++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 homeassistant/components/notify/file.py diff --git a/homeassistant/components/notify/file.py b/homeassistant/components/notify/file.py new file mode 100644 index 00000000000..832d9401079 --- /dev/null +++ b/homeassistant/components/notify/file.py @@ -0,0 +1,100 @@ +""" +homeassistant.components.notify.file +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +File notification service. + +Configuration: + +To use the File notifier you will need to add something like the following +to your config/configuration.yaml + +notify: + platform: file + path: PATH_TO_FILE + filename: FILENAME + timestamp: 1 or 0 + +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 +*Required +Name of the file to use. The file will be created if it doesn't exist. + +date +*Required +Add a timestamp to the entry, valid entries are 1 or 0. +""" +import logging +from pathlib import (Path, PurePath) + +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. """ + + if not validate_config(config, + {DOMAIN: ['path', + 'filename', + 'timestamp']}, + _LOGGER): + return None + + path = config[DOMAIN]['path'] + filename = config[DOMAIN]['filename'] + filepath = Path(path, filename) + + # pylint: disable=no-member + 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 +class FileNotificationService(BaseNotificationService): + """ Implements notification service for the File service. """ + + # pylint: disable=no-member + def __init__(self, filepath, add_timestamp): + self._filepath = str(PurePath(filepath)) + self._add_timestamp = add_timestamp + + def send_message(self, message="", **kwargs): + """ Send a message to a file. """ + + file = open(self._filepath, 'a') + if not Path(self._filepath).stat().st_size: + 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) + + file.close() From d7dcee737bdf17b10e7f2911b0c005d702afa93e Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 11 Jun 2015 16:16:36 +0200 Subject: [PATCH 2/8] add file.py --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index ee40c987ea2..a67dd6ae707 100644 --- a/.coveragerc +++ b/.coveragerc @@ -32,6 +32,7 @@ omit = homeassistant/components/light/hue.py homeassistant/components/media_player/cast.py homeassistant/components/media_player/mpd.py + homeassistant/components/notify/file.py homeassistant/components/notify/instapush.py homeassistant/components/notify/nma.py homeassistant/components/notify/pushbullet.py From 3abb185e1644e7b395201f5021c0e9a230e8df01 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sat, 20 Jun 2015 11:00:20 +0200 Subject: [PATCH 3/8] add file notification platform --- homeassistant/components/notify/file.py | 46 +++++++------------------ 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/notify/file.py b/homeassistant/components/notify/file.py index 832d9401079..84a93beee58 100644 --- a/homeassistant/components/notify/file.py +++ b/homeassistant/components/notify/file.py @@ -11,28 +11,22 @@ to your config/configuration.yaml notify: platform: file - path: PATH_TO_FILE filename: FILENAME timestamp: 1 or 0 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 *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 Add a timestamp to the entry, valid entries are 1 or 0. """ import logging -from pathlib import (Path, PurePath) +import os import homeassistant.util.dt as dt_util from homeassistant.helpers import validate_config @@ -46,51 +40,37 @@ def get_service(hass, config): """ Get the file notification service. """ if not validate_config(config, - {DOMAIN: ['path', - 'filename', + {DOMAIN: ['filename', 'timestamp']}, _LOGGER): return None - path = config[DOMAIN]['path'] filename = config[DOMAIN]['filename'] - filepath = Path(path, filename) + timestamp = config[DOMAIN]['timestamp'] - # pylint: disable=no-member - 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']) + return FileNotificationService(hass, filename, timestamp) # pylint: disable=too-few-public-methods class FileNotificationService(BaseNotificationService): """ Implements notification service for the File service. """ - # pylint: disable=no-member - def __init__(self, filepath, add_timestamp): - self._filepath = str(PurePath(filepath)) - self._add_timestamp = add_timestamp + def __init__(self, hass, filename, add_timestamp): + self.filepath = os.path.join(hass.config.config_dir, filename) + self.add_timestamp = add_timestamp def send_message(self, message="", **kwargs): """ Send a message to a file. """ - file = open(self._filepath, 'a') - if not Path(self._filepath).stat().st_size: + file = open(self.filepath, 'a') + 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: + if self.add_timestamp == 1: text = '{} {}\n'.format(dt_util.utcnow(), message) file.write(text) else: From 2f1b12a6f173c01f9631d0ad5a4d3c3f411983cb Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 11 Jun 2015 14:32:03 +0200 Subject: [PATCH 4/8] add file notification platform --- homeassistant/components/notify/file.py | 100 ++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 homeassistant/components/notify/file.py diff --git a/homeassistant/components/notify/file.py b/homeassistant/components/notify/file.py new file mode 100644 index 00000000000..832d9401079 --- /dev/null +++ b/homeassistant/components/notify/file.py @@ -0,0 +1,100 @@ +""" +homeassistant.components.notify.file +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +File notification service. + +Configuration: + +To use the File notifier you will need to add something like the following +to your config/configuration.yaml + +notify: + platform: file + path: PATH_TO_FILE + filename: FILENAME + timestamp: 1 or 0 + +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 +*Required +Name of the file to use. The file will be created if it doesn't exist. + +date +*Required +Add a timestamp to the entry, valid entries are 1 or 0. +""" +import logging +from pathlib import (Path, PurePath) + +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. """ + + if not validate_config(config, + {DOMAIN: ['path', + 'filename', + 'timestamp']}, + _LOGGER): + return None + + path = config[DOMAIN]['path'] + filename = config[DOMAIN]['filename'] + filepath = Path(path, filename) + + # pylint: disable=no-member + 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 +class FileNotificationService(BaseNotificationService): + """ Implements notification service for the File service. """ + + # pylint: disable=no-member + def __init__(self, filepath, add_timestamp): + self._filepath = str(PurePath(filepath)) + self._add_timestamp = add_timestamp + + def send_message(self, message="", **kwargs): + """ Send a message to a file. """ + + file = open(self._filepath, 'a') + if not Path(self._filepath).stat().st_size: + 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) + + file.close() From 3a1bc715b76ca7905ae5bf6bf472cbe5899f0f87 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 11 Jun 2015 16:16:36 +0200 Subject: [PATCH 5/8] add file.py --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index cf494acc679..45b67e14b84 100644 --- a/.coveragerc +++ b/.coveragerc @@ -32,6 +32,7 @@ omit = homeassistant/components/light/hue.py homeassistant/components/media_player/cast.py homeassistant/components/media_player/mpd.py + homeassistant/components/notify/file.py homeassistant/components/notify/instapush.py homeassistant/components/notify/nma.py homeassistant/components/notify/pushbullet.py From 832e9a631ef27b7cf531c52207318f188d14335e Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sat, 20 Jun 2015 11:00:20 +0200 Subject: [PATCH 6/8] add file notification platform --- homeassistant/components/notify/file.py | 46 +++++++------------------ 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/notify/file.py b/homeassistant/components/notify/file.py index 832d9401079..84a93beee58 100644 --- a/homeassistant/components/notify/file.py +++ b/homeassistant/components/notify/file.py @@ -11,28 +11,22 @@ to your config/configuration.yaml notify: platform: file - path: PATH_TO_FILE filename: FILENAME timestamp: 1 or 0 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 *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 Add a timestamp to the entry, valid entries are 1 or 0. """ import logging -from pathlib import (Path, PurePath) +import os import homeassistant.util.dt as dt_util from homeassistant.helpers import validate_config @@ -46,51 +40,37 @@ def get_service(hass, config): """ Get the file notification service. """ if not validate_config(config, - {DOMAIN: ['path', - 'filename', + {DOMAIN: ['filename', 'timestamp']}, _LOGGER): return None - path = config[DOMAIN]['path'] filename = config[DOMAIN]['filename'] - filepath = Path(path, filename) + timestamp = config[DOMAIN]['timestamp'] - # pylint: disable=no-member - 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']) + return FileNotificationService(hass, filename, timestamp) # pylint: disable=too-few-public-methods class FileNotificationService(BaseNotificationService): """ Implements notification service for the File service. """ - # pylint: disable=no-member - def __init__(self, filepath, add_timestamp): - self._filepath = str(PurePath(filepath)) - self._add_timestamp = add_timestamp + def __init__(self, hass, filename, add_timestamp): + self.filepath = os.path.join(hass.config.config_dir, filename) + self.add_timestamp = add_timestamp def send_message(self, message="", **kwargs): """ Send a message to a file. """ - file = open(self._filepath, 'a') - if not Path(self._filepath).stat().st_size: + file = open(self.filepath, 'a') + 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: + if self.add_timestamp == 1: text = '{} {}\n'.format(dt_util.utcnow(), message) file.write(text) else: From ef40b94a87fc36b5c8b3446a94330a5b877146e1 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sat, 20 Jun 2015 23:03:53 +0200 Subject: [PATCH 7/8] use with open --- homeassistant/components/notify/file.py | 28 ++++++++++++------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/notify/file.py b/homeassistant/components/notify/file.py index 84a93beee58..186bc53ca98 100644 --- a/homeassistant/components/notify/file.py +++ b/homeassistant/components/notify/file.py @@ -62,19 +62,17 @@ class FileNotificationService(BaseNotificationService): def send_message(self, message="", **kwargs): """ Send a message to a file. """ - file = open(self.filepath, 'a') - 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) + 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) - - file.close() + if self.add_timestamp == 1: + text = '{} {}\n'.format(dt_util.utcnow(), message) + file.write(text) + else: + text = '{}\n'.format(message) + file.write(text) From 13dac91fa6025e41e5ce56aebfde59be20fddf4a Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sat, 20 Jun 2015 23:41:24 +0200 Subject: [PATCH 8/8] remove blank line --- homeassistant/components/notify/file.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/notify/file.py b/homeassistant/components/notify/file.py index e428be0ff4b..186bc53ca98 100644 --- a/homeassistant/components/notify/file.py +++ b/homeassistant/components/notify/file.py @@ -76,4 +76,3 @@ class FileNotificationService(BaseNotificationService): else: text = '{}\n'.format(message) file.write(text) -